Detailed explanation and differences of the three inheritance methods of C++
foreword
I find that sometimes conceptual things are still difficult to understand, so this article uses a few simple examples to illustrate these three different inheritance methods and their previous differences~
1. Public inheritance
-
Base class's
public
andprotected
member's access properties are in derived class保持不变
, but not base class'sprivate
member不可直接访问
. -
public
Member functions in derived classes can directly access and members in the base classprotected
, but cannot directly access the members of the base classprivate
. -
Members inherited from the base class are accessed through objects of the derived class, only
public
the members.
#include<iostream>
using namespace std;
class CFather
{
public:
int m_testA{
0};
protected:
int m_testB{
0};
private:
int m_testC{
0};
};
class CSon: public CFather
{
void test()
{
m_testA = 1; // 编译正确 :public 继承后,在内部或者外部都可以访问public成员
m_testB = 1; // 编译正确 :public 继承后,在内部可以访问protected成员
m_testC = 1; // 编译错误 :无论哪种继承,都无法访问private成员
}
};
int main()
{
CSon _test;
_test.m_testA = 2; // 编译正确 :public 继承后,在内部或者外部都可以访问public成员
_test.m_testB = 2; // 编译错误 :public 继承后,在外部无法访问protected成员
_test.m_testC = 2; // 编译错误 :无论哪种继承,都无法访问private成员
system("pause");
return 0;
}
Two, protected inheritance
- Both the base class's
public
andprotected
membersprotected
appear in the derived class as identity, butprivate
the members of the base class are `not directly`` accessible. public
Member functions in derived classes can directly access and members in the base classprotected
, but cannot directly access the members of the base classprivate
.不能直接访问
A member inherited from a base class through an object of a derived class任何
.
#include<iostream>
using namespace std;
class CFather
{
public:
int m_testA{
0};
protected:
int m_testB{
0};
private:
int m_testC{
0};
};
class CSon: protected CFather
{
void test()
{
m_testA = 1; // 编译正确 :protected 继承后,在内部可以访问public成员
m_testB = 1; // 编译正确 :protected 继承后,在内部可以访问protected成员
m_testC = 1; // 编译错误 :无论哪种继承,都无法访问private成员
}
};
int main()
{
CSon _test;
_test.m_testA = 2; // 编译错误 :protected 继承后,在外部无法访问public成员
_test.m_testB = 2; // 编译错误 :protected 继承后,在外部无法访问protected成员
_test.m_testC = 2; // 编译错误 :无论哪种继承,都无法访问private成员
system("pause");
return 0;
}
At this time, it can be understood that after the derived class protected
inherits the base class, the base class public
becomes protected
, and the others remain the same.
class CFather
{
protected:// proteced 继承之后public变成了 proteced
int m_testA{
0};
protected:
int m_testB{
0};
private:
int m_testC{
0};
};
Three, private inheritance
- Both the base class's
public
and the members' identity appear in the derived class, but the base class's members do not .protected
private
private
不可直接访问
- A member function in a derived class can be a member
直接访问
of the base class , but not a member of the base class .public
protected
不能直接访问
private
不能直接访问
A member inherited from a base class through an object of a derived class任何
.
#include<iostream>
using namespace std;
class CFather
{
public:
int m_testA{
0};
protected:
int m_testB{
0};
private:
int m_testC{
0};
};
class CSon: private CFather
{
void test()
{
m_testA = 1; // 编译正确 :private 继承后,在内部可以访问public成员
m_testB = 1; // 编译正确 :private 继承后,在内部可以访问protected成员
m_testC = 1; // 编译错误 :无论哪种继承,都无法访问private成员
}
};
int main()
{
CSon _test;
_test.m_testA = 2; // 编译错误 :private 继承后,在外部无法访问public成员
_test.m_testB = 2; // 编译错误 :private 继承后,在外部无法访问protected成员
_test.m_testC = 2; // 编译错误 :无论哪种继承,都无法访问private成员
system("pause");
return 0;
}
At this time, it can be understood that after the derived class inherits the base class, the and private
in the base class become , and the others remain the same.public
protected
private
class CFather
{
private:// private 继承之后public变成了 private
int m_testA{
0};
private:// private 继承之后protected变成了 private
int m_testB{
0};
private:
int m_testC{
0};
};
Fourth, the difference between the three
-
public inheritance method
- All members in the base class
public
are attributes in the derived classpublic
; - All members in the base class
protected
are attributes in the derived classprotected
; - All members of the base class
private
cannot be used in the derived class.
- All members in the base class
-
protected inheritance
- All members in the base class
public
are properties in the derived classprotected
; - All members in the base class
protected
are properties in the derived classprotected
; - All members of the base class
private
cannot be used in the derived class.
- All members in the base class
-
private inheritance method
- All members in the base class
public
are properties in the derived classprivate
; - All members in the base class
protected
are properties in the derived classprivate
; - All members of the base class
private
cannot be used in the derived class.
- All members in the base class
The following table summarizes the effects of different inheritance methods on the members of different attributes
Inheritance/base class members | public member | protected member | private members |
---|---|---|---|
public inheritance | public | protected | Invisible |
protected inheritance | protected | protected | Invisible |
private inheritance | private | private | Invisible |
V. Summary
1. Regardless of the inheritance method, the private members in the base class can never be used in the derived class (cannot be accessed or called in the member function of the derived class).
2. If you want the members of the base class to be inherited by the derived class and used without hindrance, then these members can only be declared as public or protected; only those members that you do not want to use in the derived class can be declared as private.
3. If you want the members of the base class to be neither exposed (not accessible through objects) but also used in derived classes, you can only declare them as protected.
4. The access rights of base class members in derived classes must not be higher than those specified in the inheritance method. For example, when the inheritance mode is protected, then the highest access right of the base class members in the derived class is also protected, and those higher than protected will be downgraded to protected, but those lower than protected will not be upgraded. For another example, when the inheritance method is public, the access rights of base class members in derived classes will remain unchanged.
Afterword
What we said here is that the private members of the base class cannot be used in the derived class, and we did not say that the private members of the base class cannot be inherited. In fact, the private members of the base class can be inherited, and (member variables) will occupy the memory of the derived class object, but it is not visible in the derived class, making it unusable. This feature of private members can well hide the implementation of the base class from derived classes to reflect object-oriented encapsulation. Since private and protected inheritance methods will change the access rights of base class members in derived classes, resulting in complex inheritance relationships, we generally use public in actual development.