Write a program to implement polymorphism using area of circle and rectangle in C++

4 min 13 sec read Basic


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

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python