C++ 多继承与虚基类 练习

实验目的

1.掌握多重继承的使用。
2.理解虚基类的作用。
3.熟悉派生类对象与基类的转换。

实验要求

1.将代码和运行结果复制到word文档提交。
2.word文档命名格式:实验X-姓名-学号。
3.禁止抄袭。
4.按时提交。

实验内容

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)成员。

代码与类图

在这里插入图片描述

#include<iostream>

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;
}

最后
我觉得这篇讲的简单易懂,完全不懂的同学有兴趣可以看看
C++ 多重继承、虚继承与虚基类

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值