Skip to content
Related Articles
Open in App
Not now

Related Articles

C++ | Constructors | Question 7

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 28 Jun, 2021
Improve Article
Save Article

What is the output of following program?

#include <iostream>
using namespace std;
class Point
    int x, y;
   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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!