作业: 多重继承
1. 定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)
2. 再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。
3. 由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后调用其show函数进行测试
#include <iostream>
using namespace std;
// 学生类
class Student
{
private:
string name; // 姓名
int age; // 年龄
int score; // 分数
public:
// 无参构造函数
Student() {cout << "Student无参构造函数" << endl;}
// 有参构造函数
Student(string s, int age, int score) : name(s), age(age), score(score) {cout << "Student有参构造函数" << endl;}
// 析构函数
~Student() {cout << "Student析构函数" << endl;}
// show函数
void show() {
cout << "name = " << name << "\t" << "age = " << age << "\t" << "score = " << score << endl;
}
};
// 党员类
class Party
{
private:
string poa; // 党组织活动
string org; // 组织
public:
// 无参构造函数
Party() {cout << "Party无参构造函数" << endl;}
// 有参构造函数
Party(string s1, string s2) : poa(s1), org(s2) {cout << "Party有参构造函数" << endl;}
// 析构函数
~Party() {cout << "Party析构函数" << endl;}
// show函数
void show() {
cout << "poa = " << poa << "\t" << "org = " << org << endl;
}
};
// 学生干部类
class Leader : public Student, public Party
{
private:
string post; // 职位
public:
// 无参构造函数
Leader() {cout << "Leader无参构造函数" << endl;}
// 有参构造函数
Leader(string name, int age, int score, string poa, string org, string post) : Student(name, age, score), Party(poa, org), post(post) {
cout << "Leader有参构造函数" << endl;
}
// 析构函数
~Leader() {cout << "Leader析构函数" << endl;}
// show函数
void show() {
Student::show ();
Party::show ();
cout << "post = " << post << endl;
}
};
int main()
{
// 实例学生干部对象
Leader l1("lisi", 18, 99, "学习第十四届全国人民代表大会会议内容", "hqyj", "宣传委员");
l1.show ();
return 0;
}