C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - Programs (Q.No. 18)
18.
What will be the output of the following program?
#include<iostream.h> 
class Point
{
    int x, y; 
    public:
    Point(int xx = 10, int yy = 20)
    {
        x = xx;
        y = yy; 
    }
    Point operator + (Point objPoint)
    {
        Point objTmp;
        objTmp.x = objPoint.x + this->x; 
        objTmp.y = objPoint.y + this->y;
        return objTmp;
    }
    void Display(void)
    {
        cout<< x << " " << y;
    }
};
int main()
{
    Point objP1;
    Point objP2(1, 2);
    Point objP3 = objP1 + objP2;
    objP3.Display(); 
    return 0; 
}
1 2
10 20
11 22
Garbage Garbage
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
7 comments Page 1 of 1.

Ben said:   7 years ago
What about the lack of operator "=" overloading for the point class?

And the fact the obj3 is not a pointer.

Gopal said:   8 years ago
Everything is correct, because default constructor is present with arguments.

Aarti said:   8 years ago
Your are right @Abhishek.

This program will give compile time. Because default constructor will not be created by the compiler.

The programmer have to declare default constructor.

Abhishek said:   8 years ago
If we have defined a parameterized constructor then default constructor will not be created by the compiler.

So we can not create object Point objP1.

Jessica B said:   8 years ago
Yes, Correct.

Thanks for the explanation @Sreenath. M.

Vaishali said:   10 years ago
I don't think the explanation is correct. How can objP1 have values (1,2)?

Sreenath. M. said:   1 decade ago
Left operand of overloaded '+' operator refers to the 'this' pointer and the right operand refers to the formal parameter of the overloading function.

objTmp.x = 1 + 10;
objTmp.y = 2 + 20;

So the result is 11 22.

Post your comments here:

Your comments will be displayed after verification.