当前位置:网站首页>Detailed explanation of inheritance
Detailed explanation of inheritance
2022-07-22 17:38:00 【Code loving students】
Catalog
1. The concept and definition of inheritance
1.2 The definition of inheritance
2. Base class ( Parent class ) And derived class objects ( Subclass ) Assignment transformation of
3.1 Member variable with the same name
3.2 Member function with the same name
4. Default constructor for derived classes
6. Inheritance and static members
1. The concept and definition of inheritance
1.1 Concept of inheritance
Inherit (inheritance) Mechanism It is object-oriented programming that enables code to Reuse The most important means of , It allows programmers to protect
Expand on the basis of the original class characteristics , Add function , This creates a new class , call Derived class , The original class is called Base class . Inheritance presents the hierarchy of object-oriented programming , It embodies the cognitive process from simple to complex . The reuse we used to contact was function reuse , Inheritance is reuse at the class design level .
1.2 The definition of inheritance
1.2.1 Inherited definition format
1.2.2 Inheritance relationship and access qualifier
There are three kinds of inheritance methods and access qualifiers :
- public
- protected
- private
1.2.3 Inherit the change of access mode of base class members
There are three types of inheritance and access qualifiers , There are nine results of inherited member access methods :
Members of the class | public Inherit | protected Inherit | private Inherit |
The base class public member | Of a derived class public member | Of a derived class protected member | Derived class private member |
The base class protected member | Of a derived class protected member | Of a derived class protected member | Derived class private member |
The base class private member | Not visible in derived classes | Not visible in derived classes | Not in derived classes so |
summary :
1. In the base class private Decorated member , Not visible in derived classes , But this does not mean that it does not inherit in the derived class , It's just that it can't be accessed outside and inside the class .
2. Because the base class is private Decorated members cannot be accessed within derived classes , If you want to access in a derived class , But it is not accessed outside the class , Then you can use protected Decorate members in the base class .
3. You can observe , except private After the access rights of become invisible , The access rights of the rest of the base class members have become the same as The smaller value of the original access permission and inheritance mode permission
4.class The default inheritance method is private Inherit ,struct The default inheritance method is public Inherit
2. Base class ( Parent class ) And derived class objects ( Subclass ) Assignment transformation of
- Objects of derived classes can assign values to objects of base classes 、 The pointer 、 quote , There is a saying that compares this assignment conversion to slicing , The implication is that the part inherited from the parent class will be cut out and assigned
- Derived classes can assign values to base classes , Base classes cannot assign values to derived classes
Assigning a base class to a derived class is not allowed
3. Scope in inheritance
- There are independent scopes in both base and derived classes
- When a base class and a derived class have members with the same name , Derived class members will automatically shield members with the same name in the base class , This situation is called hiding
- If it is a member function with the same name , Functions with the same name in the base class will be hidden , Including overloaded functions
- In practice, try to avoid using members with the same name :
3.1 Member variable with the same name
This situation belongs to hidden Base Medium a member
So how to access Base Medium a What about the members ?
We can go through Scope To let the compiler know which member of the same name to access
3.2 Member function with the same name
When there is a function with the same name between the derived class and the base class , All functions with the same name in the base class will be hidden .
4. Default constructor for derived classes
1. The constructor of the derived class must call the constructor of the base class to initialize that part of the members of the base class . If the base class has no default
Constructor for , The call... Must be displayed during the initialization list phase of the derived class constructor , Equivalent to that the base class is a member of a derived class
If there is no base class, there is no constructor , In the initialization list, you can initialize the base class like initializing its own members
2. The copy constructor of the derived class must call the copy constructor of the base class to complete the copy initialization of the base class , Here we are concerned with the relationship between derived classes and base classes Slice assignment
When assigning values , Because in addition to assigning members of the derived class itself, you also need to assign members of the base class , Therefore, you need to call the assignment of the base class
3. Of a derived class operator= You must call the... Of the base class operator= Complete the assignment of the base class , Notice the hiding of operator overloading
4. When initializing a derived class object, first call the base class constructor, and then call the derived class constructor , Derived class object destruct cleanup first calls derived class destruct and then calls the destruct of base class , Meet the characteristics of stack
Always call the construction of the base class first and then the construction of the derived class , When released contrary Of .
5. The compiler will do special processing for destructor names , Processing into destrutor(), Therefore, without treatment , Destructors of base and derived classes will constitute hidden , Therefore, there is no need to show the destructor that calls the base class , The compiler will automatically call .
If it shows the destructor that calls the base class , The compiler cannot recognize this function , Because it has been completed by the destructor of the derived class, so , If you want to call a function that has been dropped because of this, you need to add a scope :
But when we show how to call the destructor of the base class , We will find that the destructor is called more than once , This is to ensure that derived classes are destructed first , So when the derived class is destructed, it will Automatically call The destructor of the base class , Therefore, there is no need to show the destructor that calls the base class .
5. Inheritance and friends
Friend relationships cannot be inherited , It's like your father's friend is not necessarily your friend .
6. Inheritance and static members
Because static members do not exist in objects , So when a derived class inherits all the members of the base class , Except that static members are the only one , The rest can be inherited indefinitely .
The figure also well reflects , All objects inherit the same static member variable count
7. diamond inheritance
Single inheritance : When a derived class has only one direct base class, this inheritance relationship is called single inheritance
Multiple inheritance : When a derived class directly inherits two or more base classes, this relationship is called multiple inheritance
So what is diamond inheritance ?
Diamond inheritance is actually a special case of multi inheritance
Here we can see , There are two in our class string, And we just need one , This is the problem of diamond inheritance :
have Redundancy and ambiguity .
How should we solve this problem ?
We only need to add virtual Key words can be used
We can find it in student and teacher Class objects no longer exist string Members , But into vbptr The pointer to , This pointer points to a named vbtable Virtual base table of in , The table stores the offset of the pointer , And the pointer Current position plus offset Just equal to the only one string Member's address , So this solves redundancy and ambiguity , There is only one in the whole object string
边栏推荐
- Solidworks基础特征你了解多少?| 拉伸特征的4种方法
- [reprint] UE4 interview Basics (I)
- pytorch
- Sparse array (sparse)
- Wechat marketing strategy, what are the advantages of wechat marketing?
- Win11 terminal administrator cannot open the solution
- How much do you know about the basic features of SolidWorks| Four methods of extruding features
- 指令安排问题
- 广度优先遍历(Breath First Search)
- sqlmap的打开方式以代码的形式打开不是以图像形式打开
猜你喜欢
李宏毅机器学习2020--P20&21 RNN
Sparse array (sparse)
稀疏数组(sparse)
PlayBook introduction
DOM operation of JS - event chain (bubble target capture)
Infrared remote control of FPGA
Sql语言(基础一)
NFS shared storage service
深度解决npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
Graduation thesis on production line balance optimization [Flexsim simulation]
随机推荐
Niuke net brush question 1 [Fibonacci sequence of minimum steps]
This competition is a bit against the sky! Kunpeng application innovation competition openeuler track is fully opened
Li Hongyi machine learning 2020--p20 & 21 RNN
mysql约束之_自增长约束_auto_increment
"New capabilities" of rongyun Super Group
What if win11 encounters a problem and needs to restart?
Unity3d GameObject component
[learning notes, dog learning C] deeply understand arrays and pointers
大佬们,flink sql job往yarn提交作业,报不能执行sql错误,本地执行不报错,服务器
关键路径问题
会议OA项目之项目简介&会议发布
pytorch
On the dilemma faced by non transferable reputation points NFT SBTS
信息安全CISP认证-大家关心哪些问题?
ACL and net
Openeuler is ambitious, open source huizhichuang future | 2022 open atom global open source summit openeuler sub forum is about to open
Instruction arrangement problem
Problems in CPD registration
[external sorting] fast sorting idea completes external sorting
Real time synchronization and conversion of massive data based on Flink CDC