1. The Inheritance methods in C ++ include:
Public, private, and protected (they directly affect the members of the derived class and the access rules of their objects to the base class members ).
(1) Public (Public inheritance): the attributes of all members in the base class remain unchanged during inheritance, and Private Members in the base class are hidden. A member of a derived class can only access the public/protected member in the base class, but not the private member. The object of a derived class can only access the public member in the base class.
(2) Private (private inheritance): When the inheritance is made, the attributes of all members in the base class become private, and the Private Members in the base class are hidden. A member of a derived class can only access the public/protected member of the base class, but cannot access the private member. The object of a derived class cannot access any member of the base class.
(3) protected: When inheriting, the attributes of all members in the base class are changed to protected, and the Private Members in the base class are hidden. A member of a derived class can only access the public/protected member of the base class, but cannot access the private member. The object of a derived class cannot access any member of the base class.
# 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 appliesProgramHidden, even the derived class is invisible, while the protection members of the base class only hide the application, which is not hidden for the derived class, protection inheritance and private inheritance are rarely used in actual programming work. They are only meaningful in technology theory.