概述
点击蓝字
关注我们
实验目的
1.掌握多重继承的使用。
2.理解虚基类的作用。
3.熟悉派生类对象与基类的转换。
1
实验要求
1.将代码和运行结果复制到word文档提交。
2.word文档命名格式:实验X-姓名-学号。
3.禁止抄袭。
4.按时提交。
2
实验内容
1.从Person类派生出学生类Student和教师类Teacher;从Student类中派生研究生类Graduate;从Graduate类和Teacher类派生出助教生类Assistant。根据类视图完成类的定义以及相应的构造函数,注意虚基类的使用。在main函数中创建类的对象测试这些类。(测试派生类对象与基类的转换,熟悉各对象的内存模型,可选做)
下面给出各个类的成员和继承关系。
1)Person类数据成员:
string name_; //姓名
Gender gender_; // 性别,枚举类型
Birthday birth_; //出生日期,类对象
2)Student类添加int类型成绩(score)成员
3)Teacher类添加string类型职称(title)成员
4)Graduate类添加string类型导师(advisor)成员
5)Assistant类添加string类型专业(subject)成员。
(
类图显示
3
代码
```
#include
using namespace std;
enum Gender {
Male, Female
};
class Birthday {
public:
Birthday(int day, int month, int year) : day_(day), month_(month), year_(year) {}
void Print() {
cout << "生日:" << day_ << "日 " << month_ << "月 " << year_ <
}
private:
int day_;
int month_;
int year_;
};
class Person {
public:
Person() :gender_(Male), name_("") {}
Person(const Birthday& birth, Gender gender, const string& name) : birth_(birth), gender_(gender), name_(name) {}
void Print() {
cout << "name:" << name_ << " sex";
if (gender_ == 0)
cout << " male" << " ";
else
cout << " female" << " ";
birth_.Print();
}
private:
Birthday birth_ = Birthday(0, 0, 0);
Gender gender_;
string name_;
};
class Student : virtual public Person {
public:
Student() :Person(), score_(0) {}
Student(const Birthday& birth, Gender gender, const string& name, int score) : Person(birth, gender, name),
score_(score) {}
void Print() {
Person::Print();
cout << " ";
PrintScore();
}
void PrintScore() {
cout << "score:" << score_;
}
private:
int score_;
};
class Teacher : virtual public Person {
public:
Teacher() :Person(), title_("") {}
Teacher(const Birthday& birth, Gender gender, const string& name, const string& title) : Person(birth, gender,
name),
title_(title) {}
void Print() {
Person::Print();
cout << " ";
PrintTitle();
}
void PrintTitle() {
cout << "职称:" << title_;
}
private:
string title_;
};
class Graduate : public Student {
public:
Graduate() :Student(), advisor_("") {}
Graduate(const string& name, Gender gender, const Birthday& birthday, int score, const string& advisor) : Student(
birthday, gender, name, score), advisor_(advisor), Person(birthday, gender, name) {}
void Print() {
Student::Print();
cout << " ";
PrintAdvisor();
}
void PrintAdvisor() {
cout << "导师:" << advisor_;
}
private:
string advisor_;
};
class Assistant : public Graduate, public Teacher {
public:
Assistant() :Graduate(), Teacher(), subject_("") {}
Assistant(const string& name, Gender gender, const Birthday& birthday, int score, const string& advisor,
const string& title, const string& subject)
: Graduate(name, gender, birthday, score, advisor), Teacher(birthday, gender, name, title), Person(birthday, gender, name),
subject_(subject) {}
void Print() {
Person::Print();
cout << " ";
Student::PrintScore();
cout << " ";
Graduate::PrintAdvisor();
cout << " ";
Teacher::PrintTitle();
cout << " ";
PrintSubject();
}
void PrintSubject() {
cout << "专业:" << subject_;
}
private:
string subject_;
};
int main() {
Person person(Birthday(3, 5, 3), Male, "Linux");
person.Print();
cout << endl;
Student student(Birthday(6, 3, 7), Male, "丹尼斯里奇", 99);
student.Print();
cout << endl;
Teacher teacher(Birthday(8, 2, 6), Male, "阿兰图灵", "码师");
teacher.Print();
cout << endl;
Graduate graduate("reww", Male, Birthday(9, 9, 9), 0, "cwj");
graduate.Print();
cout << endl;
Assistant assistant("ymj", Female, Birthday(10, 10, 10), 60, "马冬梅", "程序猿",
"计算机");
assistant.Print();
cout << endl;
}
```
扫码关注我们
2019软件工程
图文源自:一个神秘人
最后
以上就是拉长小馒头为你收集整理的基类使用私有数据_C++作业之多继承与虚基类的全部内容,希望文章能够帮你解决基类使用私有数据_C++作业之多继承与虚基类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复