Q:

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

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Operator Overloading (set 2).

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;
}

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;
}

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;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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.

Answer Program 2:

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;
}

Answer Program 3:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Operator Overloading | Find output programs | ... >>
<< C++ Operator Overloading | Find output programs | ...