There are 4 Main features of object-oriented programming: abstraction, encapsulation, inheritance and polymorphism.
9.1 Concepts of inheritance and derivation
Base class A |
Cannot be inherited part |
Can be inherited part |
following Bearing
Derived
Derived class B |
Inherit part from base class |
Self-added part |
Note:
The inheritance arrows are pointed by B to a derived arrow from A to B
Class B inherits Class a(or class b is derived from class a ). Class B inherits all members except constructors and destructors from class A, and adds new properties and operations. class A is called the base or parent class B, and class b is called Class a The derived class.
Implementation of 9.2 inheritance 9.2.1definition of a derived class
The general way to define a derived class is
Class Derived classes name: Inheritance Way base class 1, inheritance way base class 2,... , inheritance mode base class N
{
New added member declarations for derived classes
}
For single-inheritance cases, the definition of a derived class can be simplified to
Class derived class name: Inheritance mode base class name
{
Declaration of new member of derived class
}
class family declaration code (saved in header file shape.h )
9.2.2 composition of derived classes
The members of a derived class include members that inherit the base class and those declared when the derived class is defined.
The derived class adds its own membership, accomplishing two requirements: modifying the base class member and describing the new feature or method.
Derived classes modify the members of the base class by declaring a new member with the same name as the base class member in the derived class. That is, overwrite with the same name.
Note: If you define a member function with the same name as the base class in a derived class, the function name and the formal parameter list are identical, otherwise they become overloaded functions instead of overriding functions.
9.2.3 inherited access control
C + + provides public inheritance, private inheritance(Private), and protection inheritance(protected ) Three ways, different ways have different access rights, in order to achieve the security and sharing control of data.
1. Public succession
The keyword public, which has the following access control attributes: ① the base class's publicly-owned member, the protected member retains its original access property in the derived class.
The member functions of the ② derived class can access the public and protected members of the base class and cannot access the private members.
A function other than the ③ derived class can access a public member inherited from a base class through the object of the derived class.
2. Protection of inheritance
Protected is a keyword that has the following access control features:
① the public member of the base class, the protected member becomes the protected Access property in the derived class .
The member functions of the ② derived class can access the public and protected members of the base class members and cannot access the private members of the base class members.
A function other than the ③ derived class cannot access a public member inherited from a base class through an object of the derived class.
3. Private inheritance
Private is a keyword that has the following access control features:
The public member of the ① base class, the protected member, becomes the private access property in the derived class .
The member functions of the ② derived class can access the public and protected members of the base class and cannot access the private members of the base class.
A function other than the ③ derived class cannot access a public member inherited from a base class through an object of the derived class.
Inheritance type Access mode |
Public |
Protected |
Private |
Public |
Public |
Protected |
Not accessible |
Protected |
Protected |
Protected |
Not accessible |
Private |
Private |
Private |
Not accessible |
Cover phenomenon:
#include <iostream>using namespace Std;class base{ public:void function () {cout<< "function Ofclass Base "<<endl;}}; Class Deriver:public base{public:void function () {cout<< "Function of class Deriver" <<endl;}}; int main () {deriver derobj;derobj.function (); return 0;}
Inheritance and derivation