标题:
C++语言具有静态数据和静态成员函数的Point类
[打印本页]
作者:
daming
时间:
2014-12-30 01:59
标题:
C++语言具有静态数据和静态成员函数的Point类
本帖最后由 daming 于 2014-12-30 02:15 编辑
#include<iostream>
using namespace std;
class Point //point类定义
{
public: // 外部接口
Point(int xx=0,int yy=0){X=xx;Y=yy;countP++;};
Point(Point&); //拷贝构造函数
~Point(){countP--;};
int GetX(){return X;}
int GetY(){return Y;}
static void GetC(){cout<<"Object id="<<countP<<endl;} // 静态函数成员
private:
int X,Y;
static int countP; //静态数据成员声明
};
int Point::countP=0; //静态数据成员初始化
Point::Point(Point &p)
{
X=p.X;
Y=p.Y;
countP++;
}
void main() //主函数实现
{
Point A(4,5); //定义对象A
cout<<"Point A,"<<A.GetX()<<", "<<A.GetY();
A.GetC(); //输出对象号,对象名引用 "."
Point B(A);
cout<<"Point B,"<<B.GetX()<<", "<<B.GetY();
Point::GetC(); //类名引用 "::"
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1