Q:

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

0

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

Program 1:

#include <iostream>
using namespace std;

class Test {
public:
    void operator<<(string str)
    {
        cout << str << endl;
    }
};

int main()
{
    Test T1;
    T1 << "Hello world";
    return 0;
}

Program 2:

#include <iostream>
using namespace std;

class Test {
public:
    void operator.(string str)
    {
        cout << str << endl;
    }
};

int main()
{
    Test T1;
    T1.operator.("Hello world");
    return 0;
}

Program 3:

#include <iostream>
using namespace std;

class Test {
public:
    void operator>>(string str)
    {
        cout << str << endl;
    }
};

int main()
{
    Test T1;
    T1 >> "Hello world";
    return 0;
}

All Answers

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

Answer Program 1:

Output:

Hello world

Explanation:

Here, we created a class Test that contains member function to overload "<<" operator. In the main() function, we created the object T1 and call the "<<" operator to print the string "Hello world" on the console screen.

Answer Program 2:

Output:

main.cpp:6:18: error: expected type-specifier before ‘.’ token
     void operator.(string str)
                  ^
main.cpp: In function ‘int main()’:
main.cpp:15:16: error: expected type-specifier before ‘.’ token
     T1.operator.("Hello world");
                ^
main.cpp:15:17: error: expected unqualified-id before ‘(’ token
     T1.operator.("Hello world");
                 ^

Explanation:

It will generate a compilation error. Because we cannot overload dot '.' operator in C++.

Answer Program 3:

Output:

Hello world

Explanation:

In the above program, we created a class Test that contains member function to overload ">>" operator, we created the object T1 and call ">>" operator to print the string "Hello world" on the console screen.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now