C++ | Constructors | Question 7

What is the output of following program?

#include <iostream>
using namespace std;

class Point
{
    int x, y;
public:
   Point(const Point &p) { x = p.x; y = p.y; }
   int getX() { return x; }
   int getY() { return y; }
};

int main()
{
    Point p1;
    Point p2 = p1;
    cout << "x = " << p2.getX() << " y = " << p2.getY();
    return 0;
}

(A) x = garbage value y = garbage value
(B) x = 0 y = 0
(C) Compiler Error


Answer: (C)

Explanation: There is compiler error in line “Point p1;”. The class Point doesn’t have a constructor without any parameter. If we write any constructor, then compiler doesn’t create the default constructor.

It is not true other way, i.e., if we write a default or parameterized constructor, then compiler creates a copy constructor. See the next question.

Quiz of this Question






Practice Tags :

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.