C++静态成员变量和静态成员函数

#include<iostream>
using namespace std;
class Point{
public:
    Point(int x=0,int y=0):x(x),y(y){count++;}
    Point(const Point &p):x(p.x),y(p.y){count++;}
    ~Point(){count--;}
    int getX()const{return x;}
    int getY()const{return y;}
    static void showCount(){
        cout<<count<<endl;
    }
private:
    int x,y;
    static int count;
};
int Point::count=0;
int main(){
    Point a(1,2);
    cout<<a.getX()<<" "<<a.getY()<<endl;
    Point::showCount();
    Point b(a);
    cout<<b.getX()<<" "<<b.getY()<<endl;
    Point::showCount();
    return 0;
}

 

posted on 2020-03-14 18:06  ~明月几时有  阅读(160)  评论(0)    收藏  举报