当前位置:网站首页>多重继承与派生类成员标识
多重继承与派生类成员标识
2022-07-29 02:06:00 【编程小程】
单一继承
一个派生类只有一个直接基类的情况称为单一继承(single-inheritance)。
class Person // 人
{
};
class Student: public Person // 学生
{
};
class GStudent: public Student // 研究生
{
};
多重继承或多继承
由多个基类共同派生出新的派生类,这样的继承结构被称为多重继承或多继承(multiple-inheritance)。
在继承树只有一层的情况下,多重继承几乎等同于按顺序单一继承了若干个类。
示例1: 派生类躺椅类继承了基类椅子和床类
class Chair // 椅子
{
};
class Bed // 床
{
};
// 躺椅
class DeckChair : public Bed, public Chair
{
};
菱形继承:
在继承树比较长的情况下,多重继承的情况会很复杂。 ,菱形继承是多继承的一种特殊情况。
示例:公共基类:person,派生类student继承person,GStudent继承student, 派生类 :EMployee 继承 preson
派生类 :EGStudent 继承了GStudent 和 EMployee
派生类对象的构造过程
class Person // 人
{
private:
int _idPerson;
public:
Person(int id) :_idPerson(id) {
cout << "Create Person" << endl; }
};
class Student : public Person // 学生
{
private:
int _snum;
public:
Student(int s, int id) :Person(id), _snum(s) {
}
};
class GStudent : public Student // 研究生
{
private:
int _gsnum;
public:
GStudent(int g, int s,int id) :Student(s,id),_gsnum(g) {
}
};
// 教职工
class Employee : public Person
{
private:
int _enum;
public:
Employee(int e, int id) :Person(id), _enum(e) {
}
};
class EGStudent : public GStudent, public Employee
{
private:
int _egsnum;
public:
EGStudent(int es, int g, int s, int sid, int e, int eid)
:GStudent(g, s, sid),
Employee(e, eid) ,
_egsnum(es) {
}
};
int main()
{
EGStudent egs(1, 2, 3, 4, 5, 6);
return 0;
}
数据冗余和二义性
两个身份证号从逻辑上讲应是一回事,但是物理上是分配了不同内存空间,是两个变量。
对象的内存分布图
虚基类:
数据冗余和二义性的解决办法:
两个身份证号显然是不合理的。可以把class Person这个共同基类设置为虚基类,这样从不同路径
继承来的同名数据成员在内存中就只有一个拷贝,同名函数也只有一种映射。
虚基类(virtual base class)定义方式如下:
class 派生类名:virtual 访问限定符 基类类名{…};
class 派生类名:访问限定符 virtual 基类类名{…};
virtual 关键字只对紧随其后的基类名起作用:
class Person // 人
{
private:
int _idPerson;
public:
Person(int id) :_idPerson(id) {
cout << "Create Person" << endl; }
};
class Student : public virtual Person // 学生
{
private:
int _snum;
public:
Student(int s, int id) :Person(id), _snum(s) {
}
};
class GStudent : public Student // 研究生
{
private:
int _gsnum;
public:
GStudent(int g, int s, int id) :Student(s, id), _gsnum(g),Person(id) {
}
};
// 教职工
class Employee : public virtual Person
{
private:
int _enum;
public:
Employee(int e, int id) :Person(id), _enum(e) {
}
};
class EGStudent : public GStudent, public Employee
{
private:
int _egsnum;
public:
EGStudent(int es, int g, int s, int id, int e)
:GStudent(g, s, id),
Employee(e, id),
Person(id),
_egsnum(es)
{
}
};
int main()
{
EGStudent egs(1, 2, 3, 4, 5);
Person *p = ⪖
return 0;
}
虚继承中派生类对象的构造过程:
在派生类对象的创建中,首先是虚基类的构造函数并按它们声明的顺序构造。第二批是非虚基类的构
造函数按它们声明的顺序调用。第三批是成员对象的构造函数。最后是派生类自己的构造函数被调用
菱形虚拟继承的egs对象的内存分布图:
物理内存:
总结:
有了多继承,就存在菱形继承,有了菱形继承就有菱形虚拟继承,底层实现就很复杂。所以一般不
建议设计出多继承,不要设计出菱形继承。否则在复杂度及性能上都有问题。
边栏推荐
- HTTP cache
- The outsourcing company "mixed" for two years, and I only did five things seriously. Now I get byte offer smoothly.
- 如果非要在多线程中使用 ArrayList 会发生什么?
- 详解JS的四种异步解决方案:回调函数、Promise、Generator、async/await
- 云开发打工人必备上班摸鱼划水微信小程序源码
- Explain asynchronous tasks in detail: task status and lifecycle management
- 新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版
- [quality] code quality evaluation standard
- Source code and display of 18 classic programs in C language vs2019
- [mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages
猜你喜欢
Meeting notice of meeting OA
Understand the evolution of redis architecture in one article
代码实现 —— 多项式的最大公因式(线性代数)
ECCV 2022 | AirDet:无需微调的小样本目标检测方法
Esbuild Bundler HMR
STM32F103 learn the steps and template fool tutorial of 1-keil5 project establishment
4年测试经验,好不容易进了阿里,两个月后我选择了裸辞...
聊聊接口性能优化的11个小技巧
Chapter 3 business function development (deletion and modification of clue remarks)
C语言实现三子棋游戏
随机推荐
ES6 event binding (v-on usage)
ES6 detailed quick start!
Explain the four asynchronous solutions of JS in detail: callback function, promise, generator, async/await
How awesome is the architecture of "12306"?
Cuda-npp image and video processing
新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版
TCP重传机制有哪些?
3d智能工厂工艺流转可视化交互展示应用优点
Understand the evolution of redis architecture in one article
K210——声源定位、声音识别
[upload pictures can be cut-1]
MySQL驱动中关于时间的坑
全新UI四方聚合支付系统源码/新增USDT提现/最新更新安全升级修复XSS漏洞补单漏洞
C language to achieve the three chess game
STM32F103 learn the steps and template fool tutorial of 1-keil5 project establishment
代码实现 —— 多项式的最大公因式(线性代数)
FPGA skimming memory (Verilog implementation of ram and FIFO)
What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
Summary of knowledge points of Engineering Economics
Split, an avalanche caused by connection pool parameters