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

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

Program 1:

#include <iostream>
using namespace std;

class Test {
public:
    int A;
    Test()
    {
        A = 0;
    }

    Test(int a)
    {
        A = a;
    }
    void print()
    {
        cout << A << " ";
    }
};

Test operator*(Test T1, Test T2)
{
    Test temp;

    temp.A = T1.A * T2.A;

    return temp;
}

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

    T3 = operator*(T1, T2);

    T3.print();

    return 0;
}

Output:

50

Explanation:

Here, we created a class Test with public data member A, and we defined constructors and print() member function, overloaded ‘*’ operator using non-member function. So, we passed two objects argument and returned a temporary object that contains the result of the multiplication.

In the main() function, we created three objects T1, T2, and T3. And then multiplication of T1 and T2 assigned to the object T3 using the overloaded function.

Program 2:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test()
    {
        A = 0;
    }
    Test(int a)
    {
        A = a;
    }

    int operator<(Test T)
    {
        int ret = 0;

        if (A < T.A)
            return 1;
        else
            return 0;
    }
};

int main()
{
    Test T1(10);
    Test T2(5);

    if (T1 < T2)
        cout << "T1 less than T2";
    else
        cout << "T2 less than T1";

    return 0;
}

Output:

T2 less than T1

Explanation:

Here, we created the class Test with data member A and defined two constructors. We also defined function to overload less than '<' operator to compare two objects. The overloaded function returned an integer value for comparison.

In the main() function. Here we created T1 and T2 objects that initialized with 10 and 5 respectively. And we compared both objects then "T2 less than T1" message will be printed on the console screen.

Program 3:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test()
    {
        A = 0;
    }
    Test(int a)
    {
        A = a;
    }

    int operator ::()
    {
        return A;
    }
};

int main()
{
    Test T1(10);

    cout << ::T1;

    return 0;
}

Output:

main.cpp:17:18: error: expected type-specifier before ‘::’ token
     int operator ::()
                  ^~
main.cpp: In function ‘int main()’:
main.cpp:27:13: error: ‘::T1’ has not been declared
     cout << ::T1;
             ^~

Explanation:

It will generate syntax error because we cannot overload "::" operator in C++.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.