cpp——多重继承——构造 复制 析构 赋值

来源:互联网 发布:刘斐和郭汝瑰 知乎 编辑:程序博客网 时间:2024/05/16 09:58

多父类继承自不同父类

class CAnimal1{public:    CAnimal1(int group) : mGroup(group)    {        cout << "CAnimal1(" << group << ")" << endl;    }        CAnimal1(const CAnimal1 &other) : mGroup(other.mGroup)    {        cout << "CAnimal1(const CAnimal1 &other)" << endl;    }        ~CAnimal1()    {        cout << "~CAnimal1()" << endl;    }    public:    CAnimal1& operator=(const CAnimal1 &other)    {        mGroup = other.mGroup;        cout << "CAnimal1 operator=" << endl;        return *this;    }    private:    int mGroup;};class CAnimal2{public:    CAnimal2(int group) : mGroup(group)    {        cout << "CAnimal2(" << group << ")" << endl;    }        CAnimal2(const CAnimal2 &other) : mGroup(other.mGroup)    {        cout << "CAnimal2(const CAnimal2 &other)" << endl;    }        ~CAnimal2()    {        cout << "~CAnimal2()" << endl;    }    public:    CAnimal2& operator=(const CAnimal2 &other)    {        mGroup = other.mGroup;        cout << "CAnimal2 operator=" << endl;        return *this;    }    private:    int mGroup;};class CDonkey : public CAnimal1{public:    CDonkey(int price) : CAnimal1(4), mPrice(price)    {        cout << "CDonkey(" << mPrice << ")" << endl;    }        CDonkey(const CDonkey &other) : CAnimal1(other), mPrice(other.mPrice)    {        cout << "CDonkey(const CDonkey &other)" << endl;    }        ~CDonkey()    {        cout << "~CDonkey()" << endl;    }    public:    CDonkey& operator=(const CDonkey &other)    {        CAnimal1::operator=(other);        mPrice = other.mPrice;        cout << "CDonkey operator=" << endl;        return *this;    }    private:    int mPrice;};class CHorse : public CAnimal2{public:    CHorse(int price) : CAnimal2(5), mPrice(price)    {        cout << "CHorse(" << mPrice << ")" << endl;    }        CHorse(const CHorse &other) : CAnimal2(other), mPrice(other.mPrice)    {        cout << "CHorse(const CHorse &other)" << endl;    }        ~CHorse()    {        cout << "~CHorse()" << endl;    }    public:    CHorse& operator=(const CHorse &other)    {        CAnimal2::operator=(other);        mPrice = other.mPrice;        cout << "CHorse operator=" << endl;        return *this;    }    private:    int mPrice;};class CMule : public CDonkey, public CHorse{public:    CMule() : CHorse(800), CDonkey(500)    {        cout << "CMule()" << endl;    }        CMule(const CMule &other) : CHorse(other), CDonkey(other)    {        cout << "CMule(const CMule &other)" << endl;    }        ~CMule()    {        cout << "~CMule()" << endl;    }    public:    CMule& operator=(const CMule &other)    {        CHorse::operator=(other);        CDonkey::operator=(other);        cout << "CMule operator=" << endl;        return *this;    }};void multiDerived(){    CMule mule;    CMule otherMule = mule;    otherMule = mule;}
output:
CAnimal1(4)CDonkey(500)CAnimal2(5)CHorse(800)CMule()CAnimal1(const CAnimal1 &other)CDonkey(const CDonkey &other)CAnimal2(const CAnimal2 &other)CHorse(const CHorse &other)CMule(const CMule &other)CAnimal2 operator=CHorse operator=CAnimal1 operator=CDonkey operator=CMule operator=~CMule()~CHorse()~CAnimal2()~CDonkey()~CAnimal1()~CMule()~CHorse()~CAnimal2()~CDonkey()~CAnimal1()

多父类继承自同一父类

class CAnimal{public:    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(int price) : CAnimal(4), mPrice(price)    {        cout << "CDonkey(" << mPrice << ")" << endl;    }        CDonkey(const CDonkey &other) : CAnimal(other), mPrice(other.mPrice)    {        cout << "CDonkey(const CDonkey &other)" << endl;    }        ~CDonkey()    {        cout << "~CDonkey()" << endl;    }    public:    CDonkey& operator=(const CDonkey &other)    {        CAnimal::operator=(other);        mPrice = other.mPrice;        cout << "CDonkey operator=" << endl;        return *this;    }    private:    int mPrice;};class CHorse : public CAnimal{public:    CHorse(int price) : CAnimal(5), mPrice(price)    {        cout << "CHorse(" << mPrice << ")" << endl;    }        CHorse(const CHorse &other) : CAnimal(other), mPrice(other.mPrice)    {        cout << "CHorse(const CHorse &other)" << endl;    }        ~CHorse()    {        cout << "~CHorse()" << endl;    }    public:    CHorse& operator=(const CHorse &other)    {        CAnimal::operator=(other);        mPrice = other.mPrice;        cout << "CHorse operator=" << endl;        return *this;    }    private:    int mPrice;};class CMule : public CDonkey, public CHorse{public:    CMule() : CHorse(800), CDonkey(500)    {        cout << "CMule()" << endl;    }        CMule(const CMule &other) : CHorse(other), CDonkey(other)    {        cout << "CMule(const CMule &other)" << endl;    }        ~CMule()    {        cout << "~CMule()" << endl;    }    public:    CMule& operator=(const CMule &other)    {        CHorse::operator=(other);        CDonkey::operator=(other);        cout << "CMule operator=" << endl;        return *this;    }};void multiDerived(){    CMule mule;    CMule otherMule = mule;    otherMule = mule;}
output:
CAnimal(4)CDonkey(500)CAnimal(5)CHorse(800)CMule()CAnimal(const CAnimal &other)CDonkey(const CDonkey &other)CAnimal(const CAnimal &other)CHorse(const CHorse &other)CMule(const CMule &other)CAnimal operator=CHorse operator=CAnimal operator=CDonkey operator=CMule operator=~CMule()~CHorse()~CAnimal()~CDonkey()~CAnimal()~CMule()~CHorse()~CAnimal()~CDonkey()~CAnimal()

总结

  • 父类们的构造顺序跟子类继承父类们的顺序一致,与子类初始化式中的父类们顺序无关
  • 父类们即使继承自同一父类,每个父类在子类中保存一份copy
  • 析构顺序总是构造顺序的反序
0 0
原创粉丝点击