#include <iostream>
using namespace std;
class Human
{
private:
string name;
int age;
public:
Human(string n, int a)//有参构造函数
{
name = n;
age = a;
}
Human()//无参构造函数
{
name = "unknown";
age = 0;
}
~Human()//析构函数
{
cout << "析构函数" << endl;
}
void show()//show函数
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
}
};
class Student : public Human//学生类继承自人类
{
private:
int score;
public:
Student(string n, int a, int s) : Human(n, a)//派生类有参构造函数
{
score = s;
}
Student() : Human()//派生类无参构造函数
{
score = 0;
}
~Student()//派生类析构函数
{
cout << "学生析构函数" << endl;
}
void show()//派生类show函数
{
cout << "分数:" << score << endl;
Human::show();
}
};
class Pary : public Human//党员类继承自人类
{
private:
string activity;
string organization;
public:
Pary(string n, int a, string act, string org) : Human(n, a)//派生类有参构造函数
{
activity = act;
organization = org;
}
Pary() : Human()//派生类无参构造函数
{
activity = "unknown";
organization = "unknown";
}
~Pary()//派生类析构函数
{
cout << "党员析构函数" << endl;
}
void show()//派生类show函数
{
cout << "党组织活动:" << activity << endl;
cout << "组织:" << organization << endl;
Human::show();
}
};
class StudentLeader : public Student, public Pary//学生干部类继承自学生类和党员类
{
private:
string position;
public:
StudentLeader(string n, int a, int s, string act, string org, string pos) : Student(n, a, s), Pary(n, a, act, org)//派生类有参构造函数
{
position = pos;
}
StudentLeader() : Student(), Pary()//派生类无参构造函数
{
position = "unknown";
}
~StudentLeader()//派生类析构函数
{
cout << "学生干部析构函数" << endl;
}
void show()//派生类show函数
{
cout << "职位:" << position << endl;
Student::show();
Pary::show();
}
};
int main()
{
StudentLeader sl("张三", 20, 90, "学习", "学生会", "干事");
cout << "学生干部信息:" << endl;
sl.show();
return 0;
}