虚基类——(1)定义人员类Person: 公有成员:姓名(Name); 保护成员:性别(Gender),年龄(Age); 构造函数和析构函数...

题目描述:

(1)定义人员类Person:
公有成员:姓名(Name);
保护成员:性别(Gender),年龄(Age); 

构造函数和析构函数 

(2) 从人员类Person派生学生记录类StudentRecord:
添加公有成员:学号(Number),班级(ClassName),
添加静态公有成员:学生总人数(TotalCount);
添加保护成员:平均成绩(Score);
实现构造函数和析构函数。
(3) 从人员类Person派生教师记录类TeacherRecord:
添加公有成员:学院(CollegeName),系(DepartmentName);
添加保护成员:教龄(Year);
实现构造函数和析构函数。 

(4)从学生记录类StudentRecord和教师记录类TeacherRecord派生学生助教类TeachingAssistant:
添加公有成员:辅导课程(LectureName);
实现公有函数:显示人员信息(Show),屏幕打印 姓名,性别,年龄,学号,班级,学生总人数,平均成绩,学院,系,教龄,辅导课程。
实现构造函数和析构函数。为检验类间结构设计是否正确,设计函数void SetName(String name)实现更改一名助教的姓名的功能。

创建一个助教类的对象

助教
姓名 性别 年龄 学号 班级 平均成绩 学院 系 教龄 辅导课程
郑七 男 22 2010123 软20101 89         信息 软件    1 数据结构
显示其信息。

调用更改姓名的函数,更改其姓名为“郑八”,并再次显示其信息。

输入

输出

显示构造的信息和更改前和更改后的助教信息

样例输入

 

样例输出

Person郑七constructed

Student郑七constructed

teacher郑七constructed

teachingassistant郑七constructed

Name:郑七 Gender:男 Age:22 Number:2010123 ClassName:软20101 TotalCount:1 Score:8

9 CollegeName:信息 DepartmentName:软件 Year:1 LectureName:数据结构

Name:郑八 Gender:男 Age:22 Number:2010123 ClassName:软20101 TotalCount:1 Score:8

9 CollegeName:信息 DepartmentName:软件 Year:1 LectureName:数据结构

teachingassistant郑八destructed

teacher郑八destructed

Student郑八destructed

Person郑八destructed

提示

各类的构造函数和析构函数都有输出。

调用公有函数Show,以分别显示各个记录的人员信息。

在派生助教类时,使用虚基类。

 

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

class Person
{
protected:
    string Gender;
    int Age;
public:
    string Name;
    Person(string _Name="郑七",string _Gender="",int _Age=22)
    {
        Name=_Name;
        Gender=_Gender;
        Age=_Age;
        cout<<"Person"<<Name<<"constructed"<<endl;
    }
    ~Person()
    {
        cout<<"Person"<<Name<<"destructed"<<endl;
    }
};
class StudentRecord:virtual public Person
{
public:
    int Number;
    string ClassName;
    int TotalCount;
protected:
    int Score;
    StudentRecord(string _Name="郑七",string _Gender="",int _Age=22,int _Number=2010123,string _ClassName="软20101",int _TotalCount=1,int _Score=89):Person(_Name,_Gender,_Age),TotalCount(_TotalCount)
    {
        Number=_Number;
        ClassName=_ClassName;
        Score=_Score;
        cout<<"Student"<<Name<<"constructed"<<endl;
    }
    ~StudentRecord()
    {
        cout<<"Student"<<Name<<"destructed"<<endl;
    }
};
class TeacherRecord :virtual public Person
{
public:
    string CollegeName;
    string DepartmentName;
protected:
    int Year;
    TeacherRecord(string _Name="郑七",string _Gender="",int _Age=22,int _Number=2010123,string _CollegeName="信息",string _DepartmentName="软件",int _Year=1):Person(_Name,_Gender,_Age)
    {
        {
            CollegeName=_CollegeName;
            DepartmentName=_DepartmentName;
            Year=_Year;
            cout<<"teacher"<<Name<<"constructed"<<endl;
        }

    }
    ~TeacherRecord()
    {
        cout<<"teacher"<<Name<<"destructed"<<endl;
    }
};

class TeachingAssistant:virtual public StudentRecord,virtual public TeacherRecord
{
public:
    string LectureName;
    TeachingAssistant(string _LectureName="数据结构",string _Name="郑七",string _Gender="",int _Age=22,int _Number=2010123,string _CollegeName="信息",string _DepartmentName="软件",int _Year=1,string _ClassName="软20101",int _TotalCount=1,int _Score=89):StudentRecord(_Name, _Gender, _Age, _Number, _ClassName, _TotalCount, _Score),TeacherRecord(_Name,_Gender,_Age,_Number,_CollegeName,_DepartmentName,_Year)
    {
        LectureName=_LectureName;
        cout<<"teachingassistant"<<Name<<"constructed"<<endl;
    }
    void Print()
    {
        cout<<"Name:"<<Name<<" ";
        cout<<"Gender:"<<Gender<<" ";
        cout<<"Age:"<<Age<<" ";
        cout<<"Number:"<<Number<<" ";
        cout<<"ClassName:"<<ClassName<<" ";
        cout<<"TotalCount:"<<TotalCount<<" ";
        cout<<"Score:"<<Score<<" ";
        cout<<"CollegeName:"<<CollegeName<<" ";
        cout<<"DepartmentName:"<<DepartmentName<<" ";
        cout<<"Year:"<<Year<<" ";
        cout<<"LectureName:"<<LectureName<<endl;
    }
    void SetName(string _Name)
    {
        Name=_Name;
    }
    ~TeachingAssistant()
    {
        cout<<"teachingassistant"<<Name<<"destructed"<<endl;
    }
};

int main()
{
    TeachingAssistant a1;
    a1.Print();
    a1.SetName("郑八");
    a1.Print();
    return 0;
}

 

转载于:https://www.cnblogs.com/handsometaoa/p/11021261.html

    原文作者:ajcv23429
    原文地址: https://blog.csdn.net/ajcv23429/article/details/101129518
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞