Home  >  Article  >  Backend Development  >  Detailed explanation of C++ polymorphic public inheritance

Detailed explanation of C++ polymorphic public inheritance

hzc
hzcforward
2020-07-01 15:33:081816browse

Recommended study: "c Tutorial"

- The behavior of the method should depend on the call to the method object. This more complex behavior is called polymorphism - having multiple forms, i.e. the same method behaves differently depending on the context.

Virtual implements the object to call the correct method or sequence of methods.

Two mechanisms to achieve polymorphism:

  1. #Redefine the method of the base class in the derived class (the base class will not be changed. class method).
  2. Use virtual methods.

# If you use the keyword virtual when declaring a function, these methods are called virtual methods.

  • After a method is declared virtual in the base class, it will automatically become a virtual method in the derived class. However, it is also a good idea to use the keyword virtual in the derived class declaration to indicate which functions are virtual.
class Brass
{
private: ...
public:
  ...
  virtual void ViewAcct() const;
  virtual ~Brass(){} }
 BrassPlus :

The base class declares a fictitious function to ensure that when the derived object is released, the destructor is called in the correct order (see the role of virtual functions later).

Brass dom("Dominic Banker", 11224, 4183.45);
BrassPlus dot("Dorothy Banker", 12118, 2592.00);
dom.ViewAcct();    // use Brass::ViewAcct()dot.ViewAcct();    // use BrassPlus::ViewAcct()

The two ViewAcct() prototypes indicate that there will be 2 independent method definitions. The base class version has the qualified name Brass::ViewAcct(), and the derived class version has the qualified name BrassPlus::ViewAcct().

Calling a method by reference or pointer instead of object:

If the keyword virtual is not used, the program will choose based on the reference type or pointer type Method:

// behavior with non-virtual ViewAcct()// method chosen according to reference typeBrass &b1_ref = dom;
Bross &b2_ref = dot;
b1_ref.ViewAcct();    // use Brass::ViewAcct()b2_ref.ViewAcct();    // use Brass::ViewAcct()

If you want to redefine a base class method in a derived class, you should usually declare the base class method as virtual. This way, the program will choose the method version based on the type of object rather than the type of reference or pointer. It is also common practice to declare a virtual destructor for a base class. Using a virtual destructor ensures that the correct sequence of destructors is called (that is, from the derived class object to the base class object calling their destructors once).

Calling base class methods: In derived class methods, the standard technique is to use the scope resolution operator to call base class methods:

Brass::ViewAcct(); // display base portion

The above is the detailed content of Detailed explanation of C++ polymorphic public inheritance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete