In C ++, the class inheritance writing method is as follows:
Declaration of a derived class
Class derived class name: Inheritance Method Base Class Name
{
Add a member statement;
};
There are three inheritance Methods: public, private, and protected. The differences are as follows:
Public inheritance:
1. The access attributes of the public and protected members of the base class remain unchanged in the derived class, but the Private Members of the base class cannot be directly accessed.
2. The member functions in the derived class can directly access the public and protected members in the base class, but cannot directly access the Private Members of the base class.
3. Only the public members of the base class can be accessed through the object of the derived class.
Private inheritance:
1. The public and protected members of the base class appear in the derived class as private, but the Private Members of the base class cannot be directly accessed.
2. The member functions in the derived class can directly access the public and protected members in the base class, but cannot directly access the Private Members of the base class.
3. objects from a derived class cannot directly access any member of the base class.
Protected inheritance:
1. Both the public and protected members of the base class appear in the derived class as protected, but the Private Members of the base class cannot be directly accessed.
2. The member functions in the derived class can directly access the public and protected members in the base class, but cannot directly access the Private Members of the base class.
3. objects from a derived class cannot directly access any member of the base class.