Virtual functions and polymorphism

    Polymorphism in C++ includes:

(1) Compile-time polymorphism (static polymorphism): function overloading, operator overloading, template implementation

(2) Run-time polymorphism (dynamic polymorphism): virtual function implementation

    The concept of virtual function:

    A member function declared virtual in a base class and redefined in one or more derived classes. Usage format: virtual function return type function name (parameter list) {function body}. Acts as manipulating an object through a base class pointer or reference to a derived class.

    Inheritance with virtual function

(1) non-virtual function: you do not want derived class to redefine (override, overwrite) it.

(2) virtual function: you want the derived class to redefine (override, overwrite) it, and you have a default definition for it.

(3) pure virtual function: You hope that the derived class must redefine (override, overwrite) it, and you have no default definition for it. The way to implement a pure virtual function in a base class is to add "= 0" after the function prototype

Use the following program to understand these three functions:

class Shape
{
public:
  virtual void draw() const = 0; //pure virtual function
  virtual void error(const std::string& msg); //virtual function
  int objectID() const; //non-virtual function
  ...
};

class Rectangle:public Shape{...}; //继承
class Ellipse:public Shape{...}; //继承

     Here's a simple example:

using namespace std;

class A
{
public:
  A(){};
  ~A(){};
  void showMe()
  {
    cout << "A" << endl;
  }
};

class B : class A
{
public:
  B(){};
  ~B(){};
  void showMe()
  {
    cout << "B" <<endl;
  }
};

int main(int argc, char const* argv[])
{
  A *p1 = new A;
  A *p2 = new B;
  p1 -> showMe();
  p2 -> showMe();
  
  return 0;
}

    The result of this program running is "A" "A". The problem is that p2 clearly points to the object of class B, but it calls the showMe() function of class A, which is an undesired result. To solve this problem, you need to use virtual functions.

using namespace std;

class A
{
public:
  A(){};
  ~A(){};
  virtual void showMe()
  {
    cout << "A" << endl;
  }
};

class B : class A
{
public:
  B(){};
  ~B(){};
  virtual void showMe()
  {
    cout << "B" <<endl;
  }
};

int main(int argc, char const* argv[])
{
  A *p1 = new A;
  A *p2 = new B;
  p1 -> showMe();
  p2 -> showMe();
  
  return 0;
}

    At this point, run it again, and the output results are "A" and "B". After adding the virtual keyword, the member function showMe() of the base class becomes a virtual function, and naturally the showMe() of the derived class also automatically becomes a virtual function. Whether the virtual keyword needs to be added before the response function of the derived class is all right.

    To sum up: when the base class pointer points to a subclass object, and the function with the same name as the base class in the subclass is called through this pointer, if the base class is declared as a virtual function (the subclass can not be written), the subclass will be called. This function (the concept of overriding), if the base class is not declared as a virtual function, it will call the function of the base class.

    


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325891586&siteId=291194637