好像带参数的构造函数定义都在*.h中定义,而不能在*.cpp中定义。(*.h代表类定义,*.cpp代表定义类中成员函数的C++ source file)。比如:
#include <iostream.h>
#include<string.h>
class student
{
public:
student(char *pName="no name",int ssid=0) //书上好像都没有把这种带参数的构造函数在另外的文件(以*.h表示)中定义,都是在类(以*.cpp表示)中定义的。
{
id=ssid;
strcpy(name,pName);
cout<<"constructing new student "<<pName<<endl;
}
student(student &s)
{
cout<<"constructing copy of "<<s.name<<endl;
strcpy(name,"copy of ");
strcat(name,s.name);
id=s.id;
}
~student()
{
cout<<"destructing "<<name<<endl;
}
protected:
char name[40];
int id;
};
void fn(student s)
{
cout<<"In function fn()"<<endl;
}
void main()
{
student randy("randy",1234);
cout<<"calling fn()"<<endl;
fn(randy);
cout<<"returned from fn()"<<endl;
}