將本章11.8節中的程序片斷加以補充完善。在程序中使用繼承和組合。在ios
定義Professor類對象prof1時給出全部數據的初值,而後修改profl的生日數據,最後ide
輸出profl的所有最新數據spa
- #include<iostream>
- #include<string>
- using namespace std;
- class Teacher
- {
- public:
- Teacher(int n,string name,string s):num(n),name(name),sex(s){}
- void display()
- {
- cout<<"num:"<<num<<endl;
- cout<<"name:"<<name<<endl;
- cout<<"sex:"<<sex<<endl;
- }
- private:
- int num;
- string name;
- string sex;
- };
- class BirthDate
- {
- public:
- BirthDate(int y,int m,int d):year(y),month(m),day(d){}
- void change(int y,int m,int d)
- {
- year=y;
- month=m;
- day=d;
- }
- void display()
- {
- cout<<year<<"/"<<month<<"/"<<day<<endl;
- }
- private:
- int year;
- int month;
- int day;
- };
- class Professor:public Teacher
- {
- public:
- Professor(int n,string name,string s,int y,int m,int d):Teacher(n,name,s), birthday(y,m,d){}
- void change(int y,int m,int d)
- {
- birthday.change(y,m,d);
- }
- void display()
- {
- Teacher::display();
- birthday .display();
- }
- private:
- BirthDate birthday;
- };
- int main()
- {
- Professor p1(101,"李四","男",1992,3,13);
- p1.change(1990,4,7);
- p1.display();
- }