What is the appropriate output for the following program? (C++) class point { pr
ID: 3813323 • Letter: W
Question
What is the appropriate output for the following program? (C++)
class point {
private:
float x,y;
public:
point();
point(float,float);
float getx() { return x; };
float gety() { return y; };
point operator+(point p2);
};
point::point() {
x = 0;
y = 0;
}
point::point(float _x, float _y) {
x = _x;
y = _y;
}
point point::operator+(point p2) {
point temp(x - p2.x, y - p2.y);
return temp;
}
int main()
{
point a(3,4), b(2,2), c;
c = a + b;
cout << "c = (" << c.getx() << "," << c.gety() << ") ";
return 0;
}
A.) c = (5,6)
B.) c = (0,0)
C.) c = (6,8)
D.) c = (1,2)
Explanation / Answer
Answer is D)
C = (1,2)
Here there is point class where the variable are x and y
having methods:
// default constructor
point();
// parameterised constructor : assign values to x and y
point(float,float);
// return x
float getx() { return x; };
// return y
float gety() { return y; };
// operator overloading for +, where it calculate (x1 - x2),(y1 - y2)
point operator+(point p2);
The following opreation:
c = a + b;
temp(x - p2.x, y - p2.y)
x =3
p2.x =2
y =4
p2.y =2
=> temp(3 - 2, 4 - 2)
=> temp(1,2)
assign the temp variable to c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.