P31-c++ class inheritance-13 chapter summary

1. Chapter 13 Class Inheritance

The content of this chapter includes:

  • Inheritance of is-a relationship.
  • How to derive another class from one class in a public way.
  • Protect access.
  • Initialization list of constructor members.
  • Force conversion up and down.
  • Virtual member function.
  • Early (static) binding and late (dynamic) binding.
  • Abstract base class.
  • Pure virtual function.
  • When and how to use public inheritance.

2. Summary

Inheritance defines a new class (derived class) by using an existing class (base class), making it possible to modify the programming code as needed.
Public inheritance establishes an is-a relationship, which means that derived objects should also be some kind of base objects. As part of the is-a model, the derived class inherits the data members and most of the methods of the base class, but does not inherit the constructor, destructor and assignment operator of the base class.

The derived class can directly access the public and protected members of the base class, and can access the private members of the base class through the public and protected methods of the base class. You can add data members and methods to the derived class, and you can use the derived class as a base class for further development. Each derived class must have its own constructor.

When the program creates a derived class object, it will first call the constructor of the base class, and then call the constructor of the derived class; when the program deletes the object, it will first call the destructor of the derived class, and then call the destructor of the base class.

If you want to use the class as a base class, you can declare the members as protected instead of private, so that the derived class will have direct access to these members. However, the use of private members can usually reduce the possibility of programming problems. If you want the derived class to redefine the method of the base class, you can use the keyword virtual to declare it as virtual. In this way, objects accessed through pointers or references can be processed according to the type of the object, rather than the type of reference or pointer. Specifically, the destructor of the base class should usually be virtual.

You can consider defining an ABC: only the interface is defined, and the implementation is not involved. For example, you can define the abstract class Shape, and then use it to derive concrete shape classes, such as Circle and Square. ABC must contain at least one pure virtual method. You can add =0 before the semicolon in the declaration to declare the pure virtual method.

virtual double area() const = 0;

It is not necessary to define pure virtual methods. For classes that contain pure virtual members, you cannot use it to create objects. Pure virtual methods are used to define
the general interface of derived classes.

Guess you like

Origin blog.csdn.net/sgy1993/article/details/113886450