作业
1.定义一个人类 (Human):私有成员(姓名,年龄)、成员方法(无参构造、有参构造、析构函数、show函数)
2.由人类派生出一个学生类(Student):私有成员属性(分数)、成员方法(无参构造、有参构造、析构函数、show函数)
3.由人类派生出一个党员类(Pary):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。
4.由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象
然后调用其show函数进行测试
#include <iostream>
#include <string>
using namespace std;
class Human
{
private:
string name;
int age;
public:
Human() { cout << "Human的无参构造函数" << endl; }
Human(string n, int a) : name(n), age(a) { cout << "Human的有参构造函数" << endl; }
~Human() { cout << "Human的析构函数" << endl; }
void show() const
{
cout << "Human::name=" << name << endl;
cout << "Human::age=" << age << endl;
}
};
class Student : public Human
{
private:
int score;
public:
Student() : Human() { cout << "Student的无参构造函数" << endl; }
Student(string n, int a, int s) : Human(n, a), score(s) { cout << "Student的有参构造函数" << endl; }
~Student() { cout << "Student的析构函数" << endl; }
void show() const
{
Human::show();
cout << "Student::score=" << score << endl;
}
};
class Party : public Human
{
private:
string activity;
string organization;
public:
Party() : Human() { cout << "Party的无参构造函数" << endl; }
Party(string n, int a, string act, string org) : Human(n, a), activity(act), organization(org)
{
cout << "Party的有参构造函数" << endl;
}
~Party() { cout << "Party的析构函数" << endl; }
void show() const
{
Human::show();
cout << "Party::activity=" << activity << endl;
cout << "Party::organization=" << organization << endl;
}
};
class Leader : public Student, public Party
{
private:
string post;
public:
Leader() : Student(), Party(), post("") {cout << "Leader的无参构造函数" << endl;}
Leader(string n, int a, int s, string act, string org, string p):Student(n, a, s), Party(n, a, act, org), post(p)
{
cout << "Leader的有参构造函数" << endl;
}
~Leader(){cout << "Leader的析构函数" << endl;}
void show() const
{
Student::show();
Party::show();
cout << "Leader::post=" << post << endl;
}
};
int main()
{
Leader l1("何与", 20, 99, "清扫垃圾", "第二党支部", "小组长");
l1.show();
cout << "****************************" << endl;
return 0;
}