Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C++ | Constructors | Question 8

  • Difficulty Level : Basic
  • Last Updated : 28 Jun, 2021




#include <iostream>
using namespace std;
  
class Point
{
    int x, y;
public:
   Point(int i = 0, int j = 0) { x = i; y = j; }
   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) Compiler Error
(B) x = 0 y = 0
(C) x = garbage value y = garbage value


Answer: (B)

Explanation: Compiler creates a copy constructor if we don’t write our own. Compiler writes it even if we have written other constructors in class. So the above program works fine. Since we have default arguments, the values assigned to x and y are 0 and 0.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!