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

发布时间:2020-07-05 23:11:03 作者:liam2199
来源:网络 阅读:1489

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

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

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

    构造函数的作用初始化。

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

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

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

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

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

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

 

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 <iostream> 
  4. using namespace std; 
  5.  
  6. Test::Test() 
  7.     num_ = 0; 
  8.     cout << "Initializing Default " << endl; 

main.cpp

 

  1. # include <iostream> 
  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 <iostream> 
  4. using namespace std; 
  5.  
  6. Test::Test(int num) 
  7.     num_ = num; 
  8.     cout << "Initializing " << num_  << endl; 

main.cpp

 

  1. # include <iostream> 
  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 <iostream> 
  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 <iostream> 
  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 <iostream> 
  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 "<<num_ << endl; 
  14.  
  15. Test::~Test() 
  16.     cout << "Destory " << num_ << endl; 

main.cpp

  1. # include <iostream> 
  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 <iostream> 
  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 "<<num_ << endl; 
  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 <iostream> 
  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 "<<num_ << endl; 
  14.  
  15. Test::~Test() 
  16.     cout << "Destory " << num_ << endl; 

 

main.cpp

  1. //main.cpp 
  2. # include <iostream> 
  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; 

 

推荐阅读:
  1. 构造函数和析构函数
  2. C++构造函数和析构函数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++析构函数 显式调用析构函数 默认构造函数

上一篇:H3C R4900 G2服务器通过HDM安装系统

下一篇:AWK学习笔记

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》