C++析构函数在对象数组,动态指针中使用

一、c++中析构函数的主要作用:

1、完成对象被删除前的的一些清理工作;

2、在对象生存周期结束之后系统会自动调用析构函数,然后释放此对象的空间;

3、如果程序没有声明构造函数,系统会默认自动产生一个隐含的构造函数;

4、析构函数只能有一个,析构函数不能传递任何参数,没有返回类型。

5、使用构造函数和析构函数时,注意先后的使用顺序,注意调用时间和调用顺序,在一般情况下,调用析构函数的次序正好和调用析构函数的次序相反;【待会儿介绍另外的情况,在堆中程序员自己决定释放调用构造函数的时机】

二、实例说明:以Box类为例说明

1、构造函数重载:主要是理解无参和有参数的构造函数

   //1、对于一般函数,定义之后就会调用构造函数(无参数的构造函数)
	Box box1;   
	cout <<"The volume of box1 is :" << box1.volume()<<endl;
    //2、定义后就会调用构造函数,调用含参数的构造函数
	Box box2(15,12,13); 
	cout <<"The volume of box2 is :" << box2.volume()<<endl;

2、对象动态数组new在类中的使用:

// 3、 对于堆指针,定义后不会调用默认的构造函数
	Box *box3;

    // 4、 对于堆指针,定义后不会调用默认的构造函数,但是在初始化之后会调用构造函数(此时调用无参的构造函数)
	Box *box4= new Box; 
	cout <<"The volume of box4 is :" <<box4->volume()<<endl;
	

	// 5、 对于堆指针,定义后不会调用默认的构造函数,但是在初始化之后会调用构造函数(此时调用无参的构造函数)
	Box *box5= new Box(11,11,11); 
	cout <<"The volume of box5 is :" <<box5->volume()<<endl;

3、类的对象数组


	//6、定义一个数组
	Box box6[3]={ Box(3,3,3),
		          Box(4,4,4),
				  Box(5,5,5)
	             }; //调用默认的构造函数三次,同理析构函数也是三次
	cout <<"The volume of box6[0] is :" <<box6[0].volume()<<endl;
	cout <<"The volume of box6[1] is :" <<box6[1].volume()<<endl;
	cout <<"The volume of box6[2] is :" <<box6[2].volume()<<endl;

4、构造函数的析构顺序:(1)先析构堆中的内存,由程序员完成,而且是先释放的先调用析构函数,譬如:本例中先释放delete box5,会先调用析构函数(box5);(2)然后在析构数组(3个类成员)(3)按照先调用构造函数后析构的准则完成

//6、构造函数的使用与什么时候释放相关,先释放的先析构,后释放的后析构
	cout <<"\t先由程序员析构堆中内存:"<<endl;
	delete box5;
	delete box4;
	cout <<"\t再析构其它:"<<endl;

三、完整的程序代码如下:

#include "iostream"
using namespace std;

//构造函数的重载,析构函数不可以重载
class Box
{
public:   
	Box();//无参的构造函数
	Box(int length,int width,int height);//有参数的构造函数
	~Box();  //调用析构函数
	int volume(); //成员函数器求体积
private:
	int length;
	int width;
	int height;
};

Box::Box()
{
	length = 10;
	width =10;
	height =10;
	cout << "无参的构造函数被调用:Box()"<<endl; 

}
Box::Box(int length,int width,int height):length(length),width(width),height(height)    //有参数的构造函数,通过初始化列表赋值
{ cout << "含参的构造函数被调用:Box(int length,int width,int height)"<<endl; }

Box::~Box() //调用析构函数
{
   cout << "析构函数被调用:~Box():"<<"此时的volume: "<< this->volume()<<endl; 
}

int Box::volume()//成员函数器求体积
{
   return (height*width*length);
}

int main()
{
	//1、对于一般函数,定义之后就会调用构造函数(无参数的构造函数)
	Box box1;   
	cout <<"The volume of box1 is :" << box1.volume()<<endl;
    //2、定义后就会调用构造函数,调用含参数的构造函数
	Box box2(15,12,13); 
	cout <<"The volume of box2 is :" << box2.volume()<<endl;

	cout<<"\n*********************************************reference(引用)************************************\n"<<endl;

	// 3、 对于堆指针,定义后不会调用默认的构造函数
	Box *box3;

    // 4、 对于堆指针,定义后不会调用默认的构造函数,但是在初始化之后会调用构造函数(此时调用无参的构造函数)
	Box *box4= new Box; 
	cout <<"The volume of box4 is :" <<box4->volume()<<endl;
	

	// 5、 对于堆指针,定义后不会调用默认的构造函数,但是在初始化之后会调用构造函数(此时调用无参的构造函数)
	Box *box5= new Box(11,11,11); 
	cout <<"The volume of box5 is :" <<box5->volume()<<endl;

	cout<<"\n*********************************************array(数组)************************************\n"<<endl;

	//6、定义一个数组
	Box box6[3]={ Box(3,3,3),
		          Box(4,4,4),
				  Box(5,5,5)
	             }; //调用默认的构造函数三次,同理析构函数也是三次
	cout <<"The volume of box6[0] is :" <<box6[0].volume()<<endl;
	cout <<"The volume of box6[1] is :" <<box6[1].volume()<<endl;
	cout <<"The volume of box6[2] is :" <<box6[2].volume()<<endl;


    cout<<"\n*************************************注意析构函数(desconsructor)的调用顺序:**************************\n"<<endl;

	//6、构造函数的使用与什么时候释放相关,先释放的先析构,后释放的后析构
	cout <<"\t先由程序员析构堆中内存:"<<endl;
	delete box5;
	delete box4;
	cout <<"\t再析构其它:"<<endl;

	return 0;
}

运行结果如下:

猜你喜欢

转载自blog.csdn.net/haoaoweitt/article/details/81199157
今日推荐