/* *Copyright(c)2016,烟台大学计算机与控制工程学院 *All rights reserved *文件名称:123.cpp *作 者:王蕊 *完成日期:2016年5月10日 *版 本 号:v1.0 * *问题描述:分别定义Teacher类和Cadre类,采用多重继承方式由这两个类派生出新类Teacher_Cadre。 */ #include<iostream> #include<cstring> using namespace std; class Teacher { private: string title; string address; string name; int age; char sex; int telephone; public: Teacher(string n,int a,char s,string t,string ad,int tel):name(n),age(a),sex(s),title(t),address(ad),telephone(tel){} void display(); }; void Teacher::display() { cout<<"Name:"<<name<<endl; cout<<"Age:"<<age<<endl; cout<<"Sex:"<<sex<<endl; cout<<"Title:"<<title<<endl; cout<<"Address:"<<address<<endl; cout<<"Tel:"<<telephone<<endl; } class Cadre { private: string post; string address; string name; int age; char sex; int telephone; public: Cadre(string n,int a,char s,string p,string ad,int tel):name(n),age(a),sex(s),post(p),address(ad),telephone(tel){} string printpost(); void display(); }; void Cadre::display() { cout<<"Name:"<<name<<endl; cout<<"Age:"<<age<<endl; cout<<"Sex:"<<sex<<endl; cout<<"Post:"<<post<<endl; cout<<"Address:"<<address<<endl; cout<<"Tel:"<<telephone<<endl; } string Cadre::printpost() { return post; } class Teacher_Cadre:public Teacher,public Cadre { private: double wages; public: Teacher_Cadre(string n,int a,char s,string t,string p,string ad,int tel,double w):Teacher(n,a,s,t,ad,tel),Cadre(n,a,s,p,ad,tel),wages(w){} void show(); } ; void Teacher_Cadre::show() { Teacher::display(); cout<<"Post:"<<Cadre::printpost()<<endl; cout<<"Wages:"<<wages<<endl; } int main() { Teacher_Cadre Colin("shu baba",32,'F',"Professor","Headmasters","zhongguo",123456789,300000); Colin.show(); return 0; }
运行结果:
学习心得:
学会用继承方式解决问题,还有派生类的构造函数的写法。
时间: 2024-10-07 10:39:17