关于一道c++多继承的问题

定义以下类:

1个CPerson类,其属性有:姓名、性别和年龄。

2、从CPerson类派生出CStudent类,增加属性:学号、入学时间和入学成绩;

3、从CPerson类派生出CTeacher类,添加属性:职务、部门和工作时间;

4、由CStudent类派生出CGraduate类,添加属性:研究方向和导师;

5、由CGraduate和CTeacher共同派生出在职研究生类CGradonWork;

6、CDate为日期类。

每个类定义一个Print函数,输出其属性值,输出见样例。

主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

int main()
{
    CGradOnWork s1("crq",40, "Male", "teacher", "sxxy", CDate(2002,9,1),"15", CDate(2003, 9, 1), 400, "tm", "cs");
    s1.Print();
    s1.CTeacher::Print();
    s1.CGraduate::Print();
    s1.CStudent::Print();
    s1.CPerson::Print();
    return 0;
}

输入

输出
输出样例信息

样例输入
样例输出
crq 40 Male teacher sxxy 2002-9-1 15 2003-9-1 400 tm cs
crq 40 Male teacher sxxy 2002-9-1
crq 40 Male 15 2003-9-1 400 tm cs
crq 40 Male 15 2003-9-1 400
crq 40 Male

稍等

#include <iostream>
#include <string>
using namespace std;

class CPerson {
public:
    CPerson(string name, string gender, int age) : name(name), gender(gender), age(age) {}
    void Print() const {
        cout << "姓名:" << name << ",性别:" << gender << ",年龄:" << age << endl;
    }
protected:
    string name;
    string gender;
    int age;
};

class CStudent : public CPerson {
public:
    CStudent(string name, string gender, int age, string id, CDate entranceDate, int entranceScore)
            : CPerson(name, gender, age), id(id), entranceDate(entranceDate), entranceScore(entranceScore) {}
    void Print() const {
        CPerson::Print();
        cout << "学号:" << id << ",入学时间:" << entranceDate.ToString() << ",入学成绩:" << entranceScore << endl;
    }
protected:
    string id;
    CDate entranceDate;
    int entranceScore;
};

class CGraduate : public CStudent {
public:
    CGraduate(string name, string gender, int age, string id, CDate entranceDate, int entranceScore, string researchDirection, string tutor)
            : CStudent(name, gender, age, id, entranceDate, entranceScore), researchDirection(researchDirection), tutor(tutor) {}
    void Print() const {
        CStudent::Print();
        cout << "研究方向:" << researchDirection << ",导师:" << tutor << endl;
    }
protected:
    string researchDirection;
    string tutor;
};

class CTeacher : public CPerson {
public:
    CTeacher(string name, string gender, int age, string title, string department, CDate hireDate)
            : CPerson(name, gender, age), title(title), department(department), hireDate(hireDate) {}
    void Print() const {
        CPerson::Print();
        cout << "职务:" << title << ",部门:" << department << ",工作时间:" << hireDate.ToString() << endl;
    }
protected:
    string title;
    string department;
    CDate hireDate;
};

class CGradOnWork : public CGraduate, public CTeacher {
public:
    CGradOnWork(string name, int age, string gender, string title, string department, CDate hireDate, string id, CDate entranceDate, int entranceScore, string researchDirection, string tutor)
            : CGraduate(name, gender, age, id, entranceDate, entranceScore, researchDirection, tutor),
              CTeacher(name, gender, age, title, department, hireDate) {}
    void Print() const {
        CGraduate::Print();
        CTeacher::Print();
    }
};

class CDate {
public:
    CDate(int year, int month, int day) : year(year), month(month), day(day) {}
    string ToString() const {
        return to_string(year) + "年" + to_string(month) + "月" + to_string(day) + "日";
    }
protected:
    int year;
    int month;
    int day;
};

int main()
{
    CGradOnWork s1("crq",40, "Male", "teacher", "sxxy", CDate(2002,9,1),"15", CDate(2003, 9, 1), 400, "tm", "cs");
    s1.Print();
    s1.CTeacher::Print();
    s1.CGraduate::Print();
    s1.CStudent::Print();
    s1.CPerson::Print();
    return 0;
}
 

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
以下是完整的代码实现:

#include <iostream>
#include <string>
using namespace std;

class CDate {
public:
    CDate(int year, int month, int day) : year_(year), month_(month), day_(day) {}
    void Print() {
        cout << year_ << "-" << month_ << "-" << day_;
    }
private:
    int year_;
    int month_;
    int day_;
};

class CPerson {
public:
    CPerson(const string& name, const string& gender, int age)
        : name_(name), gender_(gender), age_(age) {}
    virtual void Print() {
        cout << name_ << " " << age_ << " " << gender_;
    }
private:
    string name_;
    string gender_;
    int age_;
};

class CStudent : public CPerson {
public:
    CStudent(const string& name, int age, const string& gender,
             const string& student_id, const CDate& enroll_date, int enroll_score)
        : CPerson(name, gender, age), student_id_(student_id), enroll_date_(enroll_date),
          enroll_score_(enroll_score) {}
    void Print() {
        CPerson::Print();
        cout << " " << student_id_ << " ";
        enroll_date_.Print();
        cout << " " << enroll_score_;
    }
private:
    string student_id_;
    CDate enroll_date_;
    int enroll_score_;
};

class CGraduate : public CStudent {
public:
    CGraduate(const string& name, int age, const string& gender,
              const string& student_id, const CDate& enroll_date, int enroll_score,
              const string& research_direction, const string& advisor)
        : CStudent(name, age, gender, student_id, enroll_date, enroll_score),
          research_direction_(research_direction), advisor_(advisor) {}
    void Print() {
        CStudent::Print();
        cout << " " << research_direction_ << " " << advisor_;
    }
private:
    string research_direction_;
    string advisor_;
};

class CTeacher : public CPerson {
public:
    CTeacher(const string& name, int age, const string& gender,
             const string& position, const string& department, const CDate& work_date)
        : CPerson(name, gender, age), position_(position), department_(department),
          work_date_(work_date) {}
    void Print() {
        CPerson::Print();
        cout << " " << position_ << " " << department_ << " ";
        work_date_.Print();
    }
private:
    string position_;
    string department_;
    CDate work_date_;
};

class CGradOnWork : public CGraduate, public CTeacher {
public:
    CGradOnWork(const string& name, int age, const string& gender,
                const string& position, const string& department, const CDate& work_date,
                const string& student_id, const CDate& enroll_date, int enroll_score,
                const string& research_direction, const string& advisor)
        : CGraduate(name, age, gender, student_id, enroll_date, enroll_score,
                    research_direction, advisor),
          CTeacher(name, age, gender, position, department, work_date) {}
    void Print() {
        CGraduate::Print();
        cout << " " << CTeacher::position_ << " " << CTeacher::department_ << " ";
        CTeacher::work_date_.Print();
        cout << endl;
    }
};

int main()
{
    CGradOnWork s1("crq", 40, "Male", "teacher", "sxxy", CDate(2002, 9, 1),
                   "15", CDate(2003, 9, 1), 400, "tm", "cs");
    s1.Print();
    s1.CTeacher::Print();
    s1.CGraduate::Print();
    s1.CStudent::Print();
    s1.CPerson::Print();
    return 0;
}

这里使用了多重继承的方式,将 CGraduateCTeacher 共同派生出 CGradOnWork 类。在 CGradOnWork 类中,我们使用了构造函数的初始化列表来初始化 CGraduateCTeacher 的基类,以及自己的数据成员。在 CGradOnWork 类中,我们定义了自己的 Print 函数,并在其中调用了基类的 Print 函数,以输出各个类的属性值。在 main 函数中,我们创建了 CGradOnWork 类的一个对象 s1,并分别调用各个类的 Print 函数,输出各个类的属性值。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢