我是靠谱客的博主 勤劳火龙果,这篇文章主要介绍C++实验6——继承与派生,现在分享给大家,希望可以做个参考。

大一时写的,很菜

目录

实验目的:

实验任务:

实验步骤:

参考代码:


实验目的:

  1. 学习定义和使用类的继承关系,定义派生类;
  2. 熟悉不同继承方式下对基类成员的访问控制;
  3. 学习利用虚基类解决二义性问题。

实验任务:

  1. 定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。
  2. 定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。
  3. 定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类,在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题?编程试试看。
  4. (选做)从实验6中的people(人员)类派生出student(学生)类,添加属性:班号char classNO[7];从people类派生出teacher(教师)类,添加属性:职务char principalship[11]、部门char department[21]。从student类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser;从graduate类和teacher类派生出TA(助教生)类,注意虚基类的使用。重载相应的成员函数,测试这些类。类之间的关系如下图所示:

实验步骤:

  1. 编写程序定义基类Animal,成员变量age定义为私有的。构造派生类dog,在其成员函数 SetAge(int n)中直接对age赋值时,会出现类似以下的错误提示:
    error C2248 : 'age' : cannot access private member declared in class 'Animal'
    error C2248: 'age' : cannot access private member declared in class 'Animal'
    把age改为公有成员变量后重新编译就可以了。程序名: lab7_1.cpp。e'
  2. 编写程序定义一个基类BaseClass,构造其派生类DetixedClass,在构造函数和板构函数中用cout.输出提示信息,观察构造函数和析构函数的执行情况。程序名: lab7_2.cpp。
  3. 用debug 功能跟踪程序lab7_2的执行过程,观察基类和派生类的构造函数和板构函数
    的执行情况。
  4. 编写程序定义一个车(vehicle)基类,由此派生出自行车(bicycle)类、汽车(motorcar)类,注意把vehicle派生为虚基类。再从 bicycle和 motorcar派生出摩托车(motorcycle)类,在main()函数中测试这个类。程序名:lab7_3.cpp。
    编译成功后,把vehicle设置为非虚基类,再编译一次,此时系统报错,无法编译成功。这是因为若不把vehicle设置为虚基类,会出现二义性错误,程序不能成功编译。

参考代码:

复制代码
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
/*实验6.1 定义一个基类Animal,有私有整型成员变量age,构造其派生类dog, 在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。*/ /*age设为私有,会报错“Animal::age”: 无法访问 private 成员(在“Animal”类中声明)和 成员 "Animal::age" (已声明 所在行数: 6) 不可访问*/ /*#include<iostream> using namespace std; class Animal{ int age;//不加访问类型,默认为私有 }; class dog:public Animal{ public: void SetAge(int n){ age=n; } }; int main(){ dog d; d.SetAge(5); return 0; }*/ //设为公有,三种继承方式都不会报错 #include<iostream> using namespace std; class Animal { public: int age; }; /*class dog:private Animal{ public: void SetAge(int n){ age=n; } };*/ /*class dog:protected Animal{ public: void SetAge(int n){ age=n; } };*/ class dog :public Animal { public: void SetAge(int n) { age = n; } }; int main() { dog d; d.SetAge(5); return 0; }
复制代码
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
//实验6.2 定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。 #include<iostream> using namespace std; class BaseClass { int Number; public: BaseClass(int a) { Number = a; cout << "调用基类构造函数" << endl; } ~BaseClass() { cout << "调用基类析构函数" << endl; } }; class DerivedClass :public BaseClass { public: DerivedClass(int a, int b, int c) :BaseClass(a), var(b) { var0 = c; cout << "调用派生类构造函数" << endl; } ~DerivedClass() { cout << "调用派生类的析构函数" << endl; } private: int var, var0; }; int main() { DerivedClass d(4, 5, 6); return 0; }
复制代码
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
/*实验6.3 定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数, 由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性, 汽车(motorcar)类有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类, 在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题?编程试试看。*/ #include<iostream> using namespace std; class vehicle { double MaxSpeed; double Weight; public: vehicle(double m, double w) { MaxSpeed = m; Weight = w; cout << "调用vehicle构造函数" << endl; } void Run() { cout << "vehicle run" << endl; } void stop() { cout << "vehicle stop" << endl; } }; class bicycle :virtual public vehicle { //若不用虚基类,会报错:对Run stop venicle等访问不明确 double Height; public: bicycle(double m, double w,double h) :vehicle(m, w),Height(h) { cout << "调用bicycle构造函数" << endl; } }; class motorcar :virtual public vehicle { int SeatNum; public: motorcar(double m, double w,int s) :vehicle(m, w) ,SeatNum(s){ cout << "调用motorcar构造函数" << endl; } }; class motorcycle :public bicycle, public motorcar { public: motorcycle(double m, double w,double h,int s) :vehicle(m, w), bicycle(m, w,h), motorcar(m, w,s) { cout << "调用motorcycle构造函数" << endl; } }; int main() { motorcycle qq(70.5, 500.9,546.3,8); qq.Run(); qq.stop(); return 0; }

复制代码
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*实验6.4(选做)从实验6中的people(人员)类派生出student(学生)类,添加属性:班号char classNO[7]; 从people类派生出teacher(教师)类,添加属性:职务char principalship[11]、部门char department[21]。 从student类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser; 从graduate类和teacher类派生出TA(助教生)类,注意虚基类的使用。重载相应的成员函数,测试这些类。 类之间的关系如下图(在实验要求里)所示:*/ #include<iostream> #include<string> using namespace std; class date { int year; int month; int day; public: date() { } ~date() { } date(date& d); void setdate() { cout << "依次输入年月日" << endl; cin >> year >> month >> day; } void showdate() { cout << year << "年" << month << "月" << day << "日" << endl; } }; date::date(date& d) { year = d.year; month = d.month; day = d.day; } class people { public: people() { } ~people() { } people(people& p); void setpeople() { cout << "依次输入姓名编号性别(男1女2)身份证号" << endl; cin >> name >> number >> sex >> id; birthday.setdate(); } void showpeople() { cout << "姓名 :" << name << endl << "编号 :" << number << endl << "身份证号:" << id << endl; cout << "性别 :" << (sex == 1 ? "男" : "女") << endl; cout << "出生日期:"; birthday.showdate(); } private: string name; //name定义为string类型,若定义为char类型,输出时只输出第一个字母 long long int number; int sex; date birthday; long long int id; }; people::people(people& p) { name = p.name; number = p.number; sex = p.sex; birthday = p.birthday; id = p.id; } class student :virtual public people { public: student() { } ~student() { } student(student& s); void setstudent() { cout << "输入班级编号: "; cin >> classNO; //其实字符型数组输入应该用循环,但是本程序这样操作没有了长度限制,为什么? setpeople(); } void showstudent() { cout << "班级编号:" << classNO << endl; showpeople(); } private: char classNO[7]; }; student::student(student& s) { // classNO = s.classNO; //这样做会报错,下边的也是 int i; for (i = 0; i < 7; i++) { classNO[i] = s.classNO[i]; } } class teacher :virtual public people { public: teacher() { } ~teacher() { } teacher(teacher& t); void setteacher() { setpeople(); cout << "输入职务"; cin >> principalship; cout << "输入部门"; cin >> department; cout << endl; } void setteacher1() { cout << "输入职务"; cin >> principalship; cout << "输入部门"; cin >> department; cout << endl; } void showteacher() { showpeople(); cout << "职务:" << principalship << endl << "部门:" << department << endl; } void showteacher1() { cout << "职务:" << principalship << endl << "部门:" << department << endl; } private: char principalship[11]; char department[21]; }; teacher::teacher(teacher& t) { // principalship = t.principalship; // department = t.department; int i, j; for (i = 0; i < 11; i++) { principalship[i] = t.principalship[i]; } for (j = 0; j < 21; j++) { department[i] = t.department[i]; } } class graduate :public student { char subject[21]; teacher adviser; public: graduate() { } ~graduate() { } graduate(graduate& g); void setgraduate() { setstudent(); cout << "输入专业"; cin >> subject; adviser.setteacher1(); } void showgraduate() { showstudent(); cout << "专业:" << subject << endl; adviser.showteacher1(); } }; graduate::graduate(graduate& g) { // subject = g.subject; int i; for (i = 0; i < 21; i++) { subject[i] = g.subject[i]; } adviser = g.adviser; } class TA :public graduate, public teacher { public: TA() { } ~TA() { } TA(TA& ta); void setTA() { setgraduate(); } void showTA() { showgraduate(); } }; int main() { TA a; cout << "添加助教:n"; a.setTA(); a.showTA(); system("pause"); system("cls"); teacher b; cout << "添加教师:n"; b.setpeople(); b.showpeople(); system("pause"); system("cls"); student c; cout << "添加学生:n"; c.setstudent(); c.showstudent(); system("pause"); system("cls"); graduate d; cout << "添加研究生:" << endl; d.setgraduate(); d.showgraduate(); return 0; }

最后

以上就是勤劳火龙果最近收集整理的关于C++实验6——继承与派生的全部内容,更多相关C++实验6——继承与派生内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部