C++——构造函数&析构函数

本文深入探讨了C++中构造函数和析构函数的使用,包括普通构造函数、带参数的构造函数以及初始化列表的使用方法,同时介绍了析构函数在资源回收中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

类的构造函数

普通构造函数

带参数的构造函数

初始化列表来初始化字段

类的析构函数


类的构造函数

类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行

构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。

普通构造函数

#include <iostream>

using namespace std ;

class Line
{
public :
	void SetLenth(int len);

	int  GetLenth(void);

	Line();	// 这是构造函数

private:
	int Lenth ;
};


Line::Line(void)	
{
	cout << "类的对象被建立" << endl ;
}

int Line::GetLenth(void)
{
	return Lenth ;
}

void Line::SetLenth(int len)
{
	Lenth = len ;
}

void main()
{
	Line line ;
	line.SetLenth(2);
	cout << "Lenth is  " <<line.GetLenth()<< endl;

}

/* 输出结果
类的对象被建立
Lenth is  2
*/

带参数的构造函数

默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数。这样在创建对象时就会给对象赋初始值

#include <iostream>

using namespace std ;

class Line
{
public :
	void SetLenth(int len);

	int  GetLenth(void);

	Line(int len);	// 这是构造函数

private:
	int Lenth ;
};


Line::Line(int len)	
{
	Lenth = len ;
}

int Line::GetLenth(void)
{
	return Lenth ;
}

void Line::SetLenth(int len)
{
	Lenth = len ;
}

void main()
{
	Line line(5) ;
	cout << "Lenth is  " <<line.GetLenth()<< endl;

}

/* 输出结果是
Lenth is  5
*/

初始化列表来初始化字段

还可以使用初始化列表来初始化字段。假设有一个类 C,具有多个字段 X、Y、Z 等需要进行初始化,只需要在不同的字段使用逗号进行分隔。

#include <iostream>

using namespace std ;

class Line
{
public :
	void SetLenth(int len);

	int  GetLenth1(void);

	int  GetLenth2(void);

	int  GetLenth3(void);

	Line(int len1 , int len2 ,int len3);

private:
	int Lenth1 ;
	int Lenth2 ;
	int Lenth3 ;

};

// 使用初始化列表来初始化字段
Line::Line(int len1 , int len2 ,int len3): Lenth1(len1) ,Lenth2(len2) , Lenth3(len3)
{
	cout << "Object is being created "  << endl ;
}

int Line::GetLenth1(void)
{
	return Lenth1 ;
}

int Line::GetLenth2(void)
{
	return Lenth2 ;
}

int Line::GetLenth3(void)
{
	return Lenth3 ;
}


void main()
{
	Line line(2,3,6) ;

	cout << "Lenth1  is  " <<line.GetLenth1()<< endl;
	cout << "Lenth2  is  " <<line.GetLenth2()<< endl;
	cout << "Lenth3  is  " <<line.GetLenth3()<< endl;

}

/* 输出结果是
Object is being created
Lenth1  is  2
Lenth2  is  3
Lenth3  is  6
*/

类的析构函数

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

#include <iostream>

using namespace std ;

class Line
{
public :
	void SetLenth(int len);

	int  GetLenth1(void);

	int  GetLenth2(void);

	int  GetLenth3(void);

	Line(int len1 , int len2 ,int len3);	//  构造函数

	~Line();	// 析构函数

private:
	int Lenth1 ;
	int Lenth2 ;
	int Lenth3 ;

};

// 使用初始化列表来初始化字段
Line::Line(int len1 , int len2 ,int len3): Lenth1(len1) ,Lenth2(len2) , Lenth3(len3)
{
	cout << "Object is being created "  << endl ;
}

Line::~Line()
{
	cout << "Object is being deleted" << endl;
}
int Line::GetLenth1(void)
{
	return Lenth1 ;
}

int Line::GetLenth2(void)
{
	return Lenth2 ;
}

int Line::GetLenth3(void)
{
	return Lenth3 ;
}


void main()
{
	Line line(2,3,6) ;

	cout << "Lenth1  is  " <<line.GetLenth1()<< endl;
	cout << "Lenth2  is  " <<line.GetLenth2()<< endl;
	cout << "Lenth3  is  " <<line.GetLenth3()<< endl;

}

/* 输出结果是
Object is being created
Lenth1  is  2
Lenth2  is  3
Lenth3  is  6
Object is being deleted
*/

部分资料来源于菜鸟教程

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lucas_zgp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值