//类的静态成员
class Point
{
public:
Point(int xx=0,int yy=0) //隐式内联构造函数
{
X=xx;
Y=yy;
countP++;
}
Point(Point &p); //拷贝构造函数
~Point() //隐式内联析构函数
{
counP--;
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
static void GetC() //静态函数成员,可以直接通过类名调用
{
cout<<"Object id="<<countP<<endl;
}
private:
int X,Y;
static int countP;
};
Point::Point(Point &p)
{
X=p.X;
Y=p.Y;
countP++;
}
int Point::countP=0; //静态数据成员定义和初始化,使用类名限定
int main()
{
Point::GetC();// 直接通过类名调用函数输出对象个数的初始值
<span style="white-space:pre"> </span>Point A(4,5);
cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
A.GetC();
Point B(A);
cout<<"Point B,"<<B.GetX()<<","<<B.GetY();
B.GetC();
}