这要分情况,如果说你要创建无参数的对象时,就不可以共存,因为编译器就会不知道要调用那个构造函数而出错。而当你创建有参数的对象时,就可以。
一般情况下,一个类中有一个全部都是默认参数的构造函数就够了,因为它相当于重载了所有的构造函数。
不可以。例如,
#include
using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
Date()
{
year=2010;
month=12;
day=8;
}
Date(int y=2010,int m=12,int d=8)
{
year=y;
month=m;
day=d;
}
void ShowDate()
{
cout<
};
int main()
{
Date obj1;
obj1.ShowDate();
Date obj2(2010,12,10);
obj2.ShowDate();
return 0;
}
这个程序会报错::\Program Files\Microsoft Visual Studio\MyProjects\类\1.cpp(28) : warning C4520: 'Date' : multiple default constructors specified
D:\Program Files\Microsoft Visual Studio\MyProjects\类\1.cpp(31) : error C2668: 'Date::Date' : ambiguous call to overloaded function
Error executing cl.exe.