分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
#include <iostream> #include <cstring> using namespace std; class Person { public: string name; int age; string sex; string add; int tele; Person(string a,int b,string c,string d,int e); }; Person::Person(string a,int b,string c,string d,int e):name(a),age(b),sex(c),add(d),tele(e) {} class Teacher:public Person { public: string title; void display(); Teacher(string a,int b,string c,string d,int e,string f); }; Teacher::Teacher(string a,int b,string c,string d,int e,string f):Person(a,b,c,d,e),title(f) {} void Teacher::display() { cout<<"姓名:"<<name<<" 年龄:"<<age<<" 性别:"<<sex<<" 职称:"<<title<<" 地址:"<<add<<" 电话:"<<tele; //cout<<"姓名:"<<Teacher::name<<" 年龄:"<<Teacher::age<<" 性别:"<<Teacher::sex<<" 职称:"<<Teacher::title<<" 地址:"<<Teacher::add<<" 电话:"<<Teacher::tele; } class Cadre:public Person { public: string post; Cadre(string a,int b,string c,string d,int e,string f); }; Cadre::Cadre(string a,int b,string c,string d,int e,string f):Person(a,b,c,d,e),post(f) {} class Teacher_Cadre:public Teacher,public Cadre { public: double wages; void show(); Teacher_Cadre(string a,int b,string c,string d,int e,string f,string g,double h); }; Teacher_Cadre::Teacher_Cadre(string a,int b,string c,string d,int e,string f,string g,double h): Teacher(a,b,c,d,e,f),Cadre(a,b,c,d,e,g),wages(h) {} void Teacher_Cadre::show() { display(); cout<<" 职务:"<<Cadre::post<<" 工资:"<<wages; } int main() { Teacher_Cadre aaa("lengjidong",19,"boy","student",17862817852,"child","gangchengdongdajie",3000.90); aaa.show( ); return 0; }
运行结果:
原文地址:http://blog.csdn.net/ljd939952281/article/details/45968681