当前位置:网站首页>多重继承与派生类成员标识
多重继承与派生类成员标识
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对象的内存分布图:
物理内存:
总结:
有了多继承,就存在菱形继承,有了菱形继承就有菱形虚拟继承,底层实现就很复杂。所以一般不
建议设计出多继承,不要设计出菱形继承。否则在复杂度及性能上都有问题。
边栏推荐
- FPGA skimming memory (Verilog implementation of ram and FIFO)
- HTTP断点续传以及缓存问题
- NPM install reports an error: eperm: operation not permitted, rename
- Eight practical new functions of es2022
- JMeter's BeanShell generates MD5 encrypted data and writes it to the database
- CUDA details GPU architecture
- Esbuild Bundler HMR
- MySQL驱动中关于时间的坑
- 无线振弦采集系统工作流程
- MQTT例程
猜你喜欢

Meeting notice of meeting OA

云开发口袋工具箱微信小程序源码

ES6 detailed quick start!

VR safety training of mine mining virtual reality improves employees' vigilance and protection awareness

h. 264 code stream explanation

CUDA details GPU architecture

ES6 event binding (v-on usage)

3D intelligent factory process flow visualization interactive display application advantages

The outsourcing company "mixed" for two years, and I only did five things seriously. Now I get byte offer smoothly.

Explain the four asynchronous solutions of JS in detail: callback function, promise, generator, async/await
随机推荐
Branch management practice of "two pizza" team
[upload picture 2-cropable]
当Synchronized遇到这玩意儿,有个大坑,要注意
云开发口袋工具箱微信小程序源码
C language to achieve the three chess game
The financing demand of 129 million yuan was released, and the roadshow of the Dake city project continued to irrigate the "good seedlings" of scientific innovation
多年前的回忆
家庭亲戚关系计算器微信小程序源码
漫画算法_小灰灰面试
MQTT例程
fopen、_ Wfopen reads Unicode encoded files
Practice and experience of security compliance in instant messaging scenarios
Meeting notice of meeting OA
Double write consistency of MySQL and redis
Kbxxxxx is not necessarily a patch, but also a description of a solution to a problem
0728~面试题梳理
深度剖析 —— 预处理
手把手教你安装VSCode(附带图解步骤)
关于高并发,我想聊一聊。
PHP幸运抽奖系统带后台源码