Article by: Manish Methani
Last Updated: October 16, 2021 at 8:04am IST
Polymorphism is a key concept in object-oriented programming, allowing objects of different classes to be treated as if they were objects of the same class. This enables code reusability and promotes modularity, making programs more flexible and easier to maintain. In this tutorial, we will explore the concept of polymorphism in C++ with examples and code snippets.
Polymorphism is a feature of object-oriented programming that allows objects of different types to be treated as if they were objects of the same type. This is achieved through the use of inheritance and virtual functions. When a base class pointer is used to point to an object of a derived class, the virtual functions of the derived class are invoked instead of those of the base class.
There are two types of polymorphism in C++:
Compile-time polymorphism, also known as static polymorphism or method overloading, where a function or operator can be overloaded with multiple implementations that differ in the number or types of their parameters. The appropriate implementation is chosen based on the types of the arguments at compile time.
Runtime polymorphism, also known as dynamic polymorphism or method overriding, where a virtual function defined in the base class is overridden by a function with the same name and signature in a derived class. The appropriate function to call is determined at runtime based on the actual type of the object being pointed to.
In Simple words, Poly = many , morphism = forms. The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
Let's take a look at some examples of polymorphism in C++.
#include <iostream> using namespace std; class Shape { public: void area() { cout << "Base Class" << endl; } }; class Circle : public Shape { public : void area() { out << "Area of Circle" << endl; } }; class Rectangle : public Shape { public : void area() { out << "Area of rectangle" << endl; } }; int main() { Shape *p , *q; p = new Circle(); p -> area(); q = new Rectangle(); q -> area(); return 0; }
Base Class Base Class
Now what we did in this program is, Firstly we defined pointers of Shape Class. Then we created an object of Circle Class and assigned it to Shape Class pointers p. Now p is pointing to Circle class objects but Still method of base class is being called.
Similarly , We created an object of Rectangle Class and assigned it base class pointer q. Now q is pointing to area method of Rectangle class but still base class method is being printed.
Now think for a whle , if you are pointing to method of Circle class why base class method is being called ? Similarly, If you are pointing to method of Rectangle Class why base class method is being called ? So here comes Virtual Functions into existence.
If you want to call the method of a derived class to which your base class pointer is pointed, you have to name the member function of base class as virtual. Virtual functions are declared by using virtual keyword. The following program shows how to implement virtual functions
#include <iostream> using namespace std; class Shape { public: virtual void area() { cout << "Base Class" << endl; } }; class Circle : public Shape { public : void area() { out << "Area of Circle" << endl; } }; class Rectangle : public Shape { public : void area() { out << "Area of rectangle" << endl; } }; int main() { Shape *p , *q; p = new Circle(); p -> area(); q = new Rectangle(); q -> area(); return 0; }
Area of Circle Area of rectangle
We declared the Function of base class as virtual. So when base class pointer p is pointing to Circle Class method , method inside Circle class is being implemented rather than base class one. Similarly in case of Rectangle Class .
If a base class virtual function doesn't have a function body then such a virtual function declares as pure virtual function.
class Shape { public : virtual void area() = 0; };
The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.
If a class contains pure virtual function then such class becomes an abstract class. We cannot create an object of an abstract class. But if required we can define pointer of its type.
Abstract class are defined to achieve polymorphism through late binding. Multiple classes can be derived from an abstract class. If a class is derived from an abstract class then it has to compulsorily override all the pure virtual functions declared in base class.