例5-3
题目: 具有静态、动态生存期对象的时钟程序。
代码部分
#include<iostream> using namespace std; class Clock { public: Clock():hour(0),minute(0),second(0){} void setTime(int a=0, int b=0, int c=0) { hour = a; minute = b; second = c; } void showTime() { cout << hour << ":" << minute << ":" << second << endl; } private: int hour, minute, second; }; Clock globClock; int main() { cout << "First time output:" << endl; globClock.showTime(); globClock.setTime(8, 30, 30); Clock myClock(globClock); cout << "Second time output:" << endl; myClock.showTime(); return 0; }
例5-4
题目:具有静态数据成员的Poin类
代码部分:
#include<iostream> using namespace std; class Point { private: int x, y; static int count; constexpr static int origin = 0; public: Point(int x = 0, int y = 0) :x(x), y(y) { count++; } Point(Point& p) { x = p.x; y = p.y; count++; } ~Point() { count--; } int getX() { return x; } int getY() { return y; } void showCount() { cout << " Object count=" << count << endl; } }; int Point::count = 0; constexpr int Point::origin = 0; int main() { Point a(4, 5); cout << "Point A:" << a.getX() << "," << a.getY(); a.showCount(); Point b(a); cout << "Point B:" << b.getX() << "," << b.getX(); b.showCount(); return 0; }
标签:count,21,Point,int,void,打卡,cout From: https://www.cnblogs.com/xuechenhao173/p/17419174.html