实验9:Problem I: 学生干部虚基类
注意看提示语句:注意使用虚基类使Student_Cadre只包含一份从Person类继承来的成员。
课本中有类似的题目!
Home | Web Board | ProblemSet | Standing | Status | Statistics |
Problem I: 学生干部虚基类
Submit: 1296 Solved: 802
[Submit][Status][Web Board]
[Submit][Status][Web Board]
Problem I: 学生干部虚基类
Time Limit: 1 Sec Memory Limit: 2 MBSubmit: 1296 Solved: 802
[Submit][Status][Web Board]
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
HINT
Append Code
#include<iostream> using namespace std; class Person{ public: string name; int age; char sex; string address; string phone; }; class Student:virtual public Person{ public: string major; void setAddr(string newAdd){address=newAdd;} void setTel(string newPhone){phone=newPhone;} }; class Cadre:virtual public Person{ public: string post; void setAddr(string newAdd){address=newAdd;} void setTel(string newPhone){phone=newPhone;} }; class Student_Cadre:virtual public Student,virtual public Cadre{ public: double wage; void setAddr(string newAdd){address=newAdd;} ///void setTel(string newPhone){phone=newPhone;} void display(){cout<<"name:"<<name<<endl<<"age"<<age<<endl<<"sex:"<<sex<<endl<<"address:"<<address<<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;address=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; }
向代码最深处出发~!