c++静态数据成员与静态成员函数

旧城等待, 2022-09-26 02:16 342阅读 0赞

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

  1. x=0,Y=0
  2. the number of points is 3
  3. Deconstructor point x=5
  4. Deconstructor point x=3
  5. Deconstructor point x=0

Author

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. class point
  5. {
  6. public:
  7. double x;
  8. double y;
  9. static int count;
  10. point(double a=0,double b=0):x(a),y(b)
  11. {
  12. count++;
  13. }
  14. void showpoint()
  15. {
  16. cout<<"x="<<x<<","<<"Y="<<y<<endl;//注意小写和大写
  17. }
  18. static void showcount()
  19. {
  20. cout<<"the number of points is "<<count<<endl;
  21. }
  22. ~point()//析构函数
  23. {
  24. cout<<"Deconstructor point x="<<x<<endl;
  25. }
  26. };
  27. int point::count=0;
  28. int main()
  29. {
  30. point stu1(0);
  31. point stu2(3);
  32. point stu3(5);
  33. stu1.showpoint();
  34. point::showcount();//无对象输出公用函数
  35. return 0;
  36. }

发表评论

表情:
评论列表 (有 0 条评论,342人围观)

还没有评论,来说两句吧...

相关阅读

    相关 静态数据成员静态成员函数

    类中的静态成员真是个让人爱恨交加的特性。我决定好好总结一下静态类成员的知识点,以便自己在以后面试中,在此类问题上不再被动。 静态类成员包括静态数据成员和静态函数成员两部分。