【C++】从入门到精通第二弹——类的构造与析构函数

这里写目录标题

写在最前面的话
——构造函数和析构函数是两个特殊的成员函数,都没有返回值,构造函数名和类名相同,析构函数名只是在类名前加上 ~
构造函数主要用来在创建对象时给对象中的数据成员赋值,主要目的是初始化对象,
析构函数的功能与构造函数正好相反,析构函数是用来释放对象的,再删出对象前,对对象进行清理工作。

类的构造函数

当建立一个类的对象时,构造函数就会默认被调用,如果用户不提供构造函数,编译器就会自动实现一个空的构造函数。所以构造函数的主要作用就是完成某些初始化的工作,一般来说,比如设置类成员属性操作,对成员数据赋值等

构造函数类内实现

  1 #include<iostream>
  2 using namespace std;
  3 class Person
  4 {
    
    
  5     public:
  6         Person(int age, int height)
  7         {
    
    
  8             this->age = age;
  9             this->height = height;
 10         };
 11         void show()
 12         {
    
    
 13             cout<<"age = "<<age<<endl;
 14             cout<<"height = "<<height<<endl;
 15         }
 16     private:
 17         int age;
 18         int height;
 19 
 20 
 21 };
 22 
 23 
 24 int main()
 25 {
    
    
 26     Person p(45,123);
 27     p.show();
 28     
 29     return 0;
 30 }
~      

类外实现:

  1 #include<iostream>
  2 using namespace std;
  3 class Person
  4 {
    
    
  5     public:
  6 /*        Person(int age, int height)
  7         {
  8             this->age = age;
  9             this->height = height;
 10         };*/
 11         Person(int age,int height);//注意一定要在类内先声明
 12         void show()
 13         {
    
    
 14             cout<<"age = "<<age<<endl;
 15             cout<<"height = "<<height<<endl;
 16         }
 17     private:
 18         int age;
 19         int height;
 20 
 21 
 22 };
 23 Person::Person(int age,int height)
 24 {
    
    
 25 
 26      this->age = age;
 27      this->height = height;
 28 }
 29 
 30 
 31 int main()
 32 {
    
    
 33     Person p(45,123);
 34     p.show();
 35 
 36     return 0;
 37 }

复制构造函数

  1 #include<iostream>
  2 using namespace std;
  3 class Person
  4 {
    
    
  5     public:
  6 /*        Person(int age, int height)
  7         {
  8             this->age = age;
  9             this->height = height;
 10         };*/
 11         Person(int age,int height);//注意一定要在类内先声明
 			Person::Person(Person &pp);//复制构造函数
 12         void show()
 13         {
    
    
 14             cout<<"age = "<<age<<endl;
 15             cout<<"height = "<<height<<endl;
 16         }
 17     private:
 18         int age;
 19         int height;
 20 
 21 
 22 };
 23 Person::Person(int age,int height)
 24 {
    
    
 25 
 26      this->age = age;
 27      this->height = height;
 28 }
 29 Person::Person(Person &pp)
 30 {
    
    
      this->age = pp.age;
 	  this->height = pp.height;	
 
 	}
 int main()
  {
    
    
      Person p(45,123);
      p.show();
  	  Person pp(p);
  	  pp.show();
      return 0;
  }

注意:如果当用户忘记显示声明的默认构造函数,会导致链接出错,所以当我们使用自定义的默认构造函数时必须能实现,哪怕是空实现。

类的析构函数

  1 #include<iostream>
  2 #include<string.h>
  3 using namespace std;
  4 class Person
  5 {
    
    
  6     public:
  7         Person();
  8         ~Person();
  9         void show();
 10         char* message;
 11 };
 12 Person::Person()
 13 {
    
    
 14 
 15     message = new char[1024];
 16 }
 17 void Person::show()
 18 {
    
    
 19     strcpy(message,"fsvfg");
 20 
 21     cout<<"message = "<<message<<endl;
 22 }
 23 Person::~Person()
 24 {
    
    
 25     delete []message;
 26 }
 27 int main()
 28 {
    
    
 29     Person p;
 30     p.show();
 31 
 32     return 0;
 33 }
 34 

注意:
一个类中只能出现一个析构函数
析构函数不能发生重载
不管是构造函数还是析构函数都不能使用return语句,没有返回值

在不同的环境下构造函数和析构函数调用规则如下:

自动变量的作用域是某个模块,当此模块被激活时,调用构造函数,当退出此模块时,调用析构函数。

全局变量在进入main函数之前调用构造函数,在程序终止时调用析构函数。

动态分配的对象在使用new为对象分配内存时调用构造函数,使用delete删除对象时调用析构函数。

临时变量是编译器为支持计算自动产生的,临时变量生存期的开始和结束点会调用构造函数和析构函数。

猜你喜欢

转载自blog.csdn.net/2202_75623950/article/details/134275244