Problem Description
通过本题目的练习可以掌握静态数据成员和静态成员函数的用法
要求设计一个点类Point,它具有两个double型的数据成员x,y。和一个静态数据成员count ,用以记录系统中创建点对象的数目。为该类设计构造函数和析构函数,在其中对count的值做修改,体现点的数目的动态变化。并为其添加一个静态成员函数用以输出count的值;成员函数showPoint()用于输出点的信息。
并编写主函数,输出以下的内容。
Input
无
Output
Example Input
Example Output
x=0,Y=0
the number of points is 3
Deconstructor point x=5
Deconstructor point x=3
Deconstructor point x=0
通过本题目的练习可以掌握静态数据成员和静态成员函数的用法
要求设计一个点类Point,它具有两个double型的数据成员x,y。和一个静态数据成员count ,用以记录系统中创建点对象的数目。为该类设计构造函数和析构函数,在其中对count的值做修改,体现点的数目的动态变化。并为其添加一个静态成员函数用以输出count的值;成员函数showPoint()用于输出点的信息。
并编写主函数,输出以下的内容。
Input
无
Output
Example Input
Example Output
x=0,Y=0
the number of points is 3
Deconstructor point x=5
Deconstructor point x=3
Deconstructor point x=0
Hint
// 静态函数,对象和类都可以来调用
// 非静态函数只能通过对象来调用
// 静态元素可以通过类调用,也可以通过对象调用,但是你必须初始化这个静态元素
// 非静态函数也可以调用静态变量
#include <iostream>
using namespace std;
class Point
{
private:
double x, y;
public:
static int count ;
Point(int j=0,int k=0):x(j),y(k)//统计点的个数
{
count++;
}
~Point()
{
cout<<"Deconstructor point x="<<x<<endl;
}
void show()
{
cout<<"x="<<x<<",Y="<<y<<endl;
}
static void count_show()//静态成员函数
{
cout<<"the number of points is "<<count<<endl;
}
};
int Point::count =0;//初始化静态元素
int main ()
{
Point A(0);
A.count = 0;
A.show();
Point B(3);
Point C(5);
C.count_show();
//Point::count_show();
}