Open In App
Related Articles

C++ | Constructors | Question 8

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
#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

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads