注册 登录
编程论坛 C++教室

包含不同类型参数的构造函数

meweiwei 发布于 2007-05-15 17:56, 573 次点击

请教各位大侠:一个构造函数是否允许包含不同类型的参数,如果允许的,又怎么定义这个构造函数?
#include <iostream>
using namespace std;
class Day
{
private:
int year,month,day;
public:
Day(int y,int m,int d)
{ year=y; month=m; day=d ; }
Day (Day &one)
{ year =one.year; month=one.month; day=one.day; }
void show()
{ cout<<year<<' '<<month<<' '<<day<<endl;}

};
class person
{
private:
int num,IDnum;
char sex;
Day birthday;
public:
person(int n,int IDn ,char s,Day &birthday);

};
int main()
{

return 0;
}

6 回复
#2
aipb20072007-05-15 17:59
允许,根据自己需要定义,相当于重载,只要参数不同就可以!
#3
I喜欢c2007-05-15 18:11
重载塞..
#4
meweiwei2007-05-15 18:30

谢谢两位.
不过我还是出了一个错:
error C2512: 'Day' : no appropriate default constructor available
怎么改啊?
#include <iostream>
using namespace std;
class Day
{
private:
int year,month,day;
public:
Day(int y,int m,int d)
{ year=y; month=m; day=d ; }
Day (Day &one)
{ year =one.year; month=one.month; day=one.day; }
void show()
{ cout<<year<<' '<<month<<' '<<day<<endl;}

};
class person
{
private:
int num,IDnum;
char sex;
Day birthday;
public:
person(int n ,int IDn)
{ num=n; IDnum=IDn; }

person( char s)
{ sex=s;}
person( Day &bir)
{ birthday= bir;}
void show ()
{ cout<<num<<' '<<IDnum<<' '<<sex<<endl; }


};
int main()
{

return 0;
}

#5
herbert_19872007-05-16 01:50

class Day
{
private:
int year,month,day;
public:
Day()
{
}
Day(int y,int m,int d)
{ year=y; month=m; day=d ; }
Day (Day &one)
{ year =one.year; month=one.month; day=one.day; }
void show()
{ cout<<year<<' '<<month<<' '<<day<<endl;}

};

Day类中要写上默认构造函数(无参构造函数)
Day()

#6
aipb20072007-05-16 09:11
person(int n ,int IDn)
{ num=n; IDnum=IDn; }


你有一个数据成员为Day类型,但是在这里找不到可以初始化他的构造函数。
#7
leilinghua2007-05-16 13:04
默认构造函数被重载覆盖了
1