构造函数+析构函数+拷贝构造函数 --类和对象2

空类

// 没有成员变量的类
class B
{
public:
	void Print(){}
};
class C
{
};
int main()
{
	cout << sizeof(B) << endl;
	cout << sizeof(C) << endl;
	C c1, c2;
	return 0;
}

在这里插入图片描述

默认的函数

空类里面不是什么都没有,空类里面中会自动产生6个默认的成员函数
我们不写这六个函数–会默认生成,但是默认生成的有限制有时候是需要我们自己写
在这里插入图片描述

构造函数

1、构造函数(初始化和重载)替代初始化函数init
在这里插入图片描述

class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date()//初始化
	{
		_year = 0;
		_month = 1;
		_day = 1;
	}
	Date(int year)//若前面没有初始化这里就会出错或者写一个Date(){}在前面
	{
		_year = year;
		_month = 1;
		_day = 1;
	}
	Date(int year, int month, int day)//重载
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
class Stack
{
public:
	void Init()
	{
		_a = nullptr;
		_top = _capacity = 0;
	}
	Stack()//初始化
	{
		_a = nullptr;
		_top = _capacity = 0;
	}
	Stack(int capacity)//重载
	{
		_a = (int*)malloc(sizeof(int)*capacity);
		if (_a == nullptr)
		{
			cout << "malloc fail" << endl;
			exit(-1);
		}
		_top = 0;
		_capacity = capacity;
	}
private:
	int* _a;
	int _top;
	int _capacity;
};
int main()
{
	// 经常会忘记调用Init函数,能否做到对象定义出来就初始化了?--构造函数(构造函数并不是去创建对象而是去初始化对象)
	Date d1;
	//初始化为特定的日期--函数重载
	Date d2(2001, 7, 19);
	//d1.Init(2021, 10, 9);
	Stack s1;
	Stack s2(10);
	//s1.Init();
	return 0;
}

在这里插入图片描述
2、如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,但是如果已经写了构造函数(有参或者无参)就不会再自动生成。
在这里插入图片描述

class A
{
public:
	A()
	{
		_a1 = 0;
		_a2 = 1;
	}
	void Print()
	{
		cout << _a1 << "/" << _a2  << endl;
	}
private:
	int _a1;
	int _a2;
};
class Date
{
public:
	/*Date()//不写这个构造函数的时候不会初始化
	{
	_year = 0;
	_month = 1;
	_day = 1;
	}
	*/
	//Date(int year, int month, int day)
	//{
	//_year = year;
	//_month = month;
	//_day = day;
	//}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	// 内置类型  int、char、double、指针....
	int _year;
	int _month;
	int _day;
	// 自定义类型--struct/class
	A _aa;
};
int main()
{
	Date d1;
	d1.Print();
	A a;
	a.Print();
	return 0;
}

在这里插入图片描述
自定义类型也没有无参构造函数

class A
{
public:
	/*A()
	{
	_a1 = 0;
	_a2 = 1;
	cout << "A()" << endl;
	}*/
	void Print()
	{
		cout << _a1 << "/" << _a2  << endl;
	}
private:
	//内置类型
	int _a1;
	int _a2;
};
class Date
{
public:
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	//内置类型
	int _year;
	int _month;
	int _day;
	// 自定义类型   struct/class--但此时无这个构造函数
	A _aa;
};
int main()
{
	Date d1;
	d1.Print();
	A a;
	a.Print();
	return 0;
}

在这里插入图片描述
3、内置类型–赋值就像缺省值—有构造函数来初始化就不用它了,没有才用

class Date
{
public:
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	Date()
	{
		_year = 1999;
	}
private:
	//在这里赋值不是初始化,就像缺省值一样
	int _year = 0;
	int _month = 1;
	int _day = 1;
};
int main()
{
	Date d1;
	d1.Print();
	return 0;
}

在这里插入图片描述
4、注意点
默认构造函数包括:编译器默认生成+无参的构造函数+全缺省的构造函数
在这里插入图片描述
5、就近原则

class Date
{
public:
	Date(int year)
	{
		this->year = year;
	}
private:
	int year;
};
int main()
{
	Date d(1);
}
  • 输出1
  • 如果year前面没有this来指定则year是随机值不是1

析构函数

在这里插入图片描述
析构函数–自动调用

  • 像上面data那样的类不需要析构函数因为它没有什么资源需要处理
  • 但是stack就需要,stack中对象中的资源需要清理工作,就用析构函数,防止忘记destory会内存泄漏
  • 两个栈实现队列,对于MyQueue,我们就可以不写构造函数,让编译器自动生成的构造函数和析构函数都可以完成使命
    跟构造函数类似,我们不写,内置类型成员不处理,自定义类型成员会去调用他析构函数
class Stack
{
public:
	Stack(int capacity = 4)//构造函数
	{
		_a = (int*)malloc(sizeof(int)*capacity);
		if (_a == nullptr)
		{
			cout << "malloc fail" << endl;
			exit(-1);
		}
		_top = 0;
		_capacity = capacity;
	}
	~Stack()//析构函数
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	int* _a;
	int _top;
	int _capacity;
};
int main()
{
	Stack st;
	return 0;
}

在这里插入图片描述

拷贝构造函数

  • 拷贝构造函数是构造函数的一个重载形式
  • 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用
    1、传值传参会引发无穷递归–必须使用拷贝构造函数
    在这里插入图片描述
    2、指针来拷贝构造
class Date
{
public:	
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(Date* p)
	{
	_year = p->_year;
	_month = p->_month;
	_day = p->_day;
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(2021, 10, 9);
	d1.Print();
	d2.Print();
	// 拷贝复制一个d2对象出来
	Date d4(&d2);
	d4.Print();
	return 0;
}

3、引用来拷贝构造-比指针看起来好

class Date
{
public:	
	Date(int year = 1, int month = 1, int day = 1)//构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// 如果引用传参,不是做输出型参数(形参的改变会影响实参--swap),最好用const & 做保护
	// Date d3(d2);
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		//d._day = _day;--err,所以必须要用const &做保护
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(2021, 10, 9);
	d1.Print();
	d2.Print();
	// 拷贝复制一个d2对象出来
	Date d3 = d2;
	d3.Print();
	return 0;
}

传参用传值会调用拷贝构造
在这里插入图片描述
我们不写拷贝函数,编译器默认生成拷贝构造,跟构造和析构又不太一样的,不会去区分内置类型和自定义类型成员,都会处理

  • 1、内置类型,字节序的浅 / 值拷贝(按字节序的拷贝–memcpy)
  • 2、自定义类型,会去调用他的拷贝构造完成拷贝
    我们还是需要写,不然会崩溃程序,像stack这样的类编译器会默认生成拷贝构造完成的就是浅拷贝–会出问题,解决方案:自己实现深拷贝构造函数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值