You're almost right, it's easy to get confused. You misunderstood the syntax rules only slightly.
The C++ rule about default constructor
does not say:
"If parameter-less constructor is not provided, default constructor is assumed".Instead,
it says:
"If you provide a constructor that takes a non-void parameter, and you want to allow your class to be created with no parameters, you must also provide a default constructor. The default constructor can be a constructor with default values for all parameters."Do you see the subtle difference?
So, assuming you want to have the same members, you need to change one of the following, or both:
1) Add parameter-less constructor, something like this:
class sample
{
int a,b;
public:
sample() : a(0), b(0) {}
}
2) Use your available constructor with two parameters:
sample obj1(x, y);
—SA