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
- cpp——类——多重继承——构造函数 析构函数 赋值操作符函数
- cpp——类——虚继承——构造函数 析构函数 赋值操作符函数
- cpp——类——赋值操作符函数
- C++学习笔记49——继承中的复制构造函数与赋值操作符
- cpp——多重继承——构造 复制 析构 赋值
- C++基础三剑客—复制构造函数,赋值操作,析构函数
- C++——类的构造函数、析构函数与赋值函数用法浅谈
- 对象的生灭——构造函数、拷贝构造函数、赋值构造函数和析构函数
- 继承中,构造函数,赋值操作符,析构函数与虚函数的关系
- 理解构造函数、析构函数、赋值操作符函数不能被继承
- 经典考题——String类的实现(考察构造函数析构函数拷贝构造函数和赋值构造函数)
- C++读书笔记——拷贝构造函数,赋值运算符重载函数,转换构造函数(String类)
- cpp——类——构造函数
- cpp——类——explicit构造函数
- 关注C++细节——含有本类对象指针的类的构造函数、析构函数、拷贝构造函数、赋值运算符的例子
- 类的构造函数 拷贝构造函数 赋值操作符 析构函数
- 字符串类的实现:构造函数、析构函数、复制构造函数和赋值操作符
- C++构造函数、拷贝构造函数、赋值运算符漫谈(三)——NRV
- HTML标签<label></label>标签有什么作用?
- BBED的使用-查看数据文件信息%SID
- Java集合类在实际项目中的应用
- LSTM简介
- 对象流的写入和读出
- cpp——类——多重继承——构造函数 析构函数 赋值操作符函数
- 数据库-内连接与外连接的区别(举例说明)
- HashMap的遍历方法
- Error:(1, 0) Plugin with id 'com.android.application' not found
- arm地址线的链接方式
- 多校训练赛 Furude_Rika and wall 解题报告(dp)
- 在linux和Windows使用curl
- 为什么Linux的fdisk分区时第一块磁盘分区的First Sector是2048?
- HDU5486-Difference of Clustering