Week five project four static member application

Source: Internet
Author: User

"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?
  1. class  time{  
  2. public :   
  3.      time (int =0, int =0, int =0);   
  4.     void  show_time ( );   //according to is_24 and from0, output fitting form -20:23:5/8:23:5 pm/08:23:05 pm   
  5.      void  add_seconds ( int );   //Added n seconds   
  6.      void  add_minutes ( int );   //increase n minutes      
  7.      void  add_hours ( int );   //increase n hours      
  8.      static   void  change24 ();    //change static member is_24, convert   
  9.      static   void  changefrom0 ();     //changes static member From0, toggles whether the preamble 0   
  10.  private :   
  11.      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    
  12.      static   bool  from0;  //is true, the leading 0,8:23:5 is displayed as 08:23:05   
  13.     int  hour;  
  14.     int  minute;  
  15.     int  sec;  
  16. };
  17. //write down the initialization of static members and the definition of each member function ...   
  18. int  main ( )    
  19. {  }

[ 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& &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.