C++ Operator Overloading | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Operator Overloading (set 1).
Submitted by Nidhi, on July 02, 2020

Program 1:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test()
    {
        A = 0;
    }
    Test(int a)
    {
        A = a;
    }
    Test operator+(Test A, Test B)
    {
        Test temp;

        temp.A = A.A + B.A;

        return temp;
    }

    void print()
    {
        cout << A << " ";
    }
};

int main()
{
    Test T1(10);
    Test T2(20);
    Test T3;

    T3 = T1 + T2;

    T3.print();

    return 0;
}

Output:

main.cpp:16:34: error: ‘Test Test::operator+(Test, Test)’ 
must take either zero or one argument
     Test operator+(Test A, Test B)
                                  ^
main.cpp: In function ‘int main()’:
main.cpp:37:13: error: no match for ‘operator+’ 
(operand types are ‘Test’ and ‘Test’)
     T3 = T1 + T2;
          ~~~^~~~

Explanation:

It will generate an error. Because we did not define the member function for operator overloading properly. Here we tried to overload binary '+' operator. So the first operand must be calling object and we can pass only one argument to the function for overloading '+' operator. But here we passed two arguments, which is not valid syntax to overload binary '+' operator.

Program 2:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test()
    {
        A = 0;
    }
    Test(int a)
    {
        A = a;
    }
    Test operator+(Test B)
    {
        Test temp;

        temp.A = (*this).A + B.A;

        return temp;
    }

    void print()
    {
        cout << A << " ";
    }
};

int main()
{
    Test T1(10);
    Test T2(20);
    Test T3;

    T3 = T1 + T2;

    T3.print();

    return 0;
}

Output:

30

Explanation:

Here, we created a class Test that contains data member A. here we implemented a function to overload binary '+' operator which is used to add data members of two objects and returned the sum using temporary object temp and then we returned temp object from an overloaded function.

In the main() function, we created three object T1, T2, and T3. Here, we initialized T1 and T2 with 10 and 20 respectively.

T3 = T1 + T2;

The above statement will call the overloaded method of binary '+' operator. Then the sum of data member A of T1 and T2 will be assigned to T3. And then we called the print() function that will print 30 on the console screen.

Program 3:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test()
    {
        A = 0;
    }
    Test(int a)
    {
        A = a;
    }
    Test operator+(Test B)
    {
        Test temp;

        temp.A = (*this).A + B.A;

        return temp;
    }
    void print()
    {
        cout << A << " ";
    }
};

int main()
{
    Test T1(10);
    Test T2(20);
    Test T3;

    T3 = T1.operator+(T2);

    T3.print();

    return 0;
}

Output:

30

Explanation:

Here, we created a class Test that contains data member A. We implemented a function to overload binary '+' operator which is used to add data members of two objects and returned the sum using temporary object temp and then we returned temp object from an overloaded function.

In the main() function, we created three object T1, T2, and T3. Here, we initialized T1 and T2 with 10 and 20 respectively.

T3 = T1.operator+(T2);

The above statement will call the overloaded method of binary '+' operator. Then the sum of data member A of T1 and T2 will be assigned to T3. And then we called the print() function that will print 30 on the console screen.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.