cpp——类——多重继承——构造函数 析构函数 赋值操作符函数

来源:互联网 发布:海康网络摄像机密码 编辑:程序博客网 时间:2024/05/16 08:57

多重继承

多重继承指子类包含多个直接父类

直接父类继承自不同父类

class CDAnimal{public:    CDAnimal() : mGroup(0)    {        cout << "CDAnimal()" << endl;    }        CDAnimal(int group) : mGroup(group)    {        cout << "CDAnimal(" << group << ")" << endl;    }        CDAnimal(const CDAnimal &other) : mGroup(other.mGroup)    {        cout << "CDAnimal(const CDAnimal &other)" << endl;    }        ~CDAnimal()    {        cout << "~CDAnimal()" << endl;    }    public:    CDAnimal& operator=(const CDAnimal &other)    {        mGroup = other.mGroup;        cout << "CDAnimal operator=" << endl;        return *this;    }    private:    int mGroup;};class CHAnimal{public:    CHAnimal() : mGroup(0)    {        cout << "CHAnimal()" << endl;    }        CHAnimal(int group) : mGroup(group)    {        cout << "CHAnimal(" << group << ")" << endl;    }        CHAnimal(const CHAnimal &other) : mGroup(other.mGroup)    {        cout << "CHAnimal(const CHAnimal &other)" << endl;    }        ~CHAnimal()    {        cout << "~CHAnimal()" << endl;    }    public:    CHAnimal& operator=(const CHAnimal &other)    {        mGroup = other.mGroup;        cout << "CHAnimal operator=" << endl;        return *this;    }    private:    int mGroup;};class CDonkey : public CDAnimal{public:    CDonkey() : mStrength(40)    {        cout << "CDonkey()" << endl;    }        CDonkey(int strength) : CDAnimal(4), mStrength(strength)    {        cout << "CDonkey(" << strength << ")" << endl;    }        CDonkey(const CDonkey &other) : CDAnimal(other), mStrength(other.mStrength)    {        cout << "CDonkey(const CDonkey &other)" << endl;    }        ~CDonkey()    {        cout << "~CDonkey()" << endl;    }    public:    CDonkey& operator=(const CDonkey &other)    {        CDAnimal::operator=(other);        mStrength = other.mStrength;        cout << "CDonkey operator=" << endl;        return *this;    }    private:    int mStrength;;};class CHorse : public CHAnimal{public:    CHorse() : mSpeed(2999)    {        cout << "CHorse()" << endl;    }        CHorse(int speed) : CHAnimal(5), mSpeed(speed)    {        cout << "CHorse(" << speed << ")" << endl;    }        CHorse(const CHorse &other) : CHAnimal(other), mSpeed(other.mSpeed)    {        cout << "CHorse(const CHorse &other)" << endl;    }        ~CHorse()    {        cout << "~CHorse()" << endl;    }    public:    CHorse& operator=(const CHorse &other)    {        CHAnimal::operator=(other);        mSpeed = other.mSpeed;        cout << "CHorse operator=" << endl;        return *this;    }    private:    int mSpeed;};class CMule : public CDonkey, public CHorse{public:    CMule() : mExtraStrength(20)    {        cout << "CMule()" << endl;    }        CMule(int extraStrength) : CHorse(50), CDonkey(60), mExtraStrength(extraStrength)    {        cout << "CMule(" << extraStrength << ")" << endl;    }        ~CMule()    {        cout << "~CMule()" << endl;    }    private:    int mExtraStrength;};void multi_inherit(){    CMule mule1;    CMule mule2(30);    CMule copyMule = mule2;    mule1 = mule2;}
output:
CDAnimal()CDonkey()CHAnimal()CHorse()CMule()CDAnimal(4)CDonkey(60)CHAnimal(5)CHorse(50)CMule(30)CDAnimal(const CDAnimal &other)CDonkey(const CDonkey &other)CHAnimal(const CHAnimal &other)CHorse(const CHorse &other)CDAnimal operator=CDonkey operator=CHAnimal operator=CHorse operator=~CMule()~CHorse()~CHAnimal()~CDonkey()~CDAnimal()~CMule()~CHorse()~CHAnimal()~CDonkey()~CDAnimal()~CMule()~CHorse()~CHAnimal()~CDonkey()~CDAnimal()

直接父类继承自同一父类

class CAnimal{public:    CAnimal() : mGroup(0)    {        cout << "CAnimal()" << endl;    }        CAnimal(int group) : mGroup(group)    {        cout << "CAnimal(" << group << ")" << endl;    }        CAnimal(const CAnimal &other) : mGroup(other.mGroup)    {        cout << "CAnimal(const CAnimal &other)" << endl;    }        ~CAnimal()    {        cout << "~CAnimal()" << endl;    }    public:    CAnimal& operator=(const CAnimal &other)    {        mGroup = other.mGroup;        cout << "CAnimal operator=" << endl;        return *this;    }    private:    int mGroup;};class CDonkey : public CAnimal{public:    CDonkey() : mStrength(40)    {        cout << "CDonkey()" << endl;    }        CDonkey(int strength) : CAnimal(4), mStrength(strength)    {        cout << "CDonkey(" << strength << ")" << endl;    }        CDonkey(const CDonkey &other) : CAnimal(other), mStrength(other.mStrength)    {        cout << "CDonkey(const CDonkey &other)" << endl;    }        ~CDonkey()    {        cout << "~CDonkey()" << endl;    }    public:    CDonkey& operator=(const CDonkey &other)    {        CAnimal::operator=(other);        mStrength = other.mStrength;        cout << "CDonkey operator=" << endl;        return *this;    }    private:    int mStrength;;};class CHorse : public CAnimal{public:    CHorse() : mSpeed(2999)    {        cout << "CHorse()" << endl;    }        CHorse(int speed) : CAnimal(5), mSpeed(speed)    {        cout << "CHorse(" << speed << ")" << endl;    }        CHorse(const CHorse &other) : CAnimal(other), mSpeed(other.mSpeed)    {        cout << "CHorse(const CHorse &other)" << endl;    }        ~CHorse()    {        cout << "~CHorse()" << endl;    }    public:    CHorse& operator=(const CHorse &other)    {        CAnimal::operator=(other);        mSpeed = other.mSpeed;        cout << "CHorse operator=" << endl;        return *this;    }    private:    int mSpeed;};class CMule : public CDonkey, public CHorse{public:    CMule() : mExtraStrength(20)    {        cout << "CMule()" << endl;    }        CMule(int extraStrength) : CHorse(50), CDonkey(60), mExtraStrength(extraStrength)    {        cout << "CMule(" << extraStrength << ")" << endl;    }        ~CMule()    {        cout << "~CMule()" << endl;    }    private:    int mExtraStrength;};void multi_inherit(){    CMule mule1;    CMule mule2(30);    CMule copyMule = mule2;    mule1 = mule2;}
output:
CAnimal()CDonkey()CAnimal()CHorse()CMule()CAnimal(4)CDonkey(60)CAnimal(5)CHorse(50)CMule(30)CAnimal(const CAnimal &other)CDonkey(const CDonkey &other)CAnimal(const CAnimal &other)CHorse(const CHorse &other)CAnimal operator=CDonkey operator=CAnimal operator=CHorse operator=~CMule()~CHorse()~CAnimal()~CDonkey()~CAnimal()~CMule()~CHorse()~CAnimal()~CDonkey()~CAnimal()~CMule()~CHorse()~CAnimal()~CDonkey()~CAnimal()

总结

  • 多重继承类的父类构造顺序跟子类继承父类顺序一致,与子类初始化式中父类初始化式顺序无关,(合成)默认构造函数和(合成)复制构造函数作为特殊构造函数亦如此
  • 多重继承类的父类析构顺序总是父类构造顺序的逆序
  • 多重继承类的合成赋值操作符函数的父类赋值操作符函数调用顺序跟子类继承父类顺序一致
0 0