当前位置:网站首页>What is paternity and diamond inheritance
What is paternity and diamond inheritance
2022-04-21 08:23:00 【Add chicken legs tonight】
Catalog
One : Subclass ( Derived class ) The composition of the
Two : Subclass ( Derived class ) And the father ( Base class ) The relationship between
3、 ... and : Construction and deconstruction sequence
Preface
Father and son , Just look at the name , The relationship between parent and child classes
Father and son
One : Subclass ( Derived class ) The composition of the
1、 Construct all member variables and member functions except destructors ( Because there is a minimum guarantee for both structure and Analysis , If you don't write, the system will give you , What's the use of your inheritance )
2、 You can add new members ( Add something to the parent class )
3、 The member name added in the derived class and the member name of the base class can be the same , But generally not ( Because the name is the same , There will be problems , The member name of the base class with the same name is hidden , And the function of derived classes )
But it's just hidden , But that doesn't mean you can't access
If you visit, you can use Base class name :: Base class member name
In this way, you can access the base class member with the same name
Two : Subclass ( Derived class ) And the father ( Base class ) The relationship between
Derived class objects are base class objects , But the base class object is not a derived class object .( You can remember : The son was born to his father , therefore
A son should be part of his father , So the derived class object is the base class object , The father must not have been born to his son , So the base class object is not a derived class object )
It's understandable : A derived class is an object of a subclass , It can be used as a base class object , There's no problem with this , Because the subclass inherits all the members of the parent class , It inherits everything that can be used , Then the parent class can do things , It can do everything , So it can be used as a parent object , Why can't parent objects be subclasses , Because subclasses have their own new development , Subclasses can do things , The parent class may not be able to do , So not
The code for ( Please combine 《 What is inheritance ( A look at will understand ) Code to read )
int main()
{
Father obj_fa;
Son obj_son;
obj_fa = obj_son;// Parent objects can accept child objects ( It can be understood that the parents have a big mind , Don't quarrel with small , Sure
// Accept )
//obj_son=obj_fa;// Subclass objects cannot receive parent objects ( The small can't accept the big , Prejudice against parents , incomprehension )
Father*p_fa = NUll;
Son*p_son = NUll;
p_fa = &obj_fa;// Basic operation : The pointer of the corresponding type stores the first address of the object
p_son = &obj_son;// Basic operation : The pointer of the corresponding type stores the first address of the object
p_fa = &obj_son; .// A parent class pointer can point to a child class object ( You can remember like this : The big one means the small one , Small cannot mean big )
//p_son=&obj_fa;// A subclass pointer cannot point to a parent class object
return 0;
}
3、 ... and : Construction and deconstruction sequence
1、 When a derived class object is instantiated, it will call the constructor of the base class ( As long as you create subclass objects , The parent class must have a constructor to be called , You must have a father before you can have a son , Put it in the program , Subclass data is inherited from the parent class , So when you want to initialize these data members of the subclass , Some of its data members are inherited from the parent class , You must initialize the part of the parent class first , Then initialize your own , So how to initialize parent class members , Of course, use the constructor to initialize , Can you write one yourself , To create a subclass object, you must call the constructor of the parent class , Of course, your own will also call , When did you use it , In fact, it is called when the subclass object member initializes the list , You have to before you create your subclass object , First, the part of the parent class is constructed through the constructor )
2、 If it is multi inheritance, it will call... In the order in which the base class is created , Which base class did you create first , Which one do you call first
3、 When implementing inheritance , Make sure that the base class has a constructor that can be called
The code for :
Son::Son(int v, int n)
:FA(), Father(v)// No matter who is in front when the base class order list is initialized , Who created the base class first , The constructor is called first
{
}
Four : diamond inheritance

One : The basic structure
1、 class A There are two subclasses : class A_1 And the class A_2
2、 class A_1 And the class A_2 Jointly derived classes AA
Two : Problem analysis
1、 class AA Inherit two classes A Members of
2、 Cannot explicitly access classes inherited from A Members of
3、 ... and : Solution
1、 Class name :: Member name Treat the symptoms, not the root cause ( Because the memory is still repeated , Besides, you don't need two copies of the same thing at all , Memory
Still wasted , And it will also appear in the process of use , Your class name plus scope character plus member name , Code more , Your class name
It's easy to get wrong , Then there will be problems , The data is not accurate , There will be confusion , This method can be solved, but it's not good )
2: Virtual inheritance as the medicine took effect , the symptoms lessened A radical cure ( It can solve the above two problems , In the final analysis, these two problems still appear in two
The same memory , Resulting in a waste of
Precede the inheritance method with keywords virtual( It can eliminate the problem of memory duplication )
The code for :
class A_1 virtual public A
{
int a1;
};
class A_2 virtual public A
{
int a2;
};
summary
For programmers , Don't do what you can't do
版权声明
本文为[Add chicken legs tonight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210749191084.html
边栏推荐
- Configure multiple SSH keys
- 容联七陌助力知名新零售品牌,重塑企业增长新链路
- Brief introduction of 6 screenshots of win10
- redis的bitmap能设置最大的长度是多少, 为什么可以设置的最大长度位数是2^32, 怎么计算bitmap会占用多大的空间
- Kotlin project cannot run after IntelliJ update
- 机器学习之线性回归初窥
- File转换成MultiPartFile
- 【转载】深入浅出的讲解傅里叶变换
- Be sure to watch the nine steps of MES selection, which is worth collecting and watching repeatedly (Part 2)
- Hierarchical traversal of binary tree
猜你喜欢

MySQL creates a new table

pip安装成功,但是pycharm报错的问题

Ehcart map

第六章 关联查询

Add a cute girl to your website

【转载】深入浅出的讲解傅里叶变换

Arm64 architecture programming and practice: arithmetic and shift instructions

Kernel PWN learning (1) -- environment construction

Dapr | 云原生的抽象与实现

PIP was installed successfully, but pycharm reported an error
随机推荐
[reprint] explain Fourier transform in simple terms
JVM——》常用参数
在链表结点前插入新结点
ThinkPHP quick start (III)
Kernel PWN learning (2) -- kernel UAF & ciscn2017 - babydriver
Prometheus 基础知识脑图
一种带开关量的开口式电流互感器的应用
给自己的网站添加一个可爱的妹子
【转载】深入浅出的讲解傅里叶变换
千万别贪便宜,免费的MES系统可不一定好
最近发现百度云分享都要设置有提取码, 无法设置为无提取码的分享.本文将教你怎么绕过百度设置无提取码的分享(即公开的), 一行代码搞定!
Class loading and class loader overview
knn预测最小案例总结
Be sure to watch the nine steps of MES selection, which is worth collecting and watching repeatedly (Part 2)
卷积神经网络中二维卷积核与三维卷积核有什么区别?
Webapi (VI) - BOM
链表结点的删除
Deletion of linked list nodes
第六章 关联查询
IDEA中dbug调试图标解释
