【C++】类的6个默认函数(一)

【C++】类的6个默认函数(一)

(本blog主要讲前三个类的默认函数,即:构造函数、析构函数、拷贝构造函数)

类的6个默认函数为:

1、构造函数

2、析构函数

3、拷贝构造函数

4、赋值运算符的重载函数

5、取地址操作符的重载函数

6、const修饰的取地址操作符的重载函数

特点:共有的、内联的

对象的生成:

1、开辟内存

2、对对象的内存空间进行初始化(调用构造函数)

(注意:构造函数并不是生成对象的)

①构造函数

生成新对象时用来赋资源、初始化内存空间的。(系统调用、重载决议)

默认构造函数是Goods(),没有返回值。


#include<iostream>
#pragma warning(disable:4996);
 
class Goods
{
public:
	Goods()//不含参数
	{
		std::cout<<"Goods::Goods()"<<std::endl;
	}
	Goods(int amount)//含一个参数
	{
		std::cout<<"Goods::Goods(int )"<<std::endl;
		mname = new char[1]();
		mamount = amount;
	}
	Goods(char *name,float price)//含两个参数
	{
		std::cout<<"Goods::Goods(char ,float )"<<std::endl;
		mname = new char[strlen(name)+1]();
		strcpy(mname,name);
		mprice = price;
	}
	Goods(char *name,float price,int amount)//含三个参数
	{
		std::cout<<"Goods::Goods(char ,float ,int )"<<std::endl;
		mname = new char[strlen(name)+1]();
		strcpy(mname,name);
		mprice = price;
		mamount = amount;
	}
 
private:
	char *mname;
	float mprice;
	int mamount;
};
 
int main()
{
	Goods goods1;
	Goods goods2("goods2",10.5);
	Goods goods3(20);
	Goods goods4("goods4",10.5,20);
	return 0;
}

通过以上代码,可以说明一个类里可以有多个构造函数,构造函数可以重载(“生而不同”)

②析构函数

释放对象所占其他资源,销毁资源。

#include<iostream>
 
class Goods
{
public:
	Goods()
	{
		std::cout<<this<<"Goods::Goods()"<<std::endl;
	}
	Goods(int amount)
	{
		std::cout<<this<<"Goods::Goods(int )"<<std::endl;
		mname = new char[1]();
		mamount = amount;
	}
	Goods(char *name,float price)
	{
		std::cout<<this<<"Goods::Goods(char ,float )"<<std::endl;
		mname = new char[strlen(name)+1]();
		strcpy(mname,name);
		mprice = price;
	}
	Goods(char *name,float price,int amount)
	{
		std::cout<<this<<"Goods::Goods(char ,float ,int )"<<std::endl;
		mname = new char[strlen(name)+1]();
		strcpy(mname,name);
		mprice = price;
		mamount = amount;
	}
 	~Goods()
	{
		std::cout<<this<<"~Goods::Goods()"<<std::endl;
		delete [] mname;
	}

private:
	char *mname;
	float mprice;
	int mamount;
};
 
int main()
{
	Goods goods1;
	Goods goods2("goods2",10.5);
	Goods goods3(20);
	Goods goods4("goods4",10.5,20);
	return 0;
}
 

析构函数不可以重载。

顺序:先构造的后析构,后构造的先析构。

构造函数:生成对象的一环(不能手动调用)

析构函数:依赖对象的地哦啊用(能手动调用)---->退化为普通函数的调用

③拷贝构造函数

用一个已存在的对象来生成一个相同类型的新对象。(默认:浅拷贝)

函数原型:

	Goods(const Goods& rhs)//拷贝构造函数
	{
		std::cout<<"Goods::Goods(const Goods& )"<<std::endl;
		mname = new char[strlen(rhs.mname)+1];
		strcpy(mname,rhs.mname);
		mprice = rhs.mprice;
		mamount = rhs.mamount;
	}

形参用引用,防止递归构造形参对象导致栈溢出问题。

拷贝构造函数的形参使用了:const Goods& rhs,而不使用Goods rhs或者Goods *rhs。

1、形参如果用Goods rhs 来传递,实参传递形参的过程重复在做用已存在对象来生成新对象的过程,又得调用拷贝构造函数,多次递归会导致栈溢出。

2、形参如果用Goods *rhs来传递实参与形参的值,就成了构造函数,而不是拷贝构造函数。

以上图片可以看出,调用拷贝构造函数时会出现两种情况:

1、对象内存在栈上,而资源内存(new char[strlen(name)+1]())在堆上

堆上的内存由系统管理,我们只要调用析构函数,释放资源即可(篮筐表示)

2、对象内存在堆上,资源内存也在堆上。

在遇到释放对象内存时,先要释放其资源(调析构函数),然后在销毁对象内存(delete + 对象名)即可(红框表示)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值