读书人

类的实现,该怎么处理

发布时间: 2012-03-18 13:55:39 作者: rapoo

类的实现
在头文件中定义一个有默认参数的构造函数,然后在gx.cpp中是gx类的实现,这样定义为什么出错了,如果要定义一个有默认参数的构造函数的类,且要分开实现,应该怎么做啊?谢谢!

//gx.h
#include <iostream.h>

class gx
{
public:
gx(int a=5,int b=5);

void show();
private:
int length;
int width;
};

//gx.cpp
#include <iostream.h>
#include "gx.h "
gx::gx(int a=5,int b=5);
{
//int a,b;
//cout < < "input two number: " < <endl;
//cin> > a> > b;
width=b;
length=a;
}
void gx::show()
{
cout < < "the length is: " < <length < <endl;
cout < < "the width is: " < <width < <endl;
cout < < "the mj is: " < <length*width < <endl;
}

[解决办法]
//gx.cpp
#include <iostream.h>
#include "gx.h "
gx::gx(int a,int b); /////把=5去掉
{
//int a,b;
//cout < < "input two number: " < <endl;
//cin> > a> > b;
width=b;
length=a;
}

读书人网 >C++

热点推荐