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

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

Program 1:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test(int a)
    {
        A = a;
    }
    void operator++()
    {
        ++A;
    }
    void print()
    {
        cout << A << " ";
    }
};

int main()
{
    Test T(10);

    ++T;

    T.print();

    return 0;
}

Output:

11

Explanation:

Here, we created a class Test that contains data member A and parameterized constructor, and overloaded pre-increment operator ++ to increase the value of data member A. In the main() function, we initialized object T with 10, and then use the ++T statement that will call overloaded function and increase the value of A. then we printed the value of A using print() function.

Thus, finally, 11 will be printed on the console screen.

Program 2:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    Test(int a)
    {
        A = a;
    }
    void operator++()
    {
        A = A + 1;
    }
    void print()
    {
        cout << A << " ";
    }
};

int main()
{
    Test T(10);

    T++;

    T.print();

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:26:6: error: no ‘operator++(int)’ declared 
for postfix ‘++’ [-fpermissive]
     T++;
     ~^~

Explanation:

It will generate a syntax error. Because to overload post-increment operator we need to pass dummy int argument in an overloaded member function, the correct way is given below,

void operator++(int)
{
	A = A +1;
}

Program 3:

#include <iostream>
using namespace std;

class Test {
    int A;

public:
    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:

main.cpp: In function ‘Test operator*(Test, Test)’:
main.cpp:27:10: error: ‘int Test::A’ is private within this context
     temp.A = T1.A * T2.A;
          ^
main.cpp:5:9: note: declared private here
     int A;
         ^
main.cpp:27:17: error: ‘int Test::A’ is private within this context
     temp.A = T1.A * T2.A;
                 ^
main.cpp:5:9: note: declared private here
     int A;
         ^
main.cpp:27:24: error: ‘int Test::A’ is private within this context
     temp.A = T1.A * T2.A;
                        ^
main.cpp:5:9: note: declared private here
     int A;
         ^

Explanation:

It will generate an error. Because we cannot access the private members outside the class. we access private data member A inside function which was used to overload "*" operator. That's why the above program will generate a compilation error.






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.