定义一个Teacher类,类中包括:
数据成员:name(姓名), age(年龄), gender(性别), tel(电话), title(职称)。
函数成员:构造函数。display函数输出对象的数据。
定义一个Cadre类,类中包括:
数据成员:name(姓名), age(年龄), gender(性别), tel(电话), post(职务)。
函数成员:构造函数。display函数输出对象额数据。
定义一个Teacher_Cadre类,公用继承Teacher类,公用接触Cadre类。类中包括:
数据成员:wages(工资)。
函数成员:构造函数。show函数输出对象的数据。
要求:
- 类的成员函数类内声明,类外实现。
- 派生类中的同名成员,在引用时,指定作用域。
- Teacher_Cadre类的成员函数show中,调用Teacher函数中的display,在用cout语句输出post(职务)和wages(工资)。
- 在main函数中定义三个类的对象,并使用成员函数输出对象的数据。
#include<iostream> #include<string> using namespace std; class Teacher{ private: string name; int age; string gender; int tel; string title; public: Teacher(string,int,string,int,string); void display(); }; class Cadre{ protected: string post; string name; int age; string gender; int tel; public: Cadre(string,int,string,int,string); void display(); }; class Teacher_Cadre:public Teacher,public Cadre{ private: double wages; public: Teacher_Cadre(string, int, string, int, string, string, float); void show(); }; //Teacher::Teacher(){ // cout <<"Teacher请输入姓名、年龄、性别、电话、职称" << endl; // cin >>name>>age>>gender>>tel>>title; //} Teacher::Teacher(string n, int a, string g, int te, string ti) { name = n; age = a; gender = g; tel = te; title = ti; } void Teacher::display(){ cout << "姓名:" <<name<<endl; cout << "年龄:" <<age<<endl; cout << "性别:" <<gender<<endl; cout << "电话:" <<tel<<endl; cout << "职称:" <<title<<endl; cout<<endl; } //Cadre::Cadre(){ // cout <<"Cadre请输入姓名、年龄、性别、电话、职务"<<endl; // cin >>name>>age>>gender>>tel>>post; //} Cadre::Cadre(string n, int a, string g, int t, string p):name(n),age(a),gender(g),tel(t),post(p){} void Cadre::display(){ cout << "姓名:" <<name<<endl; cout << "年龄:" <<age<<endl; cout << "性别:" <<gender<<endl; cout << "电话:" <<tel<<endl; cout << "职称:" <<post<<endl; cout<<endl; } //Teacher_Cadre::Teacher_Cadre(){ // cout<<"请输入工资"<<endl; // cin>>wages; //} Teacher_Cadre::Teacher_Cadre(string n, int a, string g, int t, string ti, string p, float w):Teacher(n,a,g,t,ti),Cadre(n,a,g,t,p),wages(w){} void Teacher_Cadre::show(){ Teacher::display(); cout<<"职务:"<<post<<endl; cout<<"工资:"<<wages<<endl; cout<<endl; } int main(void){ // Teacher t; // Cadre c; // Teacher_Cadre tc; // t.display(); // c.display(); // tc.show(); Teacher t("张三", 21, "男", 111111, "高级教师"); Cadre c("李四", 30, "男", 666666, "教学秘书"); Teacher_Cadre tea_cad1("李明", 18, "女", 888888, "特级","研究生导师",30000); t.display(); c.display(); tea_cad1.show(); return 0; }