C++经典绝版的课件.ppt

C++经典绝版的课件.ppt

* * * * * * * * * class Point { public: int getx() const;//在函数头之后注明const int gety() const; void display( ) const; ... private: int x, y; }; int Point::getx() const //此处的const也是必要的 { return x; } 例2-3 常成员函数 2.3.2类的成员函数-常成员函数 与inline不同的是,const必须在成员函数声明和实现时都指明,即const属于函数原型的一部分而不仅是修饰词。 程序中可以定义常量对象(与普通常量具有相同含义),如: const Point pt; 通过常量对象pt只能调用类的const方法。 例2-4 重载成员函数 #include iostream using namespace std; class myclass { public: void set(){x=10;y=20;z=30;} void set(int xx){x=10;y=20;z=30;} void set(int xx,int yy ){x=xx;y=yy;z=30;} void set(int x

文档评论(0)

1亿VIP精品文档

相关文档