×

C++ Polymorphism



What is Polymorphism ?

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.


Example :-

#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;
}

Output :-

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.

Virtual Functions

If you want to call the method of a derived class to which your base class pointer is pointed, you have to name member function of base class as virtual. Virtual functions are declared by using virtual keyword. 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;
}

Output :-

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 .

Pure Virtual Functions

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 acheive 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.