定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)
再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。
由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后调用其show函数进行测试
#include <iostream>
using namespace std;
class student{
private:
string name;
int age;
double score;
public:
student() {}
student(string n,int a,double s):name(n),age(a),score(s) {}
~student() {}
void show()
{
cout<<"student:name = "<<name<<endl;
cout<<"student:age = "<<age<<endl;
cout<<"student:score = "<<score<<endl;
}
};
class Party
{
private:
string activity;
string group;
public:
Party() {}
Party(string a,string g):activity(a),group(g) {}
~Party() {}
void show()
{
cout<<"party:activity: "<<activity<<endl;
cout<<"party:group: "<<group<<endl;
}
};
class cadres:public student,public Party
{
private:
string position;
public:
cadres() {}
cadres(string n,int a1,double s,string a2,string g,string p):student(n,a1,s),Party(a2,g),position(p) {}
~cadres() {}
void show()
{
student::show();
Party::show();
cout<<"cadres:: position: "<< position<<endl;
}
};
int main()
{
cadres c1("xlx",18,100,"知识问答","宣传部","宣传部部长");
c1.show();
cout << "Hello World!" << endl;
return 0;
}