Problem
Design a time class that contains static data members and member functions. A static data member is a common data for all objects in a class, in the following design, whether the clock is to be 12-hour or 24-hour, or if the number that is less than two digits is preceded by 0, is a "global" setting that is appropriate as a static data member in the class.
Code:
#include <iostream>using namespace Std;class time{public:time (int h=0,int m=0,int s=0): Hour (h), Minute (m), Second (s) {};void show_time (); According to Is_24 and FROM0, the output is suitable for the form -20:23:5/8:23:5 pm/08:23:05 pmvoid add_seconds (int); Added n seconds void add_minutes (int); Added n minutes void add_hours (int); Added n-hour static void Change24 (); Change static member is_24, convert static void Changefrom0 () between 12 and 24 o'clock; Change static member From0, toggle whether leading 0 private:static bool is_24; True, 24-hour system, such as 20:23:5, for flase,12 hours, shown as 8:23:5 pmstatic bool from0; When True, the leading 0,8:23:5 is displayed as 08:23:05int hour;int minute;int second;}; BOOL Time::is_24=true;bool time::from0=false;void Time::change24 () {is_24=!is_24;} void Time::changefrom0 () {from0=!from0;} void time::add_hours (int a) {hour+=a; if (hour>23) hour=hour%12;} void Time::show_time () {int h= (is_24)? hour:hour%12; if (h<10&&from0) cout<< "0"; cout<
Operation Result:
Summary of Knowledge points:
Static members with BOOL can be used to select class applications
Learning experience:
Good study Day Day up
Week five project four-static member application