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
- cpp——多重继承——构造 复制 析构 赋值
- cpp——类——多重继承——构造函数 析构函数 赋值操作符函数
- cpp——多重继承——对象名字查找
- C++学习笔记49——继承中的复制构造函数与赋值操作符
- cpp——类——虚继承——构造函数 析构函数 赋值操作符函数
- cpp——类——多重继承——作用域 名字查找
- 构造,复制,赋值,析构
- Cpp深入:多重继承
- Cpp--改造多重继承
- c++ primer plus 学习笔记(1)——复制构造函数与赋值运算符
- C++基础三剑客—复制构造函数,赋值操作,析构函数
- c++读书笔记——多重继承
- python学习——多重继承
- Python 面向对象 —— 多重继承
- C++ 13 —— 多重继承
- 老师—职工的多重继承
- CPP基础知识—关于菱形继承
- JavaScript面向对象(4)——最佳继承模式(深拷贝、多重继承、构造器借用、组合寄生式继承)
- 洛谷P1122 最大子树和
- codevs1540 银河英雄传说(NOI 2002)
- edclipse cdt
- 学习笔记之JavaSE(6)--Java基础语法5
- 手势GestureOverlayView源码详解
- cpp——多重继承——构造 复制 析构 赋值
- java——final
- maven项目 关于Eclipse报:" cannot be read or is not a valid ZIP file"的解决方案
- Linux系统调用列表
- Java二进制,原码、反码、补码、位运算
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
- Android手势源码浅析------手势的形成(Gesture)
- MVC
- NOIP 2013 货车运输