C++ polymorphic and virtual functions are analyzed in detail

  • 2020-10-23 20:14:09
  • OfStack

Polymorphism literally means multiple forms. Polymorphism is used when there is a hierarchy between classes and the classes are related by inheritance.

C++ polymorphism means that when a member function is called, different functions are executed depending on the type of object calling the function.

In the following example, the base class Shape is derived into two classes, as shown below:


#include <iostream> 
using namespace std;
 
class Shape {
  protected:
   int width, height;
  public:
   Shape( int a=0, int b=0)
   {
     width = a;
     height = b;
   }
   int area()
   {
     cout << "Parent class area :" <<endl;
     return 0;
   }
};
class Rectangle: public Shape{
  public:
   Rectangle( int a=0, int b=0):Shape(a, b) { }
   int area ()
   { 
     cout << "Rectangle class area :" <<endl;
     return (width * height); 
   }
};
class Triangle: public Shape{
  public:
   Triangle( int a=0, int b=0):Shape(a, b) { }
   int area ()
   { 
     cout << "Triangle class area :" <<endl;
     return (width * height / 2); 
   }
};
//  The main function of the program 
int main( )
{
  Shape *shape;
  Rectangle rec(10,7);
  Triangle tri(10,5);
 
  //  Store the address of the rectangle 
  shape = &rec;
  //  Call the rectangle's area function  area
  shape->area();
 
  //  storage 3 Angular addresses 
  shape = &tri;
  //  call 3 The area function of an Angle  area
  shape->area();
  
  return 0;
}

When the above code is compiled and executed, it produces the following results:

[

Parent class area
Parent class area

]

The reason for the error output is that the calling function area() is set by the compiler to be the version in the base class, which is known as static polymorphism, or static link-the function call is ready before the program executes. This is sometimes referred to as early binding because the area() function is already set up during program compilation.

But for now, let's modify the program a little bit and put the keyword virtual before the declaration of area() in the Shape class, as follows:


class Shape {
  protected:
   int width, height;
  public:
   Shape( int a=0, int b=0)
   {
     width = a;
     height = b;
   }
   virtual int area()
   {
     cout << "Parent class area :" <<endl;
     return 0;
   }
};

After modification, when the previous instance code is compiled and executed, it produces the following results:

[

Rectangle class area
Triangle class area

]

At this point, the compiler looks at the contents of the pointer, not its type. Therefore, since the addresses of the objects of the tri and rec classes are stored in *shape, the respective area() functions are called.

As you can see, each subclass has a separate implementation of the function area(). That's how polymorphic is used in general. With polymorphism, you can have multiple different classes, all with the same name but different functions with different implementations, and the parameters of the functions can even be the same.

Virtual functions

Virtual functions are functions declared in the base class using the keyword virtual. When you redefine a virtual function defined in a base class in a derived class, you tell the compiler not to statically link to that function.

What we want is for any point in the program to be able to choose which function to call based on the type of object being called, an operation called dynamic linking, or late binding.

Pure virtual function

You might want to define a virtual function in a base class so that it can be redefined in a derived class to better apply to an object, but you can't give a meaningful implementation of a virtual function in a base class, which is where pure virtual functions come in.

We can rewrite the virtual function area() in the base class as follows:


class Shape {
  protected:
   int width, height;
  public:
   Shape( int a=0, int b=0)
   {
     width = a;
     height = b;
   }
   // pure virtual function
   virtual int area() = 0;
};

= 0 tells the compiler that the function has no body and that the above virtual functions are pure virtual functions.

The above is the detailed analysis of C++ polymorphic and virtual functions content, more information about C++ polymorphic and virtual functions please pay attention to other related articles on this site!


Related articles: