代码如下
#include <iostream>
using namespace std;
//创建学生类
class stu
{
private:
string name;
int age;
float scort;
public:
stu()
{
cout << "学生无参构造" << endl;
};
stu(string name,int age ,float scort):name(name),age(age),scort(scort)
{
cout << "学生有参构造" << endl;
};
void show()
{
cout << "学生名字: " << name << endl;
cout << "学生年龄: " << age << endl;
cout << "学生分数: " << scort << endl;
}
~stu()
{
cout << "学生析构" << endl;
}
};
//创建当员类
class party
{
private:
string activety;
string org;
public:
party()
{
cout << "当员无参构造" << endl;
};
party(string activity,string org):activety(activity),org(org)
{
cout << "当员有参构造" << endl;
};
void show()
{
cout << "当员活动: " << activety << endl;
cout << "当员组织: " << org << endl;
}
~party()
{
cout << "当员析构" << endl;
}
};
//问题:怎么调用父类中的私有变量?
//1、在父类中自己调用输入函数
//问题:怎么初始化父类中的私有变量?
//直接显性赋值?试一试
//共同派生出学生干部
class post:public stu,public party
{
private:
string job;
public:
post()
{
cout << "干部无参构造" << endl;
};
post(string name,int age,float scort,string activety,string org,string job):\
stu(name,age,scort),party(activety,org),job(job)
{
cout << "干部有参构造" << endl;
};
//输出检验
void show()
{
cout << "干部职位: " << job << endl;
stu::show();
party::show();
}
~post()
{
cout << "干部析构" << endl;
}
};
int main()
{
cout << "Hello World!" << endl;
post stu1("zhangsan",18,89.5,"共产当","我为人人","班长");
stu1.show();
return 0;
}
效果图如下: