"Project 4-static member app"
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.
[CPP]View Plaincopyprint?
- class time{
- public :
- time (int =0, int =0, int =0);
- void show_time ( ); //according to is_24 and from0, output fitting form -20:23:5/8:23:5 pm/08:23:05 pm   
- void add_seconds ( int ); //Added n seconds   
- void add_minutes ( int ); //increase n minutes   
- void add_hours ( int ); //increase n hours   
- static void change24 (); //change static member is_24, convert   
- static void changefrom0 (); //changes static member From0, toggles whether the preamble 0   
- private :
- static bool is_24; //is true when the 24-hour system, such as 20:23:5; for the flase,12-hour system, is displayed as 8:23:5 pm   
- static bool from0; //is true, the leading 0,8:23:5 is displayed as 08:23:05   
- int hour;
- int minute;
- int sec;
- };
- //write down the initialization of static members and the definition of each member function ...
- int main ( )
- { }
[ Reference solution ]
#include <iostream>using namespace Std;class time{public:time (int=0,int=0,int=0); void Show_time ();//According to Is_ 24 and FROM0, output suitable for the form of time: 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 and time system; Change static member From0, convert whether leading private:static bool is_24; When true, the hour system, such as: 23:5, for flase, hour system, shown as: 23:5 pmstatic bool from0; When True, the preamble: 23:5 is displayed as: 23:05int hour;int minute;int sec;}; BOOL Time::is_24=true;bool Time::from0=false; Time::time (int h,int m,int s): Hour (h), Minute (m), SEC (s) {}void time::show_time () {int h= (is_24)? hour:hour%12; This is more concise than the notation below//if (is_24)//h=hour;//else//h=hour%12;if (h<10&&from0) cout<< ' 0 ';cout<< h<< ': '; if (minute<10&&from0) cout<< ' 0 ';cout<<minute<< ': '; if (sec<10& &FROM0) cout<< ' 0 '; cout<<sec;if (!is_24) cout<< ((hour>12)? "PM": "AM");//if (hour>12)//cout<< "PM";//elsecout<< "AM"; cout<<endl;} void Time::add_seconds (int n)//add n seconds {sec+=n;if (sec>59) {add_minutes (SEC/60); sec%=60;}} void time::add_minutes (int n)//increase n minutes {minute+=n;if (minute>59) {add_hours (MINUTE/60); minute%=60;}} void time::add_hours (int n)//increase n hours {hour+=n;if (hour>23) hour%=24;} void Time::change24 () {is_24=!is_24;} void Time::changefrom0 () {from0=!from0;} int main () {Time T1 (23,14,25), T2 (8,45,6);cout<< "24 O'Clock, not preamble:" <<endl;cout<< "T1 is:"; T1.show_time (); cout<< "T2 is:"; T2.show_time (); t1.add_hours (); t2.add_hours (10); Time::changefrom0 (); Note This call to static member cout<< "After 10 hours, whether the switchover is leading:" <<endl;cout<< "T1 is:"; T1.show_time ();cout<< "T2 is:"; T2.show_time (); T1.change24 ();cout<< "for a different format:" <<endl;cout<< "T1 is:"; T1.show_time ();cout<< " T2 is: "; T2.show_time (); return 0;}
This has been a long time to read the static members of the use of this side, and? : What are the advantages of symbols and so on.
Static members do not belong to which object, but are common to class objects.
Week five project four static member application