c-plus-plus
  1. c-plus-plus-polymorphism

C++ Polymorphism Polymorphism

Polymorphism is one of the important concepts in object-oriented programming. It defines the ability of objects of different classes to be treated as if they belong to the same class. In C++, Polymorphism is achieved through function overloading and virtual function.

Syntax

class BaseClass {
   public:
      virtual void print() {
         cout << "BaseClass print function" << endl;
      }
};

class DerivedClass : public BaseClass {
   public:
      void print() {
         cout << "DerivedClass print function" << endl;
      }
};

int main() {
   BaseClass* ptr;
   BaseClass bc;
   DerivedClass dc;

   ptr = &bc;
   ptr->print();

   ptr = &dc;
   ptr->print();

   return 0;
}

Example

#include <iostream>
using namespace std;

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

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);

   // store the address of Rectangle
   shape = &rec;

   // call rectangle area.
   shape->area();

   // store the address of Triangle
   shape = &tri;

   // call triangle area.
   shape->area();

   return 0;
}

Output

Rectangle class area :
Triangle class area :

Explanation

Polymorphism allows objects of different classes to be treated as if they belong to the same class. In C++, we achieve this through two methods - function overloading and virtual function.

In the above example, we have three classes - Shape, Rectangle, and Triangle. Shape is the parent class, and Rectangle and Triangle are derived classes.

Shape has a virtual function area() which is overridden in the derived classes. In the main function, we create objects of Rectangle and Triangle and store the addresses of these objects in a pointer of type Shape. We call the area() function through the pointer.

This is possible because the virtual function in the parent class is overridden in the derived classes, and the compiler knows which version of the function to call at runtime based on the object being pointed to.

Use

Polymorphism is used in situations where we want to treat objects of different classes in a uniform manner. This is useful in creating modular and flexible code that is easy to maintain and modify.

Important Points

  • Polymorphism is achieved in C++ through function overloading and virtual function.
  • Virtual function allows objects of different classes to be treated as if they belong to the same class.
  • Virtual function is used when a function in the derived class overrides a function in the parent class.
  • In polymorphism, the function to be called is determined at runtime based on the object being pointed to.

Summary

Polymorphism is an important concept in object-oriented programming. It allows objects of different classes to be treated as if they belong to the same class. In C++, polymorphism is achieved through function overloading and virtual function. Virtual function is used when a function in the derived class overrides a function in the parent class. This allows the function to be called based on the object being pointed to at runtime. Polymorphism is useful in creating modular and flexible code that is easy to maintain and modify.

Published on: