资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++构造函数和析构函数的学习(一)

    构造函数是类中特殊的成员函数。

创新互联主要企业基础官网建设,电商平台建设,移动手机平台,微信小程序开发等一系列专为中小企业按需定制产品体系;应对中小企业在互联网运营的各种问题,为中小企业在互联网的运营中保驾护航。

    创建类类型的新对象的,系统会自动调用构造函数。

    构造函数的调用是为了保证每个数据成员都能被正确初始化。

    构造函数的作用初始化。

    通常情况下,构造函数应声明为公有函数,构造它不能像其他成员函数那样被显式的调用。

    构造函数被声明为私有有特殊的用途。

    构造函数可以有任意类型和任意个数的参数,一个类可以有多个构造函数。

    如果程序未声明构造函数,默认会生成一个空的构造函数。

    不带参数的构造函数称为默认构造函数。

    如果有一个构造函数,系统不再生成默认构造函数

 

Test.h

  1. //Test.h 
  2. # ifndef _TEST_H_ 
  3. # define _TEST_H_ 
  4.  
  5. class Test 
  6. public: //如果程序未声明构造函数,默认会生成一个空的构造函数
  7.     Test(); 
  8. private: 
  9.     int num_; 
  10. }; 
  11.  
  12. # endif //_TEST_H_ 

Test.cpp

  1. //Test.cpp 
  2. # include "Test.h" 
  3. # include  
  4. using namespace std; 
  5.  
  6. Test::Test() 
  7.     num_ = 0; 
  8.     cout << "Initializing Default " << endl; 

main.cpp

 

  1. # include  
  2. # include "Test.h" 
  3. using namespace std; 
  4.  
  5. int main(void) 
  6. { //自动调用构造函数
  7.     Test t; 
  8.  
  9.     return 0; 

 运行结果:

C++构造函数和析构函数的学习(一)

 //    如果有一个构造函数,系统不再生成默认构造函数

 

Test.h

  1. //Test.h 
  2. # ifndef _TEST_H_ 
  3. # define _TEST_H_ 
  4.  
  5. class Test 
  6. public: //如果程序未声明构造函数,默认会生成一个空的构造函数
  7.     Test(int num); 
  8. private: 
  9.     int num_; 
  10. }; 
  11.  
  12. # endif //_TEST_H_ 

Test.cpp

  1. //Test.cpp 
  2. # include "Test.h" 
  3. # include  
  4. using namespace std; 
  5.  
  6. Test::Test(int num) 
  7.     num_ = num; 
  8.     cout << "Initializing " << num_ << endl; 

main.cpp

 

  1. # include  
  2. # include "Test.h" 
  3. using namespace std; 
  4.  
  5. int main(void) 
  6. { //自动调用构造函数
  7.     Test t(10); 
  8.  
  9.     return 0; 

 运行结果:

C++构造函数和析构函数的学习(一)

 

 

构造函数重载的实例:

 

Test.h

  1. //Test.h 
  2. # ifndef _TEST_H_ 
  3. # define _TEST_H_ 
  4.  
  5. class Test 
  6. public: //如果程序未声明构造函数,默认会生成一个空的构造函数
  7. Test();
  8.     Test(int num); 
  9. private: 
  10.     int num_; 
  11. }; 
  12.  
  13. # endif //_TEST_H_ 

Test.cpp

  1. //Test.cpp 
  2. # include "Test.h" 
  3. # include  
  4. using namespace std; 
  5.  
  6. Test::Test()
  7. {
  8. num_ = 0;
  9.     cout << "Initializing default " << endl; 
  10.  
  11. Test::Test(int num) 
  12.     num_ = num; 
  13.     cout << "Initializing " << num_ << endl; 

main.cpp

  1. # include  
  2. # include "Test.h" 
  3. using namespace std; 
  4.  
  5. int main(void) 
  6. { //自动调用构造函数
  7.     Test t1;
  8. Test t2(10); 
  9.  
  10.     return 0; 

 运行结果:

C++构造函数和析构函数的学习(一)

 

 构造函数与new运算符

  1. //构造函数与new运算符 
  2.  
  3. # ifndef _TEST_H_ 
  4. # define _TEST_H_ 
  5.  
  6. class Test 
  7. public: 
  8.     ////可以显式的写一个默认构造函数,这样两个函数就成了重载 
  9.     Test(); 
  10.     Test(int num); 
  11.     //析构函数不能重载 
  12.     //析构函数不能带参数,如果带参数就可以重载 
  13.     ~Test(); 
  14.     void Display(); 
  15. private: 
  16.     int num_; 
  17. }; 
  18.  
  19. # endif  

Test.cpp

  1. # include "Test.h" 
  2. # include  
  3. using namespace std; 
  4.  
  5. void Test::Display() 
  6.     cout << num_ << endl; 
  7. Test::Test() 
  8.     num_ = 0; 
  9.     cout<<"Initializing Default" << endl; 
  10.      
  11. Test::Test(int num) 
  12.     num_ = num; 
  13.     cout<<"Initializing "<
  14.  
  15. Test::~Test() 
  16.     cout << "Destory " << num_ << endl; 

main.cpp

  1. # include  
  2. # include "Test.h" 
  3. using namespace std; 
  4.  
  5. int main(void) 
  6.     Test t; 
  7.     t.Display();  
  8.  
  9.     Test t2(20);         
  10.     t2.Display();  
  11. //不仅仅分配了内存,还调用了构造函数 
  12.     Test * t3 = new Test(20);  //new operator 
  13.     t3->Display(); 
  14. //不仅仅释放了内存,也调用了析构函数      
  15.     delete t3; 
  16.  
  17.     return 0; 

 

运行结果:

C++构造函数和析构函数的学习(一)

 

//全局对象的构造先于main函数 

 

  1.  
  2. # ifndef _TEST_H_ 
  3. # define _TEST_H_ 
  4.  
  5. class Test 
  6. public: 
  7.     ////可以显式的写一个默认构造函数,这样两个函数就成了重载 
  8.     Test(); 
  9.     Test(int num); 
  10.     //析构函数不能重载 
  11.     //析构函数不能带参数,如果带参数就可以重载 
  12.     ~Test(); 
  13.     void Display(); 
  14. private: 
  15.     int num_; 
  16. }; 
  17.  
  18. # endif  

Test.cpp

  1. # include "Test.h" 
  2. # include  
  3. using namespace std; 
  4.  
  5. void Test::Display() 
  6.     cout << num_ << endl; 
  7. Test::Test() 
  8.     num_ = 0; 
  9.     cout<<"Initializing Default" << endl; 
  10.      
  11. Test::Test(int num) 
  12.     num_ = num; 
  13.     cout<<"Initializing "<
  14.  
  15. Test::~Test() 
  16.     cout << "Destory " << num_ << endl; 

 

main.cpp

  1. # include "Test.h" 
  2. using namespace std; 
  3.  
  4. //全局对象的构造先于main函数 
  5. Test t(10); 
  6.  
  7. int main(void) 
  8.     cout << "Entering main ..." << endl; 
  9.     cout << "Exiting  main ..." << endl; 
  10.  
  11.     return 0; 

运行结果:

C++构造函数和析构函数的学习(一)

 

 

    默认析构函数是一个空函数

    析构函数没有参数

    析构函数不能被重载

 

    析构函数与数组

Test.h

  1. //Test.h 
  2. # ifndef _TEST_H_ 
  3. # define _TEST_H_ 
  4.  
  5. class Test 
  6. public: 
  7.     ////可以显式的写一个默认构造函数,这样两个函数就成了重载 
  8.     Test(); 
  9.     Test(int num); 
  10.     //析构函数不能重载 
  11.     //析构函数不能带参数,如果带参数就可以重载 
  12.     ~Test(); 
  13.     void Display(); 
  14. private: 
  15.     int num_; 
  16. }; 
  17.  
  18. # endif //_TEST_H_ 

Test.cpp

  1. //Test.cpp 
  2. # include  
  3. # include "Test.h" 
  4. using namespace std; 
  5.  
  6. void Test::Display() 
  7.     cout << num_ << endl; 
  8. Test::Test() 
  9.     num_ = 0; 
  10.     cout<<"Initializing Deafule " << endl; 
  11. Test::Test(int num) 
  12.     num_ = num; 
  13.     cout<<"Initializing "<
  14.  
  15. Test::~Test() 
  16.     cout << "Destory " << num_ << endl; 

 

main.cpp

  1. //main.cpp 
  2. # include  
  3. # include "Test.h" 
  4. using namespace std; 
  5.  
  6. int main(void) 
  7. {      //定义对象数组 
  8.     Test t[2] = {10,20}; 
  9.     //动态对象 
  10.     Test* t2 = new Test(2); //传递参数2 
  11.     delete t2; 
  12.     //没有传递任何参数,创建了两个对象 
  13.     Test* t3 = new Test[2]; 
  14.     delete[] t3; 
  15.      
  16.     return 0; 

运行结果:

C++构造函数和析构函数的学习(一)

    析构函数不能重载

 

    析构函数不能带参数,如果带参数就可以重载

 

    析构函数可以显式调用,一般很少用到,会实现一些特殊的效果:

  1. //main.cpp 
  2. # include "Test.h" 
  3.  
  4. int main(void) 
  5.     Test t; 
  6.     t.~Test(); //析构函数被调用了两次 
  7.     //析构函数很少调用,可以显式调用 
  8.     return 0; 

 


网页题目:C++构造函数和析构函数的学习(一)
网页网址:http://cdkjz.cn/article/jcpogi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220