In public inheritance, the members of the public, private, and protected types of the derived classes can access the public members and protected members of the base class. The objects of the derived classes can access the public members of the base class. when private is inherited, the public, private, and protected member functions of the derived class can access the public and protected members of the base class, however, the object of the derived class cannot access any member of the base class. the Inheritance Method of protected is the same as that of private.
The base class public inherits protected inherits private inheritance.
Public member public protected private
Protected member Protected private
Private member ---------------
// Inherittest. cpp: defines the entry point of the console application.
//
# Include "stdafx. H"
Using namespace STD;
Class
{
Public:
Const static int A = 1; // if it is initialized during definition, it must be declared as const static
Protected:
Const static int B = 2;
PRIVATE:
Const static int c = 3;
};
Class B: public
{
Public:
B ()
{
D = B; // The subclass can internally access the protected member inherited from the parent class.
};
Public:
Int D;
};
Int _ tmain (INT argc, _ tchar * argv [])
{
A * obj1 = new ();
B * obj2 = new B ();
Cout <obj1-> A <Endl;
// Cout <obj1-> B <Endl; // The object cannot directly access the protected member from the outside.
Cout <obj2-> A <Endl;
Cout <obj2-> d <Endl;
// Cout <obj-> C <Endl;
// Getchar ();
Int I;
Cin> I;
Return 0;
}