1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 3-6 静态数据成员与静态成员函数

3-6 静态数据成员与静态成员函数

时间:2018-12-04 21:21:55

相关推荐

3-6 静态数据成员与静态成员函数

3-6 静态数据成员与静态成员函数

Time Limit:1000MS Memory Limit:65536KB Submit Statistic

Problem Description

通过本题目的练习可以掌握静态数据成员和静态成员函数的用法

要求设计一个点类Point,它具有两个double型的数据成员x,y。和一个静态数据成员count ,用以记录系统中创建点对象的数目。为该类设计构造函数和析构函数,在其中对count的值做修改,体现点的数目的动态变化。并为其添加一个静态成员函数用以输出count的值;成员函数showPoint()用于输出点的信息。

并编写主函数,输出以下的内容。

Input

Output

Example Input

Example Output

x=0,Y=0the number of points is 3Deconstructor point x=5Deconstructor point x=3Deconstructor point x=0

Hint

#include <iostream>#include <string.h>using namespace std;class point{private:double x,y;static int count;public:point(double xx=0,double yy=0):x(xx),y(yy){count++;}~point(){count++;cout<<"Deconstructor point x="<<x<<endl;}void showPoint(){cout<<"x="<<x<<",Y="<<y<<endl; //x}static void d(){cout<<"the number of points is "<<count<<endl;}};int point::count=0;int main(){point p1(0),p2(3),p3(5);p1.showPoint();p3.d();return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。