c++6数据共享与保护静态成员与友元.ppt

例6:静态数据成员举例 #include iostream using namespace std; class Point { public: Point(int x=0, int y=0) : x(x), y(y) { count++; } Point(Point p); int getX() { return x; } int getY() { return y; } void showCount() { cout “ Object count=“ count endl; } private: int x,y; static int count; }; 等价于: { x=x; y=y; count++; } Point::Point(Point p) { x = p.x; y = p.y; count++; } int Point::count=0; int main() { Point a(4,5); coutPoint A:a.getX(),a.getY(); a.showCount(); Point b(a); coutPoint B:b.getX(),b.getY(); b.showCount(); return 0; } 二、静态成员函数 用static定义; 与静态数据成员

文档评论(0)

1亿VIP精品文档

相关文档