In the previous exercise, we have been using the public inheritance method, that is, the total inheritance method. For the protected and private inheritance methods, that isProtection inheritanceAndPrivate inheritanceThe method is not discussed.
For a single class, it is of little significance to discuss the differences between protection inheritance and private inheritance. Their differences are only reflected in the multi-level inheritance.
Here I declare that it is not suitable for describing the content of this chapter in too many texts. I will mainly look at the examples to familiarize myself with the relationship between them through examples, too many text descriptions will blur the reader's ideas.
The routine is as follows (the important part is described in detail ):
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use 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 to protected here
A2 = 2; // a2 is converted to protected here
// A3 = 3; // error. The derived class cannot access the private member of the base class.
}
};
Class ControlProtectedClass: public ProtectedClass // inherits the ProtectedClass class in public Mode
{
Public:
Void test ()
{
A1 = 1; // a1 remains as a1 here and is converted to protected here
A2 = 2; // a2 remains as a1 here and is converted to protected here
// A3 = 3; // error. Because the Base class member is private, the control type of the Base class member cannot be changed even if the parent class is to protect inheritance.
}
};
//------------------------------------------------------------------------------
Class PrivateClass: private Base // private inheritance
{
Public:
Void test ()
{
A1 = 1; // a1 is converted to private here
A2 = 2; // a2 is converted to private here
// A3 = 3; // error. The Private Members of the base class are not allowed to access the file region and the derived class region.
}
};
Class ControlPrivateClass: public PrivateClass // inherits the PrivateClass class in the public Mode
{
Public:
Void test ()
{
// A1 = 1; // error. Because the base class PrivateClass is private-inherited, a1 has been changed to private.
// A2 = 2; // error. Because the base class PrivateClass is private-inherited, a1 has been changed to private.
// A3 = 3; // error. Because the Base class is private, the PrivateClass class is also private-inherited.
}
};
//------------------------------------------------------------------------------
Class PublicClass: public Base // there is a difference between common inheritance and other inheritance methods. After inheritance, each Member will not change the control mode.
{
Public:
Void test ()
{
A1 = 1; // a1 is still public
A2 = 2; // a2 still maintains protected
// A3 = 3; // error. The derived class cannot operate private members of the base class.
}
};
Class ControlPublicClass: public PublicClass // inherits the PublicClass class in public Mode
{
Public:
Void test ()
{
A1 = 1; // a1 is still public
A2 = 2; // a2 still maintains protected
// A3 = 3; // error. Because the Base class member is a private member, even if the parent class is a public inheritance, the control type of the Base class member cannot be changed.
}
};
//------------------------------------------------------------------------------
Int main ()
{
System ("pause ");
}
After carefully reading the examples, I believe that careful readers have already understood the differences and features of co-occurrence inheritance, protection inheritance, and private inheritance. Finally, I would like to remind readers that in the inheritance relationship, the private member of the base class not only hides the application, but even the derived class is not accessible. The protection Member of the base class only hides the application, and the derived class is not hidden, protection inheritance and private inheritance are rarely used in actual programming work. They are only meaningful in technology theory.