#include #include using namespace std;

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 Teacher { // 教师类 public: Teacher(int n, char nm[], char s, BirthDate b); // 构造函数 void display(); // 输出函数 private: int num; char name[20]; char sex; BirthDate birthday; // 生日类对象 };

Teacher::Teacher(int n, char nm[], char s, BirthDate b) : num(n), sex(s), birthday(b) { strcpy(name, nm); }

void Teacher::display() { cout << 'num: ' << num << endl; cout << 'name: ' << name << endl; cout << 'sex: ' << sex << endl; birthday.display(); }

class Professor : public Teacher { // 教授类,继承自教师类 public: Professor(int n, char nm[], char s, int y, int m, int d, double a); // 构造函数 void display(); // 输出函数 void change(int y, int m, int d); // 修改函数 private: double area; };

Professor::Professor(int n, char nm[], char s, int y, int m, int d, double a) : Teacher(n, nm, s, BirthDate(y, m, d)), area(a) {}

void Professor::display() { Teacher::display(); cout << 'area: ' << area << endl; }

void Professor::change(int y, int m, int d) { Teacher::birthday.change(y, m, d); }

int main() { Professor prof1(3012, 'Zhang', 'f', 1949, 10, 1, 125.4); cout << 'original data:' << endl; prof1.display(); cout << endl << 'new data:' << endl; prof1.change(1950, 6, 1); prof1.display(); return 0; }

C++ 教师类、生日类和教授类实现及示例

原文地址: https://www.cveoy.top/t/topic/nH90 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录