在本教程中,我们将讨论一个程序,以了解在编写自己的程序时C ++编译器是否创建了默认构造函数。
通常,C ++编译器在未定义默认构造函数的情况下使用默认构造函数,但始终使用用户定义的默认构造函数。
#include<iostream>
using namespace std;
class myInteger{
private:
int value;
//other functions in class
};
int main(){
myInteger I1;
getchar();
return 0;
}输出结果
Compiles successfully
#include<iostream>
using namespace std;
class myInteger{
private:
int value;
public:
myInteger(int v) //user-defined constructor
{ value = v; }
//other functions in class
};
int main(){
myInteger I1;
getchar();
return 0;
}输出结果
Gives error about user-defined constructor not being defined