#include iostream#include cstringusing namespace std;class Teacher 教师类 public Teacherintchar char; 声明构造函数 void display; 声明输出
#include
class Teacher //教师类 { public: Teacher(int n, char *na, char s); //声明构造函数 void display(); //声明输出函数
protected: int num; char name[20]; char sex; };
Teacher::Teacher(int n, char *na, char s) //实现构造函数 { num = n; strcpy(name, na); sex = s; }
void Teacher::display() //输出函数 { cout << "num:" << num << endl; cout << "name:" << name << endl; cout << "sex:" << sex << endl; }
class BirthDate //生日类 { public: BirthDate(int y, int m, int d); //声明构造函数 void display(); //声明输出函数 void change(int y, int m, int d); //声明修改函数
protected: 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 n, char *na, char s, int y, int m, int d, double a); //声明构造函数 void display(); //声明输出函数 void change(int y, int m, int d); //声明修改函数
private: BirthDate birthday; double area; };
Professor::Professor(int n, char *na, char s, int y, int m, int d, double a) : Teacher(n, na, 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); //创建教授对象 cout << endl << "original data:" << endl; prof1.display(); //输出原始数据 cout << endl << "new data:" << endl; prof1.change(1950, 6, 1); //修改生日 prof1.display(); //输出修改后的数据 return 0;
原文地址: http://www.cveoy.top/t/topic/cTSU 著作权归作者所有。请勿转载和采集!