您的位置:首页 > 编程语言 > C语言/C++

具有静态数据和函数成员的Point类

2016-08-31 21:48 183 查看
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:text.cpp
*作者:汤善晔
*完成日期:2016年8月31日
*版本号:v1.0
*/
问题描述:具有静态数据和函数成员的Point类

#include <iostream>

using namespace std;
class Point
{
public:
    Point(int x=0,int y=0):x(x),y(y)
    {
        count++;
    }
    Point(Point &p)
    {
        x=p.x;
        y=p.y;
        count++;
    }~Point()
    {
        count--;
    }
    int getX(){return x;}
    int getY(){return y;}
    static void showCount()
    {
        cout<<"Object count="<<count<<endl;
    }
private:
    int x,y;
    static int count;
};
int Point::count=0;
int main()
{
    Point a(4,5);
    cout<<"Point A:"<<a.getX()<<","<<a.getY();
    Point::showCount();
    Point b(a);
    cout<<"Point B:"<<b.getX()<<","<<b.getY();
    Point::showCount();
    return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言