C++继承和派生练习(二)--关于从people(人员)类派生出student(学生)类等
1. 从people(人员)类派生出student(学生)类 添加属性:班号char classNO[7];从people类派生出teacher(教师)类, 添加属性:职务char principalship[11]、部门char department[21]。 从student类中派生graduate(研究生)类,添加属性:专业char subject[21]、 导师char teacher_adviser[21];从graduate类和teacher类派生出TA(助教生)类, 注意虚基类的使用。重载相应的成员函数,测试这些类。 2. 代码如下: ``` #include<iostream> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> using namespace std; class Data { public: Data() {} Data(int yy, int mm, int dd); Data(Data &ap); ~Data(); int get_year(); int get_month(); int get_day(); void set_year(int y); void set_month(int m); void set_day(int d); private: int year; int month; int day; }; Data::Data(int yy, int mm, int dd) { year = yy; month = mm; day = dd; } Data::Data(Data &ap) { year = ap.year; month = ap.month; day = ap.day; } Data::~Data() { } int Data::get_day() { return day; } int Data::get_month() { return month; } int Data::get_year() { return year; } void Data::set_day(int d) { day = d; } void Data::set_month(int m) { month = m; } void Data::set_year(int y) { year = y; } class People { public: People(int num, string se, Data birthd, string iid); People(People &tp); People() {} People get_People(); ~People() { } void set_number(int num) { number = num; } void set_sex(string se) { sex = se; } void set_birthday(Data birth) { birthday = birth; } void set_id(string iidd) { id = iidd; } int get_number(); string get_sex(); Data get_birthday(); string get_id(); void details(); private: int number; string sex; Data birthday; string id; }; inline int People::get_number() { return number; } inline string People::get_sex() { return sex; } inline string People::get_id() { return id; } Data People::get_birthday() { return birthday; } void People::details() { cout << "Number:" << number << endl; cout << "Sex:" << sex << endl; cout << "Birhtday:" << birthday.get_year() << "/" << birthday.get_month() << "/" << birthday.get_day() << endl; cout << "ID:" << id << endl; } People::People(int num, string se, Data birth, string iid) :birthday(birth) { number = num; sex = se; id = iid; } People People::get_People() { int num, yy, mm, dd; string ID, se; cout << "Please enter the number of the people:"; cin >> num; cout << "Please enter the sex of the people:(male or female)"; cin >> se; cout << "Please enter the birthday of the people:" << endl << "(Warnning:The format is similar to 1998 8 3)" << endl; cin >> yy >> mm >> dd; cout << "Please enter the id of the people:"; cin >> ID; Data birth(yy, mm, dd); id = ID; number = num; sex = se; birthday = birth; return *this; } People::People(People &tp) { number = tp.get_number(); sex = tp.get_sex(); id = tp.get_id(); birthday = tp.get_birthday(); } class Student :virtual public People { public: char classNo[7]; Student(int num, string se, Data birthd, string iid, char a[7]) :People(num, se, birthd, iid) { strcpy(classNo, a); } ~Student() { }; void Show_Student() { cout << "This is student:" << endl; cout << "ClassNo :" << classNo << endl; } }; class Teacher :virtual public People { public: char principalship[11]; char department[21]; Teacher(int num, string se, Data birthd, string iid, char a[11],char b[21]) :People(num, se, birthd, iid) { strcpy(principalship, a); strcpy(department, b); } Teacher() { } void Show_Teacher() { cout << "This is teacher:" << endl; cout << "Principalship :" << principalship << endl; cout << "Department :" << department << endl; } }; class Graduate :virtual public Student { public: char subject[21]; Teacher adviser; Graduate(int num, string se, Data birthd, string iid, char a[7], char c[21],Teacher vt) :People(num, se, birthd, iid), Student(num, se, birthd, iid, a) { strcpy(subject, c); adviser = vt; } ~Graduate() { } void Show_Graduate() { cout << "This is Graduate:" << endl; cout << "The subject:" << subject << endl; cout << "The adviser teacher:" << endl; cout << "Principalship:" << adviser.principalship<< endl; cout << "Department:" << adviser.department << endl; cout << endl; } }; class TA :public Graduate, public Teacher {
public: TA(int num, string se, Data birthd, string iid, char a[7], char c[21], Teacher vt) :People(num, se, birthd, iid), Student(num, se, birthd, iid, a), Graduate(num, se, birthd, iid, a, c, vt) { } ~TA() { } void Show_TA() { cout << "This is TA:" << endl; cout << "The classNo:" << classNo << endl; } }; int main() { People asp; asp.get_People(); asp.details(); Data a(1998, 8, 3); Student b(18,"male",a,"110","001"); b.Show_Student(); Data a1(1987, 8, 3); Teacher c(25, "female", a1,"1614","Advanced", "Promotion"); c.Show_Teacher(); Data a2(1990, 8, 3); Graduate d(22, "female", a2, "1013", "111", "CS", c); d.Show_Graduate(); return 0; } ```
(。・∀・)ノ
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库