. Write a C++ program to implement operator overloading using polymorphism.

Here’s an example C++ program to implement operator overloading using polymorphism:

				
					#include <iostream>
using namespace std;

// Base class
class Shape {
   protected:
      int width;
      int height;

   public:
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }

      // virtual function for area calculation
      virtual int area() {
         cout << "Area of shape not defined" << endl;
         return 0;
      }

      // overloaded operator +
      Shape operator+(const Shape& shape) {
         Shape newShape;
         newShape.width = width + shape.width;
         newShape.height = height + shape.height;
         return newShape;
      }
};

// Derived class
class Rectangle: public Shape {
   public:
      // override area calculation function
      int area() {
         return (width * height);
      }
};

// Derived class
class Triangle: public Shape {
   public:
      // override area calculation function
      int area() {
         return (width * height)/2;
      }
};

int main() {
   Rectangle rect1, rect2;
   Triangle tri1, tri2;

   // Set the values of width and height for the first rectangle
   rect1.setWidth(5);
   rect1.setHeight(7);

   // Set the values of width and height for the second rectangle
   rect2.setWidth(3);
   rect2.setHeight(4);

   // Set the values of width and height for the first triangle
   tri1.setWidth(5);
   tri1.setHeight(7);

   // Set the values of width and height for the second triangle
   tri2.setWidth(3);
   tri2.setHeight(4);

   // Calculate and print the areas of the rectangles
   cout << "Area of Rectangle 1: " << rect1.area() << endl;
   cout << "Area of Rectangle 2: " << rect2.area() << endl;

   // Calculate and print the areas of the triangles
   cout << "Area of Triangle 1: " << tri1.area() << endl;
   cout << "Area of Triangle 2: " << tri2.area() << endl;

   // Add two rectangles using overloaded operator +
   Shape sumRectangles = rect1 + rect2;
   cout << "Sum of Rectangles: " << sumRectangles.area() << endl;

   // Add two triangles using overloaded operator +
   Shape sumTriangles = tri1 + tri2;
   cout << "Sum of Triangles: " << sumTriangles.area() << endl;

   // Add a rectangle and a triangle using overloaded operator +
   Shape sumRectangleTriangle = rect1 + tri1;
   cout << "Sum of Rectangle and Triangle: " << sumRectangleTriangle.area() << endl;

   return 0;
}

				
			
Output
Operator Overloading in C++
  • In this program, we define a base class called Shape which has two protected data members: width and height. The Shape class also has a constructor that sets the values of width and height and a virtual member function called area which prints a message and returns 0. The Shape class also has a virtual destructor.
  • We then define two derived classes: Rectangle and Triangle which both inherit from the Shape class using the public access specifier. Both the derived classes have their own area member functions which override the area member function of the base class.
  • In the main function, we create two objects of the derived classes: rec of Rectangle class and tri of Triangle class. We then create a pointer to the base class called shape and set it to point to the rec object. We call the area member function through the pointer, which invokes the area member function of the Rectangle class. We then set the pointer to point to the tri object and call the area member function again, which this time invokes the area member function of the Triangle class.
  • This program demonstrates the use of polymorphism in operator overloading. The area member function of the Shape class is overridden by the area member functions of the derived classes, allowing us to call the appropriate area member function depending on the object being used.