设为首页 加入收藏

TOP

sdut 3-6 静态数据成员与静态成员函数
2015-07-20 17:29:37 来源: 作者: 【 】 浏览:2
Tags:sdut 3-6 静态 数据 成员 函数

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 
  
   

using namespace std;

class point
{
private :
    double x, y;
    static int count;//静态数据成员

public:
    static void showpoint();//声明静态成员函数
    void display()
    {
        cout<<"Deconstructor point x=" << x << endl;
    }

    point(double a=0, double b=0)
    {
        count++;
        x=a;
        y=b;
    }
};

int point::count = 0;

//定义静态成员函数
void point::showpoint()
{
    cout << "x=0,Y=0" << endl;
    cout << "the number of points is " << count << endl;
}
int main()
{
    point a(5), b(3), c(0);
    point::showpoint();
    a.display();
    b.display();
    c.display();
    return 0;
}

  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇leetcode - Subsets 下一篇poj 1321 棋盘问题(dfs)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)
·整理了250个shell脚 (2025-12-26 07:53:29)
·HyperText Transfer (2025-12-26 07:20:48)
·半小时搞懂 HTTP、HT (2025-12-26 07:20:42)