基类使用私有数据_C++作业之多继承与虚基类
點擊藍字
關注我們
實驗目的
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++作业之多继承与虚基类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 诛仙3神级首饰升级流程
- 下一篇: 个人计算机必备的外部设备是什么(edr是