-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo31.cpp
86 lines (74 loc) · 1.4 KB
/
demo31.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;
class Base1
{
public:
Base1(int i): b1(i)
{ cout << "Base1 构造函数被调用!" << endl; }
void print() const
{ cout << "b1 -> " << b1 << endl; }
private:
int b1;
};
class Base2
{
public:
Base2(int i): b2(i)
{ cout << "Base2 构造函数被调用!" << endl; }
void print() const
{ cout << "b2 -> " << b2 << endl; }
private:
int b2;
};
class Base3
{
public:
Base3(): b3(0)
{ cout << "Base3 构造函数被调用!" << endl; }
void print() const
{ cout << "b3 -> " << b3 << endl; }
private:
int b3;
};
class Member
{
public:
Member(int i): m(i)
{ cout << "Member 的构造函数被调用!" << endl; }
int GetM()
{ return m; }
private:
int m;
};
/*
派生类构造规则:
1、先执行基类的构造函数
2、再执行成员对象的构造函数
3、最后执行派生类自己的构造函数
*/
// 执行基类的构造函数时,按照定义时基类的列出顺序进行构造
class Derived : public Base1, public Base2, public Base3
{
public:
Derived(int i, int j, int k, int l);
void print();
private:
int d;
Member mem;
};
Derived::Derived(int i, int j, int k, int l): Base1(i), Base2(j), mem(k)
{
d = l;
cout << "Derived 的构造函数被调用." << endl;
}
void Derived::print()
{
cout << "d -> " << d << endl;
cout << "memeber 's m -> " << mem.GetM() << endl;
}
int main()
{
Derived der(1,2,3,4);
der.print();
return 0;
}