常对象,常函数有什么作用

来源:百度知道 编辑:UC知道 时间:2024/05/23 16:10:09

产量是一个在程序执行过程中值不能改变的量。C++中的关键字CONST可以加到对象的声明中使该对象成为一个常量而不被改变。
使用CONST说明的成员函数,称为常成员函数。只有常成员函数才有权使用常量或常对象,没有使用的CONST说明的成员不能使用常对象。
#include "iostream.h"
class point
{
private:
int x,y;
public:
point(int xx=0,int yy=0)
{
x=xx;
y=yy;
}
void display()const
{
cout<<x<<"const"<<y<<endl;
}
void display()
{
cout<<x<<"*****"<<y<<endl;
}
};

void main()
{
point p(100,89);
p.display();
const point pc(200,98);
pc.display();
}
结果为:
100*****89
200const98