> 文章列表 > 4.23 C++作业

4.23 C++作业

4.23 C++作业

        定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)​

        再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。​

        由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后调用其show函数进行测试

#include <iostream>using namespace std;//学生类
class Student {
private:string name;int age;float score;public://学生类的无参构造Student(){cout<<"Stude::无参构造"<<endl;}//学生类的有参构造Student(string n,int a,float s):name(n),age(a),score(s){cout<<"Stude::有参构造"<<endl;}//析构函数~Student(){cout<<"Student::析构函数"<<endl;};//show函数void show(){cout<<"姓名:"<<name<<endl;cout<<"年龄:"<<age<<endl;cout<<"分数:"<<score<<endl;}
};//党员类
class Party {
private:string NPC;string organization;public://党员类的无参构造Party(){cout<<"Party::无参构造"<<endl;}//党员类的有参构造Party(string n,string o):NPC(n),organization(o){cout<<"Party::有参构造"<<endl;}//析构函数~Party(){cout<<"Party::析构函数"<<endl;};//show函数void show(){cout<<"党组织活动:"<<NPC<<endl;cout<<"组织:"<<organization <<endl;}};//学生干部类
class Cadre:public Student,public Party
{
private:string post;public://学生干部类的无参构造Cadre():Student(),Party() {cout<<"Party::无参构造"<<endl;}//学生干部类的有参构造Cadre(string n,int a,float s,string npc,string o,string p):Student(n,a,s),Party(npc,o),post(p){cout<<"Party::有参构造"<<endl;}//析构函数~Cadre(){cout<<"Cadre::析构函数"<<endl;};//show函数void show(){Student::show();Party::show();cout<<"职位:"<<post<<endl;}
};
int main()
{Cadre s1("张三",18,99,"青年党员","共青团","团员");s1.show();return 0;
}