創建對象時,系統會自動調用構造函數和析構函數


 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM