Get Off-Campus Placement Jobs Info !!!
Programs asked in Mettl Coding Round
Click To Practce List of Programs asked in Nagarro !!!

Commvault Written Test Qs.(C++ Programming) :: Objects and Classes - Discussion

9 / 40

What is the output of the C++ program?

#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'

Answer: Option (Login/Signup)

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 :: Commvault Sopra Steria

Post Your Answer Here:     

Reply    
Rate This: +0 -0
    Report


Report Error

Please Login First Click Here