sdut oj3-6 静态数据成员与静态成员函数

题目链接:点击打开链接   

 3-6 静态数据成员与静态成员函数
Time Limit: 1000MS Memory Limit: 65536KB
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

Hint
 
Author
 黄晶晶


代码实现:

#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
class Point
{
private:
    double x,y;
    static int Count;
public:
    Point(int a = 0,int b = 0);
    ~Point()
    {
        cout<<"Deconstructor point x="<<x<<endl;
    }
    static void showPoint();
    void display()
    {
        cout<<"x="<<x<<","<<"Y="<<y<<endl;//此处有坑
    }
};

Point::Point(int a,int b)
{
    x = a;y = b;
    Count++;
}
int Point::Count = 0;
void Point::showPoint()
{
    cout << "the number of points is " << Count << endl;
}

int main()
{
    Point a(0);
    a.display();
    Point b(3), c(5);
    Point::showPoint();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值