设计虚基类base,包含姓名、年龄及相关的成员函数。由他派生领导类leader,新增职务、部门及相关成员函数。在由base派生工程师类engineer,新增职称、专业及相关成员函数。最后由leader和engineer类共同派生出主任工程师chairman。请编程实现这几个类及他们之间的派生关系,并设计测试代码进行测试。
复制代码
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104/* 设计虚基类base,包含姓名、年龄及相关的成员函数。 由他派生领导类leader,新增职务、部门及相关成员函数。 在由base派生工程师类engineer,新增职称、专业及相关成员函数。 最后由leader和engineer类共同派生出主任工程师chairman。 请编程实现这几个类及他们之间的派生关系,并设计测试代码进行测试。 */ #include <iostream> #include <stdlib.h> #include <string> using namespace std; class base { protected: string name; int age; public: base(string name1, int age1); void show(); }; base::base(string name1, int age1) { name = name1; age = age1; } void base::show() { cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; } class leader :virtual public base //领导类 { protected: string post;//职务 string dept;//部门 public: leader(string name1, int age1, string post1, string dept1); void show(); }; leader::leader(string name1, int age1, string post1, string dept1) :base(name1, age1) { post = post1; dept = dept1; } void leader::show() { cout << "领导类:" << endl; base::show(); cout << "职务:" << post << endl; cout << "部门:" << dept << endl; } class engineer :virtual public base //工程师类 { protected: string title;//职称 string major;//专业 public: engineer(string name1, int age1, string title1, string major1); void show(); }; engineer::engineer(string name1, int age1, string title1, string major1) :base(name1, age1) { title=title1; major = major1; } void engineer::show() { cout << "工程师类:" << endl; base::show(); cout << "职称:" << title << endl; cout << "专业:" << major << endl; } class chairman :public leader,public engineer//主任工程师类 { public: chairman(string name1, int age1, string post1, string dept1, string title1, string major1); void show(); }; chairman::chairman(string name1, int age1, string post1, string dept1, string title1, string major1) :base(name1, age1), leader(name1, age1, post1, dept1), engineer(name1, age1, title1, major1) { } void chairman::show() { cout << "主任工程师类:" << endl; base::show(); cout << "职务:" << post << endl; cout << "部门:" << dept << endl; cout << "职称:" << title << endl; cout << "专业:" << major << endl; } int main() { leader a("张三", 30, "部门经理", "管理部"); engineer b("李四", 25, "初级工程师", "土木工程"); chairman c("王五",45, "总经理","产品部", "高级工程师", "建筑设计"); a.show(); cout << endl; b.show(); cout << endl; c.show(); system("PAUSE"); return 0; }
最后
以上就是沉默红牛最近收集整理的关于【C++练习】设计虚基类base,包含姓名、年龄及相关的成员函数的全部内容,更多相关【C++练习】设计虚基类base,包含姓名、年龄及相关内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复