C++_002常对象

常对象

常对象

常量是一个在程序执行过程中值不能改变的量。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
常对象在定义时必须进行初始化
原文地址:https://www.cnblogs.com/gudushishenme/p/3329839.html