#include
using namespace std;
class Box
{
double width;
public:
void setWidth() const
{
cout <<"11";
}
};
// 程序的主函数
int main( )
{
Box box;
box.setWidth();
return 0;
}
2.在类外定义常函数会出错, 例子如下
#include
using namespace std;
class Box
{
double width;
public:
void setWidth() const
{
cout <<"11";
}
};
// 成员函数定义
void Box::setWidth()
{
}
// 程序的主函数
int main( )
{
Box box;
box.setWidth();
return 0;
}
3在常函数里面,修改数据成员,会出错
改成非常函数就会对了



