概述
大一时写的,很菜
目录
实验目的:
实验任务:
实验步骤:
参考代码:
实验目的:
- 学习定义和使用类的继承关系,定义派生类;
- 熟悉不同继承方式下对基类成员的访问控制;
- 学习利用虚基类解决二义性问题。
实验任务:
- 定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。
- 定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。
- 定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类,在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题?编程试试看。
- (选做)从实验6中的people(人员)类派生出student(学生)类,添加属性:班号char classNO[7];从people类派生出teacher(教师)类,添加属性:职务char principalship[11]、部门char department[21]。从student类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser;从graduate类和teacher类派生出TA(助教生)类,注意虚基类的使用。重载相应的成员函数,测试这些类。类之间的关系如下图所示:
实验步骤:
- 编写程序定义基类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'- 编写程序定义一个基类BaseClass,构造其派生类DetixedClass,在构造函数和板构函数中用cout.输出提示信息,观察构造函数和析构函数的执行情况。程序名: lab7_2.cpp。
- 用debug 功能跟踪程序lab7_2的执行过程,观察基类和派生类的构造函数和板构函数
的执行情况。- 编写程序定义一个车(vehicle)基类,由此派生出自行车(bicycle)类、汽车(motorcar)类,注意把vehicle派生为虚基类。再从 bicycle和 motorcar派生出摩托车(motorcycle)类,在main()函数中测试这个类。程序名:lab7_3.cpp。
编译成功后,把vehicle设置为非虚基类,再编译一次,此时系统报错,无法编译成功。这是因为若不把vehicle设置为虚基类,会出现二义性错误,程序不能成功编译。
参考代码:
/*实验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;
}
//实验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;
}
/*实验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;
}
/*实验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——继承与派生所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复