在职研究生(多重继承)

【id:278】【16分】B. 在职研究生(多重继承)
时间限制
1s
内存限制
128MB
题目描述

1、建立如下的类继承结构:

1)定义一个人员类CPeople,其属性(保护类型)有:姓名、性别、年龄;

2)从CPeople类派生出学生类CStudent,添加属性:学号和入学成绩;

3)从CPeople类再派生出教师类CTeacher,添加属性:职务、部门;

4)从CStudent和CTeacher类共同派生出在职研究生类CGradOnWork,添加属性:研究方向、导师;

2、分别定义以上类的构造函数、输出函数print及其他函数(如需要)。

3、在主函数中定义各种类的对象,并测试之。


输入

第一行:姓名性别年龄

第二行:学号成绩

第三行:职务部门

第四行:研究方向导师


输出

第一行:People:

第二行及以后各行:格式见Sample


样例查看模式 
正常显示
查看格式
输入样例1 <-复制
wang-li m 23
2012100365 92.5
assistant computer
robot zhao-jun
输出样例1
People:
Name: wang-li
Sex: m
Age: 23

Student:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5

Teacher:
Name: wang-li
Sex: m
Age: 23
Position: assistant
Department: computer

GradOnWork:
Name: wang-li
Sex: m
Age: 23
No.: 2012100365
Score: 92.5
Position: assistant
Department: computer
Direction: robot
Tutor: zhao-jun

存在多重继承并且继承多个时,在编写最低类的构造函数的时候,需要使用最基类的构造函数进行初始化

在使用多重继承时,保险起见,为所有继承的类使用virtual

注意:这里是virtual,不是volatile

#include "iostream"

using namespace std;

// 1)定义一个人员类 CPeople,其属性(保护类型)有:姓名、性别、年龄;
class CPeople {
protected:
    string name;
    char type;
    int age;
public:
    CPeople() {}

    CPeople(string &name, char type, int age) : name(name), type(type), age(age) {}

    void print() {
        //People:
        // Name: wang-li
        // Sex: m
        // Age: 23
        cout << "People:" << endl;
        cout << "Name: " << name << endl;
        cout << "Sex: " << type << endl;
        cout << "Age: " << age << endl;
    }
};

// 2)从CPeople类派生出学生类 CStudent,添加属性:学号和入学成绩;
class CStudent : virtual public CPeople {
protected:
    string ID;
    double score;
public:

    CStudent() {}

    CStudent(string &name, char type, int age, string &id, double score) : CPeople(name, type, age), ID(id),
                                                                           score(score) {}

    void print() {
        //Student:
        // Name: wang-li
        // Sex: m
        // Age: 23
        // No.: 2012100365
        // Score: 92.5
        cout << "Student:" << endl;
        cout << "Name: " << name << endl;
        cout << "Sex: " << type << endl;
        cout << "Age: " << age << endl;
        cout << "No.: " << ID << endl;
        cout << "Score: " << score << endl;
    }
};

// 3)从CPeople类再派生出教师类 CTeacher,添加属性:职务、部门;
class CTeacher : virtual public CPeople {
protected:
    string work;
    string part;
public:


    CTeacher() {}

    CTeacher(string &name, char type, int age, string &work, string &part) : CPeople(name, type, age),
                                                                             work(work), part(part) {}

    void print() {
        //Teacher:
        // Name: wang-li
        // Sex: m
        // Age: 23
        // Position: assistant
        // Department: computer
        cout << "Teacher:" << endl;
        cout << "Name: " << name << endl;
        cout << "Sex: " << type << endl;
        cout << "Age: " << age << endl;
        cout << "Position: " << work << endl;
        cout << "Department: " << part << endl;
    }
};

// 4)从 CStudent 和 CTeacher类共同派生出在职研究生类 CGradOnWork,添加属性:研究方向、导师;
class CGradOnWork : public CStudent, public CTeacher {
public:
    string searchWay;
    string leader;


    CGradOnWork(string &name, char type, int age, string &id, double score,
                string &work, string &part, string &searchWay, string &leader) : CPeople(name, type, age),
                                                                                 CStudent(name, type, age,
                                                                                          id, score),
                                                                                 CTeacher(name, type,
                                                                                          age, work, part),
                                                                                 searchWay(searchWay),
                                                                                 leader(leader) {}

    void print() {
        //GradOnWork:
        // Name: wang-li
        // Sex: m
        // Age: 23
        // No.: 2012100365
        // Score: 92.5
        // Position: assistant
        // Department: computer
        // Direction: robot
        // Tutor: zhao-jun
        cout << "GradOnWork:" << endl;
        cout << "Name: " << name << endl;
        cout << "Sex: " << type << endl;
        cout << "Age: " << age << endl;
        cout << "No.: " << ID << endl;
        cout << "Score: " << score << endl;
        cout << "Position: " << work << endl;
        cout << "Department: " << part << endl;
        cout << "Direction: " << searchWay << endl;
        cout << "Tutor: " << leader << endl;
    }
};

int main() {
//第一行:姓名性别年龄
    string name;
    char type;
    int age;
    cin >> name >> type >> age;
    CPeople cPeople(name, type, age);
    cPeople.print();
    cout << endl;
// 第二行:学号成绩
    string ID;
    double score;
    cin >> ID >> score;
    CStudent cStudent(name, type, age, ID, score);
    cStudent.print();
    cout << endl;
// 第三行:职务部门
    string work;
    string part;
    cin >> work >> part;
    CTeacher cTeacher(name, type, age, work, part);
    cTeacher.print();
    cout << endl;
// 第四行:研究方向导师
    string searchWay;
    string leader;
    cin >> searchWay >> leader;
    CGradOnWork cGradOnWork(name, type, age, ID, score, work, part, searchWay, leader);
    cGradOnWork.print();

}

Guess you like

Origin blog.csdn.net/m0_62288512/article/details/131580566