#include <iostream>
#include <cstring>
using namespace std;
class Teacher //教師類
{public:
Teacher(int,char [],char); //聲明構造函
數
void display(); //聲明輸出函
數
private:
int num;
char name[20];
char sex;
};
Teacher::Teacher(int n,char nam[],char s) //定義構造
函數
{num=n;
strcpy(name,nam);
sex=s;
}
void Teacher::display() //定義輸出函
數
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
class BirthDate //生日類
{public:
BirthDate(int,int,int); //聲明構造函
數
void display(); //聲明輸出
函數
void change(int,int,int); //聲明修改函
數
private:
int year;
int month;
int day;
};
BirthDate::BirthDate(int y,int m,int d) //定義構造函
數
{year=y;
month=m;
day=d;
}
void BirthDate::display() //定義輸出
函數
{cout<<"birthday:"<<month<<"/"<<day<<"/"<<year<<endl
;}
void BirthDate::change(int y,int m,int d) //定義修改函
數
{year=y;
month=m;
day=d;
}
class Professor:public Teacher
//教授類
{public:
Professor(int,char [],char,int,int,int,float); //聲明構
造函數
void display(); //
聲明輸出函數
void change(int,int,int); //聲
明修改函數
private:
float area;
BirthDate birthday; //
定義 BirthDate 類的對
象作爲數據成員
};
Professor::Professor(int n,char nam[20],char s,int y,int
m,int d,float a):
Teacher(n,nam,s),birthday(y,m,d),area(a){ } //
定義構造函數
void Professor::display() //
定義輸出函數
{Teacher::display();
birthday.display();
cout<<"area:"<<area<<endl;
}
void Professor::change(int y,int m,int d) //定
義修改函數
{birthday.change(y,m,d);
}
int main()
{Professor prof1(3012,"Zhang",'f',1949,10,1,125.4); //
定義 Professor 對象
prof1
cout<<endl<<"original data:"<<endl;
prof1.display(); //
調用 prof1 對象的
display 函數
cout<<endl<<"new data:"<<endl;
prof1.change(1950,6,1);
//調用 prof1 對象的
change 函數
prof1.display(); //
調用 prof1 對象的
display 函數
return 0;
}
在程序中使用繼承和組合 第五章第十題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.