1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 class Teacher //教师类 5 {public: 6 Teacher(int,char [],char); //声明构造函数 7 void display(); //声明输出函数 8 private: 9 int num; 10 char name[20]; 11 char sex; 12 }; 13 14 Teacher::Teacher(int n,char nam[],char s) //定义构造函数 15 {num=n; 16 strcpy(name,nam); 17 sex=s; 18 } 19 20 void Teacher::display() //定义输出函数 21 {cout<<"num:"<<num<<endl; 22 cout<<"name:"<<name<<endl; 23 cout<<"sex:"<<sex<<endl; 24 } 25 26 class BirthDate //生日类 27 {public: 28 BirthDate(int,int,int); //声明构造函数 29 void display(); //声明输出函数 30 void change(int,int,int); //声明修改函数 31 private: 32 int year; 33 int month; 34 int day; 35 }; 36 37 BirthDate::BirthDate(int y,int m,int d) //定义构造函数 38 {year=y; 39 month=m; 40 day=d; 41 } 42 43 void BirthDate::display() //定义输出函数 44 {cout<<"birthday:"<<month<<"/"<<day<<"/"<<year<<endl;} 45 46 void BirthDate::change(int y,int m,int d) //定义修改函数 47 {year=y; 48 month=m; 49 day=d; 50 } 51 52 class Professor:public Teacher //教授类 53 {public: 54 Professor(int,char [],char,int,int,int,float); //声明构造函数 55 void display(); //声明输出函数 56 void change(int,int,int); //声明修改函数 57 private: 58 float area; 59 BirthDate birthday; //定义BirthDate类的对象作为数据成员 60 }; 61 62 Professor::Professor(int n,char nam[20],char s,int y,int m,int d,float a): 63 Teacher(n,nam,s),birthday(y,m,d),area(a){ } //定义构造函数 64 65 void Professor::display() //定义输出函数 66 {Teacher::display(); 67 birthday.display(); 68 cout<<"area:"<<area<<endl; 69 } 70 71 void Professor::change(int y,int m,int d) //定义修改函数 72 {birthday.change(y,m,d); 73 } 74 75 int main() 76 {Professor prof1(3012,"Zhang",'f',1949,10,1,125.4); //定义Professor对象prof1 77 cout<<endl<<"original data:"<<endl; 78 prof1.display(); //调用prof1对象的display函数 79 cout<<endl<<"new data:"<<endl; 80 prof1.change(1950,6,1); //调用prof1对象的change函数 81 prof1.display(); //调用prof1对象的display函数 82 return 0; 83 }