-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo32.cpp
59 lines (51 loc) · 1.02 KB
/
demo32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;
class Base
{
public:
Base():b1(0),b2(0)
{ cout << "Base 调用构造函数!" << b1 << ", " << b2 << endl; }
Base(int i, int j):b1(i), b2(j)
{ cout << "Base 调用构造函数!" << b1 << ", " << b2 << endl; }
~Base()
{ cout << "Base 调用析构函数!" << b1 << ", " << b2 << ", " << endl; }
void Print()
{ cout << b1 << ", " << b2 << ", "; }
private:
int b1, b2;
};
/*
派生类的析构规则:
1、对派生类新增普通成员进行清理
2、调用成员对象析构函数
3、调用基类析构函数
*/
class Derived : public Base
{
public:
Derived(int i, int j, int k);
~Derived();
void Print();
private:
int d;
};
Derived::Derived(int i, int j, int k): Base(i, j), d(k)
{
cout << "Derived 调用构造函数!" << d << endl;
}
Derived::~Derived()
{
cout << "Derived 调用析构函数!" << d << endl;
}
void Derived::Print()
{
Base::Print();
cout << d << endl;
}
int main()
{
Derived der1(1,2,3), der2(-4,-5,-6);
der1.Print();
der2.Print();
return 0;
}