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;
}
Answer Program 1:
Output:
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:
Explanation:
It will generate a compilation error. Because we cannot overload dot '.' operator in C++.
Answer Program 3:
Output:
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