类的构造和析构

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class Test
 6 {
 7 public:
 8     Test()
 9     {
10         a = 10;
11         p = new char[100];
12         strcpy(p, "abcdefg");
13         cout << "我是构造函数,被调用了"<<endl;
14     }
15     ~Test()
16     {
17         if (p!=NULL)
18         {
19             delete [] p;
20         }
21         cout << "我是析构函数,被调用了!"<<endl;
22     }
23     void print()
24     {
25         cout << a<<endl;
26         cout << p << endl;
27     }
28 private:
29     int a;
30     char *p;
31 protected:
32 };
33 
34 void objplay()
35 {
36     Test t1;
37     t1.print();
38     cout << "-----"<<endl;
39     Test t2;
40     t2.print();
41 }
42 int main11()
43 {
44     objplay();
45 
46     system("pause");
47     return 0;
48 }
原文地址:https://www.cnblogs.com/linst/p/4913473.html