当前位置:网站首页>Multiple inheritance and derived class member identification
Multiple inheritance and derived class member identification
2022-07-29 02:45:00 【Programming a small program】
List of articles
Single inheritance
The case where a derived class has only one direct base class is called single inheritance (single-inheritance).
class Person // people
{
};
class Student: public Person // Student
{
};
class GStudent: public Student // Graduate student
{
};
Multiple inheritance or multiple inheritance
A new derived class is derived from multiple base classes , Such an inheritance structure is called multiple inheritance or multiple inheritance (multiple-inheritance).
When the inheritance tree has only one layer , Multiple inheritance is almost equivalent to inheriting several classes in a single order .
Example 1: The derived reclining chair class inherits the base chair and bed class
class Chair // The chair
{
};
class Bed // The bed
{
};
// deck chair
class DeckChair : public Bed, public Chair
{
};
diamond inheritance :
When the inheritance tree is relatively long , Multiple inheritance can be complicated . , diamond inheritance It is a special case of multi inheritance .
Example : Common base class :person, Derived class student Inherit person,GStudent Inherit student, Derived class :EMployee Inherit preson
Derived class :EGStudent Inherited GStudent and EMployee
The construction process of derived class objects
class Person // people
{
private:
int _idPerson;
public:
Person(int id) :_idPerson(id) {
cout << "Create Person" << endl; }
};
class Student : public Person // Student
{
private:
int _snum;
public:
Student(int s, int id) :Person(id), _snum(s) {
}
};
class GStudent : public Student // Graduate student
{
private:
int _gsnum;
public:
GStudent(int g, int s,int id) :Student(s,id),_gsnum(g) {
}
};
// Faculty
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;
}
Data redundancy and ambiguity
Logically speaking, two ID number should be the same , But physically, different memory spaces are allocated , It's two variables .
Memory distribution map of object
virtual base class :
Solutions to data redundancy and ambiguity :
Two ID number are obviously unreasonable . You can put class Person This common base class is set as the virtual base class , So from different paths
The inherited data member with the same name has only one copy in memory , There is only one mapping for functions with the same name .
virtual base class (virtual base class) The definition is as follows :
class Derived class name :virtual Access qualifier Base class name {…};
class Derived class name : Access qualifier virtual Base class name {…};
virtual The keyword only works on the base class name that follows :
class Person // people
{
private:
int _idPerson;
public:
Person(int id) :_idPerson(id) {
cout << "Create Person" << endl; }
};
class Student : public virtual Person // Student
{
private:
int _snum;
public:
Student(int s, int id) :Person(id), _snum(s) {
}
};
class GStudent : public Student // Graduate student
{
private:
int _gsnum;
public:
GStudent(int g, int s, int id) :Student(s, id), _gsnum(g),Person(id) {
}
};
// Faculty
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;
}
The construction process of derived class objects in virtual inheritance :
In the creation of derived class objects , The first is the constructor of the virtual base class and construct it in the order they are declared . The second batch is the construction of non virtual base classes
Build functions are called in the order they are declared . The third batch is the constructor of the member object . Finally, the constructor of the derived class itself is called
Diamond virtual inheritance egs Memory distribution map of object :
Physical memory :
summary :
With more inheritance , There is diamond inheritance , With diamond inheritance, there is diamond virtual inheritance , The underlying implementation is complex . So generally not
It is suggested to design multiple inheritance , Don't design diamond inheritance . Otherwise, there are problems in complexity and performance .
边栏推荐
- 6 years of testing experience, teaching you how to test ~ how to control the project
- How does the Devops team defend against API attacks?
- I was stunned by this question that I browsed 746000 times
- 工程经济学知识点总结
- time_ Wait and close_ Cause of wait
- VR safety training of mine mining virtual reality improves employees' vigilance and protection awareness
- Code implementation - the greatest common factor of polynomials (linear algebra)
- Happy childhood
- 一款好看的iapp捐赠榜单源码
- [untitled]
猜你喜欢
How does the Devops team defend against API attacks?
2022/07/28 学习笔记 (day18) 常用API
Talk about the implementation principle of feign
新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版
3D intelligent factory process flow visualization interactive display application advantages
全新UI四方聚合支付系统源码/新增USDT提现/最新更新安全升级修复XSS漏洞补单漏洞
C language to achieve the three chess game
ROCBOSS开源微社区轻论坛类源码
ES6 event binding (v-on usage)
第七天笔记
随机推荐
This blogger has a comprehensive classification of QT. If you are free, go to study and summarize it and record it.
HTTP断点续传以及缓存问题
ECCV 2022 | airdet: a small sample target detection method without fine tuning
where、having、group by、order by,is null,not in,子查询,delete,日期函数
Shell script quick start-01
线上3d数字展厅制作方案及优点
自动分账系统哪家好?
Code implementation - the greatest common factor of polynomials (linear algebra)
XSS range (II) xss.haozi
owt-server源码剖析(四)--video模块分析之Mixer Out
Implement encapsulated public method global call in laravel framework
In depth analysis - Pretreatment
XSS靶场(二)xss.haozi
When I look at the source code, what am I thinking?
Code random notes_ Hash_ 349 intersection of two numbers
What are the TCP retransmission mechanisms?
Time for white horses to pass the gap
MySQL基本操作和基于MySQL基本操作的综合实例项目
工程经济学名词解释
6年测试经验,教大家测试~如何把控项目