Detailed explanation of the three inheritance methods of C++

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 publicand protectedmember's access properties are in derived class 保持不变, but not base class's privatemember 不可直接访问.

  • publicMember functions in derived classes can directly access and members in the base class protected, but cannot directly access the members of the base class private.

  • Members inherited from the base class are accessed through objects of the derived class, only publicthe 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 publicand protectedmembers protectedappear in the derived class as identity, but privatethe members of the base class are `not directly`` accessible.
  • publicMember functions in derived classes can directly access and members in the base class protected, but cannot directly access the members of the base class 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: 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 protectedinherits the base class, the base class publicbecomes 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 publicand the members' identity appear in the derived class, but the base class's members do not .protectedprivateprivate不可直接访问
  • A member function in a derived class can be a member 直接访问of the base class , but not a member of the base class .publicprotected不能直接访问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 privatein the base class become , and the others remain the same.publicprotectedprivate

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 publicare attributes in the derived class public;
    • All members in the base class protectedare attributes in the derived class protected;
    • All members of the base class privatecannot be used in the derived class.
  • protected inheritance

    • All members in the base class publicare properties in the derived class protected;
    • All members in the base class protectedare properties in the derived class protected;
    • All members of the base class privatecannot be used in the derived class.
  • private inheritance method

    • All members in the base class publicare properties in the derived class private;
    • All members in the base class protectedare properties in the derived class private;
    • All members of the base class privatecannot be used in the derived 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.

Guess you like

Origin blog.csdn.net/a924282761/article/details/124352983#comments_27860739