Re-understanding of the Three Inheritance Modes in C++.

C++ has three forms of inheritance:

1:public inheritance (most commonly used)

2:protected Protects Inheritance

private Private Inheritance

(1) public inheritance:

Parent (base) class member access propertiesSubclass (derived class) member access properties
private Memberscannot access
protected memberprotected
public memberspublic
class Base
{
public:
	int m_Age;
public:
	void Basepublicfunc() { cout << "This is Basepublicfunc Call of function!" << endl; }
protected:
	string m_Name;
	void Baseprotectedfunc() { cout << "This is Baseprotectedfunc Call of function!" << endl; }
private:
	int m_GrilFriendNumber = 1000;
	void Baseprivatefunc() { cout << "This is Baseprivatefunc Call of function!" << endl; }
};

class Son :public Base
{
public:

};
void test()
{

	Base b1;
	b1.Basepublicfunc();
	b1.Baseprotectedfunc();//Error! The protected property of the parent class is not accessible outside of its class!
	b1.Baseprivatefunc();//Error! The private attribute of the parent class is not accessible outside of its class!
	cout<<b1.m_Age<<endl;
	cout << b1.m_Name << endl;//Error! The protected property of the parent class is not accessible outside of its class!
	cout << b1.m_GrilFriendNumber << endl;//Error! The private attribute of the parent class is not accessible outside of its class!
	Son s1;
	s1.Basepublicfunc();
	s1.Baseprotectedfunc();//Error! The protected property of the parent class is not accessible outside of its class!
	s1.Baseprivatefunc();//Error! The private attribute of the parent class is not accessible outside of its class!
}

The compiler of VS2019 will now give the following red error alerts:

With the above code, we can clearly know that under public inheritance, subclasses are members that cannot access the protected and private attributes of the parent class! Subclasses can only access the public members of the parent class!

(2) Under protected Inheritance:

Parent (base) class member access propertiesSubclass (derived class) member access properties
private Memberscannot access
protected memberprotected
public membersprotected

Following the above code, change Son's inheritance pattern to protected, that is, class Son: protected Base

The compiler of VS2019 will now give the following red error alerts:

   

With the above code, we can clearly understand that under protected inheritance, subclasses are members of public, protected, and private attributes that do not have access to the parent class! That is, subclasses do not have access to any members of the parent class!

(3) Under private inheritance:

Parent (base) class member access propertiesSubclass (derived class) member access properties
private Memberscannot access
protected memberprivate
public membersprivate

Still following the code above, change Son's inheritance to private, also known as class Son: private Base

The compiler of VS2019 will now give the following red error alerts:

   

With the above code, it is clear that subclasses are also members of the public, protected, and private attributes of the parent class that cannot be accessed under private inheritance! That is, subclasses do not have access to any members of the parent class!

In summary, I should understand which attributes of the parent class members can be invoked when subclasses use these three forms of inheritance. But since we often use inheritance and then use polymorphism, we can use the keyword virtual to help me solve the problems caused by these inheritance rules!

See the following code below:

class Base
{
public:
	int m_Age;
public:
	virtual void Basepublicfunc() { cout << "This is Basepublicfunc Call of function!" << endl; }
protected:
	string m_Name;
	virtual void Baseprotectedfunc() { cout << "This is Baseprotectedfunc Call of function!" << endl; }
private:
	int m_GrilFriendNumber = 1000;
	virtual void Baseprivatefunc() { cout << "This is Baseprivatefunc Call of function!" << endl; }
};

class Son :public Base 
// Or class Son: protected Base or class Son: private Base results are the same!
{
public:
	virtual void Basepublicfunc() { cout << "This is Sonpublicfunc Call of function!" << endl; }

	virtual void Baseprotectedfunc() { cout << "This is Sonprotectedfunc Call of function!" << endl; }

	virtual void Baseprivatefunc() { cout << "This is Sonprivatefunc Call of function!" << endl; }

};
void test()
{

	Base b1;
	b1.Basepublicfunc();
	//b1.Baseprotectedfunc();//Error! The protected property of the parent class is not accessible outside of its class!
	//b1.Baseprivatefunc();//Error! The privateattribute of the parent class is not accessible outside of its class!
	//cout<<b1.m_Age<<endl;
	//Cout << b1.m_Name << endl;//Error! The protected property of the parent class is not accessible outside of its class!
	//Cout << b1.m_GrilFriendNumber << endl;//Error! The private attribute of the parent class is not accessible outside of its class!
	Son s1;
	s1.Basepublicfunc();//Correct!
	s1.Baseprotectedfunc();//Correct! Members of the protected property of the parent class are overridden in their subclasses through the virtual keyword, so they can be accessed!
	s1.Baseprivatefunc();//Correct! Members of the private attribute of the parent class are overridden in their subclasses through the virtual keyword, so they can be accessed!
}

At this point, no matter which of the three inheritance modes you use, the VS2019 compiler will not give any red error alerts! That is, the correct usage!

Of course, the design grammar of virtual base classes and virtual classes has been designed here. If I am not familiar with the details of this problem in the future, I will write another blog to learn this knowledge point more effectively.

Note: This short summary is a record of minor issues that I am not familiar with when learning coding, for personal reference only, so that I can read it again later in the learning process to improve my coding ability.

Tags: C++

Posted by xploita on Mon, 20 Sep 2021 18:04:45 +0530