3-6 静态数据成员与静态成员函数
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
通过本题目的练习可以掌握静态数据成员和静态成员函数的用法
要求设计一个点类Point,它具有两个double型的数据成员x,y。和一个静态数据成员count ,用以记录系统中创建点对象的数目。为该类设计构造函数和析构函数,在其中对count的值做修改,体现点的数目的动态变化。并为其添加一个静态成员函数用以输出count的值;成员函数showPoint()用于输出点的信息。
并编写主函数,输出以下的内容。
输入
无
输出
示例输入
示例输出
x=0,Y=0 the number of points is 3 Deconstructor point x=5 Deconstructor point x=3 Deconstructor point x=0
#include <iostream>
using namespace std;
class point
{
private:
double x;
double y;
static int count;
public:
point(double n=0,double m=0)//有默认参数的构造函数
{
x=n;
y=m;
count++;
}
~point()//析构函数
{
cout<<"Deconstructor point x="<<x<<endl;
}
static void show()//定义静态成员函数,用于输出点的个数
{
cout<<"the number of points is "<<count<<endl;
}
void showpoint()//输出点的信息
{
cout<<"x="<<x<<","<<"Y="<<y<<endl;
}
};
int point::count=0;//初始化静态参数
int main()
{
class point a(0),b(3),c(5);
a.showpoint();
point::show();//调用静态成员函数
return 0;//return前从后向前自动调用析构函数
}
#include <iostream>
using namespace std;
class point
{
private:
double x;
double y;
static int count;
public:
point(double n=0,double m=0)//有默认参数的构造函数
{
x=n;
y=m;
count++;
}
~point()//析构函数
{
cout<<"Deconstructor point x="<<x<<endl;
}
static void show()//定义静态成员函数,用于输出点的个数
{
cout<<"the number of points is "<<count<<endl;
}
void showpoint()//输出点的信息
{
cout<<"x="<<x<<","<<"Y="<<y<<endl;
}
};
int point::count=0;//初始化静态参数
int main()
{
class point a(0),b(3),c(5);
a.showpoint();
point::show();//调用静态成员函数
return 0;//return前从后向前自动调用析构函数
}