创建对象时,系统会自动调用构造函数和析构函数

 1 // test_构造函数2.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "iostream"
 6 
 7 using namespace std;
 8 
 9 class stu
10 {
11 private:
12     int a;
13 public:
14     void set(int x)
15     {
16         a = x;
17     }
18     int get()
19     {
20         return a;
21     }
22     stu()
23     {
24         cout << "自动执行构造函数" << endl ;
25     }
26     ~stu()
27     {
28         cout << "自动执行析构函数" << endl ;
29     }
30 };
31 
32 
33 int main(int argc, char* argv[])
34 {
35     stu liu;    //这里只是创建了一个对象.没有任何动作.
36                 //但是执行以后,可以看出系统还是执行了stu() 和 ~stu() 这两个函数.
37                 //这个两个函数根本没有语句去调用. 所以看出这个是系统自动调用的.
38 
39     return 0;
40 }
原文地址:https://www.cnblogs.com/adalovelace/p/4019931.html