楼主的意思是,不带参数的构造函数的声明可以在.cpp中?我想楼主先分清楚一下,.h 和.cpp 两个文件里都放些什么吧。
.h 文件放类的声明
.pp文件放类的成员函数的实现。
这样要在其他文件中使用这个类,只需引用它的.h文件就可以了。就是一个良好的编程习惯。
| 全能ASP/PHP/ASP.NET主机,支持月付 | 专业 MSSQL 数据库空间,支持月付 | 专业 MySQL 数据库空间,支持月付 | 学习型 ASP/PHP/ASP.NET 主机 30元/年 |
| 高端软件开发 = 年薪十万不是梦 | 赛孚耐:软件保护加密专家 | 身份认证令牌USB KEY |
楼主的意思是,不带参数的构造函数的声明可以在.cpp中?
class jose
{ public:
jose(int i=1,int n=3)
...............
protected:
int interval;
int number;
}
jose::jose(int i,int n)
{
interval=i;
number=n;
}
应该是这样吧

楼主的意思是,不带参数的构造函数的声明可以在.cpp中?说的没错
我估计楼主的意思,是想问在定义时还要不要写默认

好像带参数的构造函数定义都在*.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;
}
好像带参数的构造函数定义都在*.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;
}
