虛基類——(1)定義人員類Person: 公有成員:姓名(Name); 保護成員:性別(Gender),年齡(Age); 構造函數和析構函數

題目描述:ios

(1)定義人員類Person:
公有成員:姓名(Name);
保護成員:性別(Gender),年齡(Age);  數據結構

構造函數和析構函數  函數

(2) 從人員類Person派生學生記錄類StudentRecord:
添加公有成員:學號(Number),班級(ClassName),
添加靜態公有成員:學生總人數(TotalCount);
添加保護成員:平均成績(Score);
實現構造函數和析構函數。
(3) 從人員類Person派生教師記錄類TeacherRecord:
添加公有成員:學院(CollegeName),系(DepartmentName);
添加保護成員:教齡(Year);
實現構造函數和析構函數。 spa

(4)從學生記錄類StudentRecord和教師記錄類TeacherRecord派生學生助教類TeachingAssistant:
添加公有成員:輔導課程(LectureName);
實現公有函數:顯示人員信息(Show),屏幕打印 姓名,性別,年齡,學號,班級,學生總人數,平均成績,學院,系,教齡,輔導課程。
實現構造函數和析構函數。爲檢驗類間結構設計是否正確,設計函數void SetName(String name)實現更改一名助教的姓名的功能。 設計

建立一個助教類的對象 code

助教
姓名 性別 年齡 學號 班級 平均成績 學院 系 教齡 輔導課程
鄭七 男 22 2010123 軟20101 89         信息 軟件    1 數據結構
顯示其信息。 對象

調用更改姓名的函數,更改其姓名爲「鄭八」,並再次顯示其信息。 blog

輸入

string

輸出

顯示構造的信息和更改前和更改後的助教信息 io

樣例輸入

 

樣例輸出

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;
}
相關文章
相關標籤/搜索