This section contains the C++ find output programs with their explanations on C++ Operator Overloading (set 4).
Program 1:
#include <iostream>
using namespace std;
class Test {
float A;
public:
Test(float a)
{
A = a;
}
void operator++(int)
{
A++;
}
void print()
{
cout << A << endl;
}
};
int main()
{
Test T1(10.5F);
T1++;
T1.print();
return 0;
}
Program 2:
#include <iostream>
using namespace std;
class Test {
float A;
public:
Test()
{
A = 0.0F;
}
Test(float a)
{
A = a;
}
void operator++(float)
{
A++;
}
void print()
{
cout << A << endl;
}
};
int main()
{
Test T1(10.5F);
T1++;
T1.print();
return 0;
}
Program 3:
#include <iostream>
using namespace std;
class Test {
float A;
public:
Test(float a)
{
A = a;
}
void operator<<()
{
cout << A << endl;
}
};
int main()
{
Test T1(10.5F);
T1 << ;
return 0;
}
Answer Program 1:
Output:
Explanation:
Here, we create a Test class that contains data member A of float type. And, we defined a default constructor and member function print() to print the value of A, also defined member function to overload post-increment operator by passing dummy argument int. Without the dummy argument, the function will be overloaded for the pre-increment operator.
Answer Program 2:
Output:
Explanation:
It will generate compilation error, because, to overload post-increment operator, we need to pass int as a dummy argument. Otherwise, it will generate an error.
Answer Program 3:
Output:
Explanation:
It will generate compilation error, because, to overload the "<<" operator, we need to pass exactly one argument to the overloaded function.
need an explanation for this answer? contact us directly to get an explanation for this answer