9月4日作业

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值