No, the C++ compiler doesn’t create a default constructor when we initialize our own, the compiler by default creates a default constructor for every class; But, if we define our own constructor, the compiler doesn’t create the default constructor. This is so because the default constructor does not take any argument and if two default constructors are created, it is difficult for the compiler which default constructor should be called.
Example 1 compiles without any error, but the compilation of program 2 fails with the error “no matching function for call to `myInteger::myInteger()’ ”
Example 1:
CPP
#include <iostream>
using namespace std;
class myInteger {
private :
int value;
};
int main()
{
myInteger I1;
getchar ();
return 0;
}
|
Example 2:
CPP
#include <iostream>
using namespace std;
class myInteger {
private :
int value;
public :
myInteger( int v)
{
value = v;
}
};
int main()
{
myInteger I1;
getchar ();
return 0;
}
|