Detailed explanation of C++ polymorphic public inheritance

hzc
Release: 2020-07-01 16:31:08
forward
1980 people have browsed it

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.
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Brass
{<br/>private:
    ...<br/>public:<br/>  ...<br/>  virtual void ViewAcct() const;<br/>  virtual ~Brass(){}
}</span>
Copy after login
 BrassPlus :
Copy after login

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()
Copy after login

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()
Copy after login

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:

<span class="cnblogs_code">Brass::ViewAcct(); <span style="color: #008000;">//</span><span style="color: #008000;"> display base portion</span></span>

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!

Related labels:
c++
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template