In the previous exercise we have been using the inheritance of public, that is, shared inheritance, and we are not talking about protected and private inheritance, that is, protection of inheritance and privacy.
For a single class, it is not important to discuss the difference between protection inheritance and private inheritance, and their differences are reflected only in the case of multilevel inheritance.
Here I declare that the content of this chapter is not appropriate to use too much text to describe, mainly to see examples, through the familiar relationship between the examples, too much text description will blur the reader's thinking.
The routines are as follows (the important parts are described in detail):
//Program Author: Junning
//site: www.cndev-lab.com
//All manuscripts are copyrighted, if you want to reprint, please be sure that the famous source and author
#include <iostream>
using namespace std;
class Base
{
public://Public
int A1;
virtual void Test () = 0;
protected://protected
int A2;
private://Private
int A3;
};
//------------------------------------------------------------------------------
class protectedclass:protected base//protection inheritance
{
Public:
void Test ()
{
A1 = 1;//a1 is converted here to protected
A2 = 2;//a2 was converted here to protected
//a3=3;//error, the derived class cannot access the private members of the base class
}
};
class Controlprotectedclass:public protectedclass//inherits Protectedclass class by public
{
Public:
void Test ()
{
A1 = 1;//a1 here still remains for A1 here is converted to protected
A2 = 2;//a2 Here still remains for A1 here is converted to protected
//a3=3;//Error, the control type of the base class member cannot be changed because the base class member is private, even if the parent class is a protected inheritance
}
};
//------------------------------------------------------------------------------
class Privateclass:private base//private inheritance
{
Public:
void Test ()
{
A1 = 1;//a1 is converted here to private
A2 = 2;//a2 is converted here to private
//a3=3;//Error, the base class private member is inaccessible to the file area and the derived class area
}
};
class Controlprivateclass:public privateclass//inherits Privateclass class by public
{
Public:
void Test ()
{
//a1=1;//Error, because the base class Privateclass to private inheritance, A1 has been converted to private
//a2=2;//Error, because the base class Privateclass to private inheritance, A1 has been converted to private
//a3=3;//Error, because the base class member is private, the Privateclass class is also private inheritance
}
};
//------------------------------------------------------------------------------
class Publicclass:public base//There are differences and other forms of inheritance, and the members of the succession do not change their control mode
{
Public:
void Test ()
{
A1 = 1;//a1 remains public
A2 = 2;//a2 still remain protected
a3=3;//error, derived class cannot manipulate private members of base class
}
};
class Controlpublicclass:public publicclass//inherits Publicclass class by public
{
Public:
void Test ()
{
A1 = 1;//a1 remains public
A2 = 2;//a2 still remain protected
//a3=3;//Error, because the base class member is a private member, the control type of the base class member cannot be changed even if the parent class is public inheritance
}
};
//------------------------------------------------------------------------------
int main ()
{
System ("pause");
}
Carefully read the example, I believe that careful readers for the common inheritance, protection and private inheritance of the differences and characteristics have been understood, and finally remind readers that in the inheritance relationship, the private members of the base class are not only hidden from the application, even if the derived class is also hidden inaccessible, While the protected members of the base class are hidden from the application and are not hidden from the derived classes, protection of inheritance and private inheritance is extremely rare in practical programming, and they are only technically meaningful.