C ++ primer Reading Notes 11-Polymorphism

Source: Internet
Author: User

C ++ primer Reading Notes 11-Polymorphism
Polymorphism is also an important aspect in C ++. polymorphism and dynamic type. virtual functions are essentially the same thing.


1. Virtual Functions
Adding the virtual surface before the member function prototype in the class is a virtual function. The purpose of a virtual function is to redefine the function in the derived class that inherits it, so that the function of the derived class can be called at runtime through the pointer or reference of the base class.


2. Derived classes and virtual functions
In general, you need to redefine the inherited virtual functions of a derived class. There are several considerations.
<1> the declaration of a virtual function must be exactly the same as that of the function declaration prototype in the base class. The exception is that when the base class returns a pointer or reference of the base class, A derived class can be a pointer or reference of a derived class type.
<2> it doesn't matter if you add the virtual keyword, but you need to add it. You can see that this is a virtual function.
<3> once a virtual tag is added before a function in the base class, it will never be removed.


3. Trigger of dynamic binding
C ++ function calls do not use dynamic binding by default. Using dynamic binding requires two required conditions.
<1> call a function through a pointer or reference of the base class type
<2> the function is a virtual function.
It can also be noted that when a function is called through a pointer or reference, even if the derived class has redefined the base class version, the version of the derived class is not called because it does not trigger polymorphism.


4. overwrite the virtual function
Sometimes you do not want to trigger polymorphism through pointers or references. In this case, you can explicitly call the base class functions by adding a qualifier.

Derived d;Base *p = &d;p->Base::func();
If you want to call a function with the same name as a base class when defining a function in a derived class, you must call the function explicitly. Otherwise, infinite recursion is triggered.


5. virtual functions and replication Control
<1> usually, you can set the destructor to a virtual function, because the Destructor only executes its own part during execution. For example, if you use a base class pointer or reference to call the destructor, only the base class constructor will be called, and the constructor of the derived class will not be called.
<2> constructor cannot be defined as a virtual function, because the constructor runs before the object is fully constructed. during the execution of the constructor, the dynamic type of the object is incomplete.
<3> the assignment operator can be set to a virtual function in principle, but it does not make sense. The reason is as follows:
The virtual function requires that the function prototype be completely consistent, which includes the consistency of the function parameter types. If we set operator = to a virtual function, in the base class, function parameters are of the base class type, and the parameter type of the virtual function in the derived class is still of the base class type. However, the value assignment operator requires the function parameter type to be consistent with the class type, which produces very confusing things.
class Base{public:virtual Base& operator =(Base& xx);}class Derived : public Base{public:virtual Derived& operator =(Base& xx);  //virtual functionDerived& operator =(Derived& xx);//real operator =}


6. virtual functions in constructor and destructor
When running constructor and destructor, the object is not a complete class. At this time, the compiler regards the object as changing during the construction, if you call a virtual function in it, the called function will be of the class type.


Defined version.
Cause: when the base class is constructed, the members of the derived class are not initialized. If the virtual function of the derived class is called at this time, the virtual function Member of the derived class will have a problem. The same is true for basic class destructor.


The release of a member of the Data straight pipe. When the derived class calls the destructor, it first calls its own destructor and then calls the destructor of the base class, therefore, the destructor of the base class cannot call the virtual function of the derived class, because the members of the derived class have been released.


. Example:
// Base. h # pragma once # include
 
  
Using namespace std; class Base {public: Base (void) {func ();};~ Base (void) {funp () ;}; virtual void func () {cout <"this is in Base constructor" <
  
   
// Derived. h # pragma once # include "base. h" class Derived: public Base {public: Derived (void) {func ();}~ Derived (void) {func ();} virtual void func () {cout <"this is in Derived constructor" <
    
     
//main.cpp#include "Derived.h"void main(){Base X;Derived Y;}

7 pure virtual functions
The parameter followed by "= 0" indicates the pure virtual function. Note the following when using pure virtual functions:
<1> pure virtual functions do not have function bodies;
<2> the final "= 0" does not indicate that the return value of the function is 0. It only plays a formal role and tells the compilation system that "this is a pure virtual function ";
<3> This is a declaration statement and should end with a semicolon.
<4> functions cannot be called.
<5> you cannot create class objects with pure virtual functions.
<6> If a pure virtual function is declared in a class but is not defined in its derived class, the virtual function is still a pure virtual function in the derived class.


Classes that contain one or more pure virtual functions are called abstract classes. abstract classes cannot create objects. Abstract classes provide basic functional interfaces and are then implemented by Derived classes. On this basis, polymorphism is implemented. However, an abstract class can be used as a reference or pointer. The instance program is as follows:
//Base.h#pragma once#include 
      
       using namespace std;class Base{public:Base(void){ func();};~Base(void){ funp();};virtual void func(){cout<<"this is in Base constructor"<
       
        
//Derived.h#pragma once#include "base.h"class Derived :public Base{public:Derived(void){func();}~Derived(void){func();}virtual void func(){cout<<"this is in Derived constructor"<
         
          

//main.cpp#include "Derived.h"void main(){Derived Y;Base& X = Y;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.