如下图所示,构造一个Staff员工类为虚基类含有数据成员姓名、年龄、性别、地址、电话,派生出教师类Teacher增加数据成员职称和干部类Cader增加数据成员职务,派生出教师干部类Teacher_Cader增加数据成员工资,要求:
(1)在类中设计void input()和void output()函数实现数据的录入和输出。
(2)设计主程序显示教师干部信息。
已有部分代码,请按题修改一下
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string name1,int age1,string sex1,string title1,string addr1,string tel1)
{
name=name1;
age=age1;
sex=sex1;
title=title1;
addr=addr1;
tel=tel1;
}
void display();
protected:
string name;
int age;
string sex;
string title,addr,tel;
};
void Teacher::display()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"职称:"<<title<<endl;
cout<<"地址:"<<addr<<endl;
cout<<"电话:"<<tel<<endl;
}
class Cadre
{
public:
Cadre(string name1,int age1,string sex1,string post1,string addr1,string tel1);
void display();
protected:
string name;
int age;
string sex;
string post;
string addr;
string tel;
};
Cadre::Cadre(string name1,int age1,string sex1,string post1,string addr1,string tel1):name(name1),age(age1),sex(sex1),post(post1),addr(addr1),tel(tel1)
{
}
void Cadre::display()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"职务:"<<post<<endl;
cout<<"地址:"<<addr<<endl;
cout<<"电话:"<<tel<<endl;
}
class Person:public Teacher,public Cadre
{
public:
Person(string name1,int age1,string sex1,string title1,string post1,string address1,string tel1,float wages1);
void show()
{
Teacher::display();
cout<<"职务:"<<Cadre::post<<endl;
cout<<"工资:"<<wages<<endl;
}
protected:
float wages;
};
Person::Person(string name1,int age1,string sex1,string title1,string post1,string address1,string tel1,float wages1):Teacher(name1,age1,sex1,title1,address1,tel1),Cadre(name1,age1,sex1,post1,address1,tel1),wages(wages1)
{
}
int main()
{
Person person1("小明",18,"男","教授","部长","资阳","18080594922",950000);
person1.show();
system("pause");
return 0;
}
代码如下:
#include<iostream>
#include<string>
using namespace std;
class Staff
{
protected:
string name; //姓名
int age; //年龄
string sex; //性别
string addr;
string tel;
public:
void input()
{
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入性别:";
cin >> sex;
cout << "请输入地址:";
cin >> addr;
cout << "请输入电话:";
cin >> tel;
}
void output()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << sex << endl;
cout << "地址:" << addr << endl;
cout << "电话:" << tel << endl;
}
};
class Teacher : public virtual Staff
{
protected:
string title; //职称
public:
Teacher(string name1, int age1, string sex1, string addr1, string tel1, string title1)
{
name = name1;
age = age1;
sex = sex1;
title = title1;
addr = addr1;
tel = tel1;
}
void output();
void input();
};
void Teacher::input()
{
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入性别:";
cin >> sex;
cout << "请输入职称:";
cin >> title;
cout << "请输入地址:";
cin >> addr;
cout << "请输入电话:";
cin >> tel;
}
void Teacher::output()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << sex << endl;
cout << "职称:" << title << endl;
cout << "地址:" << addr << endl;
cout << "电话:" << tel << endl;
}
class Cadre :public virtual Staff
{
protected:
string post; //职务
public:
Cadre(string name1, int age1, string sex1, string addr1, string tel1,string post1);
void display();
void input();
};
Cadre::Cadre(string name1, int age1, string sex1, string addr1, string tel1,string post1)
{
name = name1;
age = age1;
sex = sex1;
addr = addr1;
tel = tel1;
post = post1;
}
void Cadre::input()
{
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入性别:";
cin >> sex;
cout << "请输入职务:";
cin >> post;
cout << "请输入地址:";
cin >> addr;
cout << "请输入电话:";
cin >> tel;
}
void Cadre::display()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << sex << endl;
cout << "职务:" << post << endl;
cout << "地址:" << addr << endl;
cout << "电话:" << tel << endl;
}
class Teacher_Cader :public Teacher, public Cadre
{
public:
Teacher_Cader(string name1 = "", int age1 = 0, string sex1 = "", string title1 = "", string post1 = "", string address1 = "", string tel1 = "", float wages1 = 0);
void input()
{
Teacher::input();
cout << "请输入职务:";
cin >> post;
cout << "请输入工资:";
cin>> wages;
}
void output()
{
Teacher::output();
cout << "职务:" << post << endl;
cout << "工资:" << wages << endl;
}
protected:
float wages;
};
Teacher_Cader::Teacher_Cader(string name1, int age1, string sex1, string title1, string post1, string address1, string tel1, float wages1) :Teacher(name1, age1, sex1, title1, address1, tel1), Cadre(name1, age1, sex1, post1, address1, tel1), wages(wages1)
{
}
int main()
{
Teacher_Cader person1;
person1.input(); //输入数据
person1.output();
system("pause");
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!