多重继承
//例11.8.多重继承 # include <iostream> # include <string.h> using namespace std; class Teacher//声明teacher类 { public: Teacher(string nam,int a,string t)//构造函数 { name=nam; age=a; title=t; } void display() { cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"title: "<<title<<endl; } protected: string name; int age; string title; }; //2. class Student//定义student类 { public: Student( string nam,char s,float sco)//构造函数 { //strcpy(name1,nam); name1=nam; sex=s; score=sco; } void display1() { cout<<"name: "<<Student::name1<<endl; cout<<"sex: "<<sex<<endl; cout<<"score: "<<score<<endl; } protected: string name1; char sex; float score; }; //3. class Graduate:public Teacher,public Student//声明多重继承的派生类graduate { public: Graduate(string nam,int a,char s,string t,float sco,float w):Teacher(nam,a,t),Student(nam,s,sco),wage(w){} void show() { cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"sex: "<<sex<<endl; cout<<"score: "<<score<<endl; cout<<"title: "<<title<<endl; cout<<"wages: "<<wage<<endl; } private: float wage; }; int main()//main函数进行调试 { Graduate grad1("wang_li",24,'f',"assistant",89.5,1200); grad1.show(); return 0; }
运行结果:
说说:我把它:strcpy(name1,nam)改成name1=nam;