日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

基类使用私有数据_C++作业之多继承与虚基类

發布時間:2023/12/19 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基类使用私有数据_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++作业之多继承与虚基类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。