//定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法
//(无参构造、有参构造、析构函数、show函数)
//再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法
//(无参构造、有参构造、析构函数、show函数)。
//由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参
//构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后
//调用其show函数进行测试
#include <iostream>
//定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法
//(无参构造、有参构造、析构函数、show函数)
//再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法
//(无参构造、有参构造、析构函数、show函数)。
//由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参
//构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后
//调用其show函数进行测试
using namespace std;
class student
{
private:
string name;
int age;
double score;
public:
//无参构造
student()
{
cout<<"student::无参构造"<<endl;
}
//有参构造
student(string name,int age,double score):name(name),age(age),score(score)
{
cout<<"student::有参构造"<<endl;
}
//析构函数
~student()
{
cout<<"student::析构函数"<<endl;
}
//show
void show()
{
cout<<"student::student"<<endl;
cout<<"name= "<<name<<endl;
cout<<"age= "<<age<<endl;
cout<<"score= "<<score<<endl;
}
};
class party
{
private:
string activity;
string organization;
public:
//无参构造
party()
{
cout<<"party::无参构造"<<endl;
}
//有参构造
party(string activity, string organization):activity(activity),organization(organization)
{
cout<<"party::有参构造"<<endl;
}
//析构函数
~party()
{
cout<<"party::析构函数"<<endl;
}
//show
void show()
{
cout<<"party::party"<<endl;
cout<<"activity= "<<activity<<endl;
cout<<"organization= "<<organization<<endl;
}
};
class student_cadre: public student,public party
{
private:
string position; //职位
public:
//无参构造
student_cadre()
{
cout<<"student_cadre::无参构造"<<endl;
}
//有参构造
student_cadre(string name,int age,double score,string activity, string organization, string position):
student(name,age,score),party(activity,organization),position(position)
{
cout<<"student_cadre::有参构造"<<endl;
}
//析构函数
~student_cadre()
{
cout<<"student_cadre::析构函数"<<endl;
}
//show
void show()
{
student::show();
party::show();
cout<<"student_cadre::student_cadre"<<endl;
cout<<"position= "<<position<<endl;
}
};
int main()
{
student_cadre s1("小黄弟弟",18,88,"党员大会","党组织","党委书记");
s1.show();
return 0;
}
效果图: