Operator Overloading

Estimated reading: 4 minutes 22 Views

Operator overloading in C++ is a feature that allows you to redefine the behavior of operators (such as +, -, *, /, etc.) for user-defined types. It enables you to extend the functionality of operators to work with objects of your own classes.

Example 1:

#include <iostream>
using namespace std;
class triangle
{
    private:
        float width, height;
    public:
        triangle(float a = 0, float b = 0)
        {
            width = a;
            height = b;
        }
        void getdata()
        {
            cout << "Enter width \n";
            cin >> width;
            cout << "Enter height \n";
            cin >> height;
        }
        void showdata()
        {
            cout << "width and height = (" << width << "," << height << ")" << endl;
        }
        void add(triangle c1, triangle c2)
        {
            width = c1.width + c2.width;
            height = c1.height + c2.height;
        }
};
int main()
{
    triangle c1, c2(3.5, 1.5), c3;
    c1.getdata();
    c3.add(c1, c2);
    c3.showdata();
    return 0;
}

This C++ code defines a class triangle representing a triangle. The class has private data members width and height, a constructor to initialize them, member functions getdata to input values for width and height, showdata to display the width and height, and add to add two triangles together.

Class Definition:

  • The triangle class is defined with private data members width and height.
  • It has a constructor that initializes width and height with default values of 0.
  • It contains member functions getdata, showdata, and add.

Main Function:

  • In the main function, three triangle objects are created: c1, c2, and c3.
  • c1 is initialized with default values using the default constructor.
  • c2 is initialized with values 3.5 and 1.5 using the parameterized constructor.
  • c3 is created without any initialization.
  • getdata is called for c1 to input values for width and height.
  • add is called for c3 with c1 and c2 as arguments, which adds the corresponding width and height of c1 and c2 and assigns the result to c3.
  • showdata is called for c3 to display the resulting width and height after addition.

Example 2:

#include <iostream>
using namespace std;
class triangle
{
    private:
        float width, height;
    public:
        triangle(float a = 0, float b = 0)
        {
            width = a;
            height = b;
        }
        void getdata()
        {
            cout << "Enter width \n";
            cin >> width;
            cout << "Enter height \n";
            cin >> height;
        }
        void showdata()
        {
            cout << "width and height = (" << width << "," << height << ")" << endl;
        }
        triangle add(triangle c2)
        {
            triangle c3;
            c3.width = width + c2.width;
            c3.height = height + c2.height;
            return c3;
        }
};
int main()
{
    triangle c1, c2(3.5, 1.5), c3;
    c1.getdata();
    c3 = c1.add(c2);
    c3.showdata();
    return 0;
}

In this example, the addition of two triangles is also performed using a member function named add. However, instead of modifying the object on which the function is called, a new triangle object is created inside the add function to store the result of the addition. This new object is then returned from the function, leaving the original objects unchanged.

Example 3:

#include <iostream>
using namespace std;
class triangle
{
    private:
        float width, height;
    public:
        triangle(float a = 0, float b = 0)
        {
            width = a;
            height = b;
        }
        void getdata()
        {
            cout << "Enter width \n";
            cin >> width;
            cout << "Enter height \n";
            cin >> height;
        }
        void showdata()
        {
            cout << "width and height = (" << width << "," << height << ")" << endl;
        }
        triangle operator+(triangle c2) // Overloading the + operator
        {
            triangle c3;
            c3.width = width + c2.width;
            c3.height = height + c2.height;
            return c3;
        }
};
int main()
{
    triangle c1, c2(3.5, 1.5), c3;
    c1.getdata();
    c3 = c1 + c2;  // Using the overloaded + operator
    c3.showdata();
    return 0;
}

This C++ code demonstrates the usage of operator overloading to perform addition between two triangle objects. Let’s break down the code and highlight the differences compared to the previous examples:

Differences compared to previous examples:

  1. Operator Overloading:
    In this example, the + operator is overloaded using a member function named operator+. This allows us to perform addition between two triangle objects using the + operator in a manner similar to built-in types.
    In contrast, the previous examples used member functions (add) to perform addition between objects.
  2. Usage in main Function:
    In the main function, the addition of c1 and c2 is performed using the + operator: c3 = c1 + c2;.
    This syntax is more intuitive and concise compared to calling a separate member function (add).
  3. Return Type:
    The operator+ function returns a triangle object representing the result of the addition.
    In the previous examples, the add functions did not return any value and instead modified one of the objects directly.
Share this

Operator Overloading

Or copy link

CONTENTS
English