Commvault Written Test Qs.(C++ Programming) :: Objects and Classes - Discussion
9 / 40
#include <iostream>
using namespace std;
class test {
float x, y;
public:
test(float a = 1.0, float b = 2.0)
{
x = a;
y = b;
}
test operator + (test & obj) {
return test(this->x + obj.x, y + obj.y);
}
operator float () {
return (x + y) ;
}
};
int main () {
test obj1(1.23, 4.56), obj2;
obj2 = obj1 + obj2;
cout << obj2;
return 0;
}
#include <iostream>
using namespace std;
class test {
float x, y;
public:
test(float a = 1.0, float b = 2.0)
{
x = a;
y = b;
}
test operator + (test & obj) {
return test(this->x + obj.x, y + obj.y);
}
operator float () {
return (x + y) ;
}
};
int main () {
test obj1(1.23, 4.56), obj2;
obj2 = obj1 + obj2;
cout << obj2;
return 0;
}
A8.79
B5.79
C3
DCompilation error: binary '<<': no operator found which takes a right-hand operand of type 'test'
Show Explanation
Operator overloading can be defined as assigning new meaning to the overloaded operator. In our program, we overloaded '+' operator which gets called when two instances of type 'test' are added.
operator float () is a conversion operator which converts type 'test' to float.
Asked In ::
Operator overloading can be defined as assigning new meaning to the overloaded operator. In our program, we overloaded '+' operator which gets called when two instances of type 'test' are added.
operator float () is a conversion operator which converts type 'test' to float.
Read Full Answer
Report Error
Please Login First Click Here