C++ Program #1

What is the output of the following C++ program?

#include<iostream>
using namespace std;

class Point
{
private:
	int x;
	int y;
public:
	Point(const Point&p) { x = p.x; y = p.y; }
	void setX(int i) {x = i;}
	void setY(int j) {y = j;}
	int getX() {return x;}
	int getY() {return y;}
	void print() { cout << "x = " << getX() << ", y = " << getY(); }
};


int main()
{
	Point p1;
	p1.setX(10);
	p1.setY(20);
	Point p2 = p1;
	p2.print();
	return 0;
}

Output

Compiler Error in first line of main(), i.e., “Point p1;”

Since you have a custom constructor, the compiler will not create a default constructor. If I remove the copy constructor from the Point class, the program works fine and prints the output.

%d bloggers like this: