分别定义Student学生类和Cadre干部类采用多重继承出Student_Cadre学生干部类。要求:1Student类含有:string name、char sex、int ID、float score 数据成员。成员函数display 用于输出name sex ID score信息。2Cadre 类含有:string name、char sex、int ID、string post职务
代码如下:
#include
class Student { protected: string name; char sex; int ID; float score; public: void display() { cout << "Name: " << name << endl; cout << "Sex: " << sex << endl; cout << "ID: " << ID << endl; cout << "Score: " << score << endl; } };
class Cadre { protected: string name; char sex; int ID; string post; public: void display() { cout << "Name: " << name << endl; cout << "Sex: " << sex << endl; cout << "ID: " << ID << endl; cout << "Post: " << post << endl; } };
class Student_Cadre : public Student, public Cadre { private: double wages; public: void display() { Student::display(); cout << "Post: " << post << endl; cout << "Wages: " << wages << endl; } };
int main() { Student_Cadre s; s.name = "Tom"; s.sex = 'M'; s.ID = 123; s.score = 89.5; s.post = "Class Monitor"; s.wages = 500.0; s.display(); return 0;
原文地址: https://www.cveoy.top/t/topic/flTG 著作权归作者所有。请勿转载和采集!