SDUT 2675 - 【3-6 静态数据成员与静态成员函数】
2017-12-26 11:44
302 查看
Problem Description
通过本题目的练习可以掌握静态数据成员和静态成员函数的用法
要求设计一个点类Point,它具有两个double型的数据成员x,y。和一个静态数据成员count ,用以记录系统中创建点对象的数目。为该类设计构造函数和析构函数,在其中对count的值做修改,体现点的数目的动态变化。并为其添加一个静态成员函数用以输出count的值;成员函数showPoint()用于输出点的信息。
并编写主函数,输出以下的内容。
Input
无
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
静态数据成员:为整个类所共有,不属于任何一个具体对象
静态成员函数:可以直接访问该类的静态数据和函数成员。而访问非静态成员,必须通过对象名。静态成员函数可以通过类名或对象名来调用。而非静态成员函数只能通过对象名来调用。
通过本题目的练习可以掌握静态数据成员和静态成员函数的用法
要求设计一个点类Point,它具有两个double型的数据成员x,y。和一个静态数据成员count ,用以记录系统中创建点对象的数目。为该类设计构造函数和析构函数,在其中对count的值做修改,体现点的数目的动态变化。并为其添加一个静态成员函数用以输出count的值;成员函数showPoint()用于输出点的信息。
并编写主函数,输出以下的内容。
Input
无
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
静态数据成员:为整个类所共有,不属于任何一个具体对象
静态成员函数:可以直接访问该类的静态数据和函数成员。而访问非静态成员,必须通过对象名。静态成员函数可以通过类名或对象名来调用。而非静态成员函数只能通过对象名来调用。
#include<bits/stdc++.h> using namespace std; class Point { public: Point(double n = 0, double m = 0) { x = n, y = m, Count++; } ~Point(){ cout << "Deconstructor point x=" << x << endl; } static void showCount();//静态成员函数,可以直接访问该类静态数据和函数成员 void showPoint(); private: static int Count;//静态数据成员声明,用于记录点的个数 double x, y; }; int Point::Count = 0;//静态数据成员定义和初始化,使用类名限定 void Point::showCount() { cout << "the number of points is " << Count << endl; } void Point::showPoint() { cout << "x=" << x << ',' << "Y=" << y << endl; } int main() { Point a(0), b(3), c(5); a.showPoint(); Point::showCount(); }
相关文章推荐
- SDUT-2675 3-6 静态数据成员与静态成员函数
- sdut 3-6 静态数据成员与静态成员函数
- sdut 3-6 静态数据成员与静态成员函数
- 【2675】3-6 静态数据成员与静态成员函数
- 2675 3-6 静态数据成员与静态成员函数
- SDUTACM 3-6 静态数据成员与静态成员函数
- 3-6 静态数据成员与静态成员函数
- 3-6 静态数据成员与静态成员函数
- 静态数据成员和静态成员函数
- 静态数据成员与静态成员函数
- C++中类的静态数据成员函数解析[转贴]
- 3-6 静态数据成员与静态成员函数
- c++静态数据成员和静态成员函数
- C++静态成员(静态数据成员和静态成员函数)
- C++类静态数据成员与类静态成员函数
- 具有静态数据和函数成员的Point类
- 在c++中,静态数据成员可以被非静态成员函数调用吗?如果可以调用的话那为什么还要定义静态成员函数呢
- C++中静态数据成员和静态成员函数
- 静态数据和静态成员函数
- C++中类的静态数据成员函数解析