C++ C++ Tutorial C++ Functions C++ Classes



CPP Polymorphism

Polymorphism is one of the fundamental concepts in object-oriented programming (OOP). It allows objects of different classes to be treated as if they were objects of the same class. C++ supports two types of polymorphism: compile-time polymorphism and runtime polymorphism.

Introduction of CPP Polymorphism

C++ is an object-oriented programming language that supports polymorphism. Polymorphism is the ability of an object to take on many forms. In C++, polymorphism is achieved through the use of virtual functions. A virtual function is a member function that is declared within a base class and is redefined by a derived class. When a virtual function is called, the actual function that is executed depends on the type of object that is calling the function.

Brief Explanation of CPP Polymorphism

Polymorphism is a powerful feature of C++ that allows objects of different classes to be treated as if they were objects of the same class. This is achieved through the use of virtual functions. A virtual function is a member function that is declared within a base class and is redefined by a derived class. When a virtual function is called, the actual function that is executed depends on the type of object that is calling the function.

There are two types of polymorphism in C++: compile-time polymorphism and runtime polymorphism. Compile-time polymorphism is achieved through function overloading and operator overloading. Function overloading allows multiple functions with the same name to be defined in a class, but with different parameters. Operator overloading allows operators such as +, -, *, /, and = to be redefined for a class.

Runtime polymorphism is achieved through the use of virtual functions. Virtual functions allow a derived class to override a base class function. When a virtual function is called on an object, the actual function that is executed depends on the type of object that is calling the function. This allows objects of different classes to be treated as if they were objects of the same class.

Code Examples

Here are some code examples that demonstrate the use of polymorphism in C++:


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

   shape = &rec;
   shape->area();

   shape = &tri;
   shape->area();

   return 0;
}

In this example, we have a base class called Shape and two derived classes called Rectangle and Triangle. The Shape class has a virtual function called area() that is overridden by the area() functions in the Rectangle and Triangle classes. In the main function, we create objects of the Rectangle and Triangle classes and assign them to a Shape pointer. We then call the area() function on the Shape pointer, which calls the appropriate area() function based on the type of object that is calling the function.

References

Activity