Problem C: 学生干部虚基类
Time Limit: 1 Sec Memory Limit: 2 MBSubmit: 1739 Solved: 1138
[ Submit][ Status][ Web Board]
Description
基于Student(学生)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Student_Cadre(学生兼干部)。
这两个基类均继承自Person类,包含姓名、年龄、性别、地址、电话等数据成员。在Student类中还包含数据成员major(专业),在Cadre类中还包含数据成员post(职务),
在Student_Cadre类中还包含数据成员wages(工资)。
注意使用虚基类使Student_Cadre只包含一份从Person类继承来的成员。
Input
学生干部的姓名,年龄,性别,专业,职位,地址,电话,薪水
修改该学生干部的新地址,新电话
Output
学生干部的信息
Sample Input
wangli23fBeijingRoad0532-61234567softwarepresident1534.2Taidonglu0532-908276510531-28766143
Sample Output
name:wangliage23sex:faddress:BeijingRoadtel:0532-61234567major:softwarepost:presidentwages:1534.2name:wangliage23sex:faddress:Taidonglutel:0531-28766143major:softwarepost:presidentwages:1534.2
HINT
Append Code
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
class Person
{
public:
string name;
int age;
char sex;
string addr;
string phone;
};
class Student :virtual public Person
{
public:
string major;
void setAddr(string _addr){addr = _addr;}
void setTel(string _phone){phone = _phone;}
} ;
class Cadre:virtual public Person
{
public:
string post;
void setAddr(string _addr){addr = _addr;}
void setTel(string _phone){phone = _phone;}
};
class Student_Cadre : virtual public Student,virtual public Cadre
{
public:
double wage;
void setAddr(string newAdd){addr=newAdd;}
void display()
{
cout<<"name:"<<name<<endl<<"age"<<age<<endl<<"sex:"<<sex<<endl<<"address:"<<addr<<endl<<"tel:"<<phone<<endl<<"major:"<<major<<endl<<"post:"<<post<<endl<<"wages:"<<wage<<endl;
}
Student_Cadre(string n,int a,char s,string add,string tel,string m,string pos,double w)
{name=n;age=a;sex=s;addr=add;phone=tel;major=m;post=pos;wage=w;}
};
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;
}