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

来源:互联网 发布:pop服务器端口 编辑:程序博客网 时间:2024/06/06 00:23

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

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

Problem Description

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

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

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

Input

 

Output

 0
0
且静态成员函数只可以引用属于该类的静态数据成员和静态成员函数
0
0
0

Example Input

Example Output

x=0,Y=0the number of points is 3Deconstructor point x=5Deconstructor point x=3Deconstructor point x=0
#include <iostream> using namespace std; class Point { private:    int x;int y;static int c; public:     Point(int xx=0,int yy=0);     ~Point();     static void put();     void showPoint(); }; Point::Point(int xx,int yy) {     c++;     x=xx;y=yy; } void Point::put() {      cout<<"the number of points is "<<c<<endl; } void Point::showPoint() {     cout<<'x'<<'='<<x<<','<<'Y'<<'='<<y<<endl; }  Point::~Point() {     cout<<"Deconstructor point x="<<x<<endl; } int Point::c=0; int main() {     Point a;     a.showPoint();     Point b(3);     Point c(5);     c.put();     return 0; }


原创粉丝点击