#include <iostream>
#include <string>
using namespace std;
class Human
{
public:
Human(){
cout << "Human的无参构造函数" << endl;
};
Human(string n, int a):name(n), age(a)
{
cout << "Human的有参构造函数" << endl;
}
Human(Human &other)
{
name = other.name;
age = other.age;
cout << "Human的拷贝构造函数" << endl;
}
Human &operator=(Human &other)
{
name = other.name;
age = other.age;
cout << "Human的拷贝赋值函数" << endl;
return *this;
}
~Human()
{
cout << "Human的析构函数" << endl;
}
void show()
{
cout << "Human.name = " << name << endl;
cout << "Human.age = " << age << endl;
}
private:
string name;
int age;
};
class Student:virtual public Human
{
public:
Student(){
cout << "Student的无参构造函数" << endl;
};
Student(string n, int a, int s):Human(n, a), score(s)
{
cout << "Student的有参构造函数" << endl;
}
~Student()
{
cout << "Student的析构函数" << endl;
}
void show()
{
//Human::show();
cout << "Student.score = " << score << endl;
}
private:
int score;
};
class Party:virtual public Human
{
public:
Party(){
cout << "Party的无参构造函数" << endl;
};
Party(string n, int g, string a, string o):Human(n, g), activity(a), organize(o)
{
cout << "Party的有参构造函数" << endl;
}
~Party()
{
cout << "Party的析构函数" << endl;
}
void show()
{
//Human::show();
cout << "Party.activity = " << activity << endl;
cout << "Party.organize = " << organize << endl;
}
private:
string activity;
string organize;
};
class Leader:public Student, public Party
{
public:
Leader(){
cout << "Leader的无参构造函数" << endl;
};
Leader(string n, int g, int s, string a, string o, string p):Human(n, g), Student(n, g, s), Party(n, g, a, o), post(p)
{
cout << "Leader的有参构造函数" << endl;
}
~Leader()
{
cout << "Leader的析构函数" << endl;
}
void show()
{
Human::show();
Student::show();
Party::show();
cout << "Leader.post = " << post << endl;
}
private:
string post;
};
int main(int argc, const char *argv[])
{
//Human h1("zs", 18);
//h1.show();
//Student s1("zs", 18, 99);
//s1.show();
//Party p1("zs", 18, "c++学习", "240702");
//p1.show();
Leader l1("zs", 18, 99, "c++学习", "240702", "吗喽");
l1.show();
return 0;
}
9月4日作业
最新推荐文章于 2024-10-20 20:14:58 发布