定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)再定义一个党员类(Party):私有成员属性(党组织活动,
#include <iostream>
using namespace std;
//学生类
class Stu
{
private:
string name;//姓名
int age;//年龄
double score;//成绩
public:
//无参构造
Stu(){cout<<"无参构造"<<endl;}
//有参构造
Stu(string n,int a,double s):name(n),age(a),score(s){cout<<"有参函数"<<endl;}
//析构函数
~Stu(){cout<<"析构函数"<<endl;}
//show函数
void show()
{
cout<<"name="<<name<<endl;
cout<<"age="<<age<<endl;
cout<<"score="<<score<<endl;
}
};
//党员类
class Party
{
private:
string activi;//活动
string orga;//组织
public:
//无参
Party(){cout<<"无参构造"<<endl;}
//有参
Party(string a,string o):activi(a),orga(o){cout<<"有参构造"<<endl;}
//析构函数
~Party(){cout<<"析构函数"<<endl;}
//show函数
void show()
{
cout<<"activi="<<activi<<endl;
cout<<"orga="<<orga<<endl;
}
};
//干部类
class Leader : public Stu,public Party
{
private:
string work;//职位
public:
//无参
Leader(){cout<<"无参构造"<<endl;}
//有参
Leader(string n,int a1,double s,string a2,string o,string w):Stu(n,a1,s),Party(a2,o),work(w){cout<<"有参构造"<<endl;}
//析构函数
~Leader(){cout<<"析构函数"<<endl;}
//show函数
void show()
{
Stu::show();
Party::show();
cout<<"work="<<work<<endl;
}
};
int main()
{
//实例化
Leader s("wss",25,100,"志愿活动","党的生日","党员");
s.show();//展示
return 0;
}
x