定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法 (无参构造、有参构造、析构函数、show函数)
再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法 (无参构造、有参构造、析构函数、show函数)。
由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参 构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后 调用其show函数进行测试
#include <iostream>
using namespace std;
class stu
{
private:
string name;
int age;
double score;
public:
stu()
{
cout<<"stu::无参构造"<<endl;
}
stu(string n,int a,double s):name(n),age(a),score(s)
{
cout<<"stu::有参构造"<<endl;
}
~stu()
{
cout<<"stu::析构函数"<<endl;
}
void show()
{
cout<<name<<endl;
cout<<age<<endl;
cout<<score<<endl;
}
};
class party
{
private:
string activity;
string organization;
public:
party()
{
cout<<"party::无参构造"<<endl;
}
party(string a,string o):activity(a),organization(o)
{
cout<<"party::有参构造"<<endl;
}
~party()
{
cout<<"party::析构函数"<<endl;
}
void show()
{
cout<<activity<<endl;
cout<<organization<<endl;
}
};
class cadre:public stu,public party
{
private:
string position;
public:
cadre()
{
cout<<"cadre::无参构造"<<endl;
}
cadre(string n,int b,double s,string a,string o,string p):stu(n,b,s),party(a,o),position(p)
{
cout<<"cadre::有参构造"<<endl;
}
~cadre()
{
cout<<"cadre::析构函数"<<endl;
}
void show()
{
stu::show();
party::show();
cout<<position<<endl;
}
};
int main()
{
cadre s1("魏无羡",18,69,"养兔子","莲花邬","夷陵老祖");
s1.show();
cout << "Hello World!" << endl;
return 0;
}