-
Notifications
You must be signed in to change notification settings - Fork 0
/
多继承
84 lines (83 loc) · 1.24 KB
/
多继承
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
//多继承:
#include<iostream>
using namespace std;
//父类1:
class Base1
{
public:
int a;
public:
Base1()
{
cout << "父类1的无参构造" << endl;
}
Base1(int a)
{
this->a = a;
cout << "父类1的有参构造" << endl;
}
~Base1()
{
cout << "父类1的析构函数" << endl;
}
void fun1(void)
{
cout << "父类1的fun1函数" << endl;
}
};
//父类2:
class Base2
{
public:
int a;
public:
Base2()
{
cout << "父类2的无参构造" << endl;
}
Base2(int a)
{
this->a = a;
cout << "父类2的有参构造" << endl;
}
~Base2()
{
cout << "父类2的析构函数" << endl;
}
void fun1(void)
{
cout << "父类2的fun1函数" << endl;
}
};
//子类:
class Son:public Base1,public Base2
{
public:
int a;
public:
Son()
{
cout << "子函数的无参构造" << endl;
}
Son(int x, int y,int z) :Base1(x),Base2(y)
{
a = z;
cout << "子函数的有参构造" << endl;
}
~Son()
{
cout << "子函数的析构函数" << endl;
}
void fun1(void)
{
cout << "子类的fun1函数" << endl;
}
};
int main()
{
Son son1(1,2,3);
cout << son1.a << endl;
cout << son1.Base1::a << endl;
cout << son1.Base2::a << endl;
return 0;
}