C++ Programming - References - Discussion
Discussion Forum : References - Programs (Q.No. 25)
25.
Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x, y;
public:
IndiaBix(int xx = 0, int yy = 0)
{
x = xx;
y = yy;
}
void Display()
{
cout<< x << " " << y;
}
IndiaBix operator +(IndiaBix z)
{
IndiaBix objTemp;
objTemp.x = x + z.x;
objTemp.y = y + z.y;
return objTemp;
}
};
int main()
{
IndiaBix objBix1(90, 80);
IndiaBix objBix2(10, 20);
IndiaBix objSum;
IndiaBix &objRef = objSum;
objRef = objBix1 + objBix2;
objRef.Display();
return 0;
}
Discussion:
6 comments Page 1 of 1.
Killua said:
6 years ago
objBix1 , x = 90, y = 80;
objBix2, x = 10, y = 20;
There is an overloaded addition operator, which add x and y member of IndiaBix.
The result is obviously 100 and 100.
I thought this line of code is invalid at first, objRef is reference that initialized with objSum and could not refer to other object.
objRef = objBix1 + objBix2;
But it's ok, cause objBix1 and objBix2 were implicit converted from lvalue to rvalue at that point.
objBix2, x = 10, y = 20;
There is an overloaded addition operator, which add x and y member of IndiaBix.
The result is obviously 100 and 100.
I thought this line of code is invalid at first, objRef is reference that initialized with objSum and could not refer to other object.
objRef = objBix1 + objBix2;
But it's ok, cause objBix1 and objBix2 were implicit converted from lvalue to rvalue at that point.
Shiskey said:
6 years ago
Somebody, please explain it.
Szapi said:
7 years ago
I think it should be a compile time error, because cout is in namespace std.
Pavithra said:
7 years ago
Anybody explain it.
(1)
Virat said:
8 years ago
Explain it.
Karthik said:
8 years ago
Please explain this.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers