Problem A: 学生干部虚基类

    xiaoxiao2022-07-12  151

    Problem A: 学生干部虚基类 Time Limit: 1 Sec Memory Limit: 2 MB Submit: 3145 Solved: 2127 [Submit][Status] Description

    基于Student(学生)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Student_Cadre(学生兼干部)。

    这两个基类均继承自Person类,包含姓名、年龄、性别、地址、电话等数据成员。在Student类中还包含数据成员major(专业),在Cadre类中还包含数据成员post(职务),

    在Student_Cadre类中还包含数据成员wages(工资)。

    注意使用虚基类使Student_Cadre只包含一份从Person类继承来的成员。

    Input

    学生干部的姓名,年龄,性别,专业,职位,地址,电话,薪水

    修改该学生干部的新地址,新电话

    Output

    学生干部的信息

    Sample Input

    wangli 23 f BeijingRoad 0532-61234567 software president 1534.2 Taidonglu 0532-90827651 0531-28766143

    Sample Output

    name:wangli age23 sex:f address:BeijingRoad tel:0532-61234567 major:software post:president wages:1534.2

    name:wangli age23 sex:f address:Taidonglu tel:0531-28766143 major:software post:president wages:1534.2

    #include <iostream> using namespace std; class Person { public: int age; string name; char sex; string add; string tel; Person(){} void setAddr(string add_) { add=add_; } void setTel(string tel_) { tel=tel_; } ~Person(){} }; class Student:virtual public Person { public: string major; Student(string name_,int age_,char sex_,string add_,string tel_,string major_):major(major_) { name=name_; age=age_; sex=sex_; add=add_; tel=tel_; } ~Student(){} }; class Cadre:virtual public Person { public: string post; Cadre(string name_,int age_,char sex_,string add_,string tel_,string post_):post(post_) { name=name_; age=age_; sex=sex_; add=add_; tel=tel_; } }; class Student_Cadre : public Student, public Cadre { public: double wages; Student_Cadre(string name_,int age_,char sex_,string add_,string tel_,string major_,string post_,double wages_):Student(name_,age_,sex_,add_,tel_,major_),Cadre(name_,age_,sex_,add_,tel_,post_),wages(wages_) { /*cout<<"name:"<<name<<endl; cout<<"age"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"address:"<<add<<endl; cout<<"tel:"<<tel<<endl; cout<<"major:"<<major<<endl; cout<<"post:"<<post<<endl; cout<<"wages:"<<wages<<endl;*/ } void display() { cout<<"name:"<<name<<endl; cout<<"age"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"address:"<<add<<endl; cout<<"tel:"<<tel<<endl; cout<<"major:"<<major<<endl; cout<<"post:"<<post<<endl; cout<<"wages:"<<wages<<endl; } //Student_Cadre(double wages_):wages(wages_){} ~Student_Cadre(){} }; int main( ) { string name, major, post, addr, tel; int age; char sex; float wage; cin>>name>>age>>sex>>addr>>tel>>major>>post>>wage; Student_Cadre st_ca(name, age, sex, addr, tel, major, post,wage); st_ca.display( ); cout<<endl; string newAddr, newTel1, newTel2; cin>>newAddr>>newTel1>>newTel2; st_ca.setAddr(newAddr); st_ca.Student::setTel(newTel1); st_ca.Cadre::setTel(newTel2); st_ca.display( ); return 0; }
    最新回复(0)