May 11, 2021 C++
Polymorphism literally means a variety of forms. Polymorphisms are used when there is a hierarchy between classes and when classes are associated through inheritance.
Polymorphism means that when a member function is called, different functions are executed depending on the type of object that called the function.
In the following example, the base class Shape is derived into two classes, as follows:
#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);
}
};
// 程序的主函数
int main( )
{
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// 存储矩形的地址
shape = &rec;
// 调用矩形的求面积函数 area
shape->area();
// 存储三角形的地址
shape = &tri;
// 调用三角形的求面积函数 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 the version in the base class, which is called static polymorphism, or static link - the function call is ready before the program executes. Sometimes this is also known as early binding because the area() function is already set up during program compilation.
But now, let's make a slight change to the program, placing the keyword virtual in the Shape class before the declaration of area(), 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, because the addresses of the tri and rec classes are stored in the shape, their respective area() functions are called.
As you can see, each sub-class has a separate implementation of the function area(). T his is the general use of polymorphism. With polymorphism, you can have several different classes, all with functions with the same name but with different implementations, and the parameters of the functions can even be the same.
A virtual function is a function declared in a base class using the keyword virtual. When you redefine a virtual function defined in a derived class, the compiler is told not to link statically to the function.
What we want is that any point in the program can choose which function to call based on the type of object being called, which is called dynamic linking, or late binding.
You may want to define a virtual function in a base class so that it is better suited to objects by redefining it in a derived class, but you can't give a meaningful implementation to a virtual function in the base class, and you'll use a pure virtual function.
We can override 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 virtual function above is a pure virtual function.