Thursday, May 13, 2021
Home C / C++ Overloading Addition operator in C++

Overloading Addition operator in C++

Here we have overloaded addition operator for a specific class or one can say addition operator is overloaded for a self-defined data type in C++. The code is:

    using namespace std;
    class point{
        int x,y;
            x =0;
            y = 0;
        friend ostream&operator<< (ostream& cin, point& z){
            cin<<z.x<<”  “<<z.y<<endl;
            return cin;
        friend istream&operator>>(istream& a, point> z)
            return a;
        point&operator+(point& a) //addition operator overloaded function
            a.x = a.x + x;
            a.y = a.y + y;
            return a;
    int main(){
        point n,m;
        cout<<“enter values for num1 :”;
        cout<<“enter values for num2 : “;
        cout<<“the sum is :”;

In the above code, we defined the class point with its attributes, then we defined its operator overloading function for addition operator, inside this function we added the “x” variables separately and the “y” variables separately of the given points. In the main() function the user is asked to enter values for the two point objects, and then the two point objects are added through the overloaded addition operator.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....