Contents
- 6.1 Basic Inheritance Syntax
- 6.2 Types of Inheritance
- 6.3 Object Model in Inheritance
- 6.4 Construction and Destruction Order in Inheritance
- 6.5 Handling Members with the Same Name in Inheritance
- 6.6 Handling Static Members with the Same Name in Inheritance
- 6.7 Multiple Inheritance Syntax
- 6.8 Diamond Inheritance
Inheritance is one of the three core features of object-oriented programming
When defining a class, a lower-level class shares the common characteristics of the level above while also having its own characteristics.
6.1 Basic Inheritance Syntax
Purpose: Reduce code duplication
Syntax: class 子类 : 继承方式 父类
Example: class MyPage : public BasePage
A subclass is also called a derived class, and a parent class is also called a base class.
The members of a derived class consist of two main parts: those inherited from the base class and those added by the derived class itself. The inherited members represent its common characteristics, while the newly added members represent its individual characteristics.
6.2 Types of Inheritance
There are three types of inheritance:
- Public inheritance
- Public members of the base class remain public in the subclass.
- Protected members of the base class remain protected in the subclass.
- Private members of the base class cannot be accessed by the subclass.
- Protected inheritance
- Public members of the base class become protected in the subclass.
- Protected members of the base class remain protected in the subclass.
- Private members of the base class cannot be accessed by the subclass.
- Private inheritance
- Public members of the base class become private in the subclass.
- Protected members of the base class become private in the subclass.
- Private members of the base class cannot be accessed by the subclass.

6.3 Object Model in Inheritance
All non-static data members in a base class are inherited by the subclass. Private data members in the base class are hidden by the compiler, so they cannot be accessed, but they are indeed inherited.
//例:
class Base{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son : public Base{
public:
int m_D;
};
void test(){
cout << "size of Son = " << sizeof(Son) << endl;
//输出结果:16
}
6.4 Construction and Destruction Order in Inheritance
After a subclass inherits from a base class, creating a subclass object also calls the base class constructor.
The construction and destruction order in inheritance is as follows:
- Construction: Construct the base class first, then the subclass.
- Destruction: The order is the reverse of construction.
//例:
class Base{
public:
Base(){
cout << "Base的构造函数" << endl;
}
~Base(){
cout << "Base的析构函数" << endl;
}
};
class Son : public Base{
public:
Son(){
cout << "Son的构造函数" << endl;
}
~Son(){
cout << "Son的析构函数" << endl;
}
};
void test(){
Son s;
}
------------
//输出
Base的构造函数
Son的构造函数
Son的析构函数
Base的析构函数
6.5 Handling Members with the Same Name in Inheritance
- To access a member of the subclass that has the same name, access it directly.
- To access a member of the base class that has the same name, add the scope qualifier.
Example: s.Base::m_A or s.Base::func()
//测试案例
#include <iostream>
using namespace std;
class Base{
public:
Base(){
m_A = 100;
}
void func(){
cout << "Base func 函数调用" << endl;
}
int m_A;
};
class Son : public Base{
public:
Son(){
m_A = 200;
}
void func(){
cout << "Son func 函数调用" << endl;
}
int m_A;
};
void test01(){
Son s;
cout << "Son : m_A = " << s.m_A << endl;
cout << "Base : m_A = " << s.Base::m_A << endl;
}
void test02(){
Son s;
s.func();
s.Base::func();
}
int main(){
test02();
system("pause");
return 0;
}
Notes:
If a member function in a subclass has the same name as one in the base class, the same-named member in the subclass hides all same-named member functions in the base class, including overloads. To access a same-named member of the base class, add the scope qualifier.
6.6 Handling Static Members with the Same Name in Inheritance
Static and non-static members with the same name are handled in the same way.
- To access a member of the subclass that has the same name, access it directly.
- To access a member of the base class that has the same name, add the scope qualifier.
#include <iostream>
using namespace std;
class Base{
public:
static int m_A;
static void func(){
cout << "Base - static void func" << endl;
}
};
int Base::m_A = 100;
class Son : public Base{
public:
static int m_A;
static void func(){
cout << "Son - static void func" << endl;
}
};
int Son::m_A = 200;
void test01(){
//通过对象访问
Son s;
cout << "Son - m_A = " << s.m_A << endl;
cout << "Base - m_A = " << s.Base::m_A << endl;
//通过类名访问
cout << "Son - m_A = " << Son::m_A << endl;
//第一个::代表通过类名方式访问 第二个::代表访问父类作用域下的成员
cout << "Base - m_A = " << Son::Base::m_A << endl;
}
void test02(){
//通过对象访问
Son s;
s.func();
s.Base::func();
//通过类名访问
Son::func();
Son::Base::func();
}
int main(){
test02();
system("Pause");
return 0;
}
6.7 Multiple Inheritance Syntax
C++ allows a class to inherit from multiple classes.
Syntax: class 子类 : 继承方式 父类1, 继承方式 父类2...
//例:
class Son : public Base1, public Base2{
If base classes in multiple inheritance contain members with the same name, a scope qualifier must be added when the subclass uses them.
//例:
cout << "Base1 - m_A = " << s.Base1::m_A << endl;
cout << "Base2 - m_A = " << s.Base2::m_A << endl;
6.8 Diamond Inheritance
Concept:
- Two derived classes inherit from the same class.
- Another class inherits from both of these classes.
- This type of inheritance is called diamond inheritance.
Problems with diamond inheritance:
-
Son1 inherits data from Base, and Son2 also inherits data from Base. When GrandSon uses the data, ambiguity occurs.
Solution: Because the two parent classes contain the same data, use scope qualifiers to distinguish between them.
g.Son1::m_A = 100 -
GrandSon inherits two copies of the data from Base, although only one copy is actually needed.
Use virtual inheritance to solve the problem of duplicated data in diamond inheritance. After virtual inheritance is applied, there is only one copy of the data, and it can be accessed directly without a scope qualifier.
Add the keyword virtual before the inheritance declaration. At this point, Base is called a virtual base class.
class Son1 : virtual public Base
Reference: A Meticulously Crafted Course by Heima Programmer | C++ Tutorial: Start at 0 and Advance Through 1, Making Programming Easier to Learn Link: https://www.bilibili.com/video/BV1et411b73Z
![[C++ Notes] Inheritance](https://img.mahaofei.com/img/20220410093451.png)
Comments