①定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)
②再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。
③由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后调用其show函数进行测试
#include <iostream>
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;
}
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 ac,string org):activity(ac),organization(org)
{
cout<<"Party::无参构造"<<endl;
}
~Party()
{
cout<<"Party::析构函数"<<endl;
}
void show()
{
cout<<"党组织活动:"<<activity<<endl;
cout<<"组织:"<<organization<<endl;
}
};
class Cadres :public Student,public Party
{
private:
string position;
public:
//无参构造
Cadres()
{
cout<<"Cadres::无参构造"<<endl;
}
//有参构造
Cadres(string name,int age,double score,string activity,string organization,string position):
Student(name,age,score),Party(activity,organization),position(position)
{
cout<<"Cadres::有参构造"<<endl;
}
//析构函数
~Cadres()
{
cout<<"Cadres::析构函数"<<endl;
}
void show()
{
Student::show();
Party::show();
cout<<"职位:"<<position<<endl;
}
};
int main()
{
Cadres c("lzp",22,99.9,"人大","计科","代表");
cout<<"*********************************"<<endl;
c.show();
cout<<"*********************************"<<endl;
return 0;
}