Sdut 2-2 Time class definition
2-2 Definition of Time class Time Limit: 1000 MS Memory limit: 65536 K Description
You can use this exercise to understand the definition of classes and objects;
Design a Time class. Private Data members include hour (hour), minute (minute), and second (second );
Public member functions include setHour (int), which sets the value of the data member hour. The default value of invalid input is 12. setMinue (int) sets the value of the data member minute. The default value of invalid input is 0; setSecond (int) sets the value of the data member second. The default value of invalid input is 0. setTime (int, int, int) sets the value of the hour, minute, and second data member; showTime () shows the value of the time object.
Call the corresponding member function in the main function main () so that the value of the time object can be received from the keyboard and displayed correctly.
Input
Enter three integers separated by a space.
Output
The output time, minute, and second values, separated ":"
Sample Input
10 11 12
Sample output
10:11:12
Prompt
Input
58 23 85
Output
12:23:00
Sample program from Huang Jingjing
This question has a small problem. When the minute and second are equal to 0, 00 is output, and "% 02d" can be used in c (# include is required ). ), And use cout in ++. with (0); cout. fill ("0"); yes, but the latter is troublesome (no separate header files are required). In this case, the latter is not implemented. Please advise !!
# Include
# Include
Using namespace std; // declare class type class time {// define private part private: int h; int m; int s; // define public part public: int sethour () // member function {if (h> 12 | h <0) h = 12; return h;} int setminue () // control the member function {if (m> 60 | m <0) m = 0; return m;} int setsecond () // controls the second member function {if (s> 60 | s <0) s = 0; return s;} void settime () // value assignment member function {cin> h> m> s;} void showtime () // output member function {printf ("% 02d: % 02d: % 02d \ n ", h, m, s); // # include
}; // Do not forget this semicolon // main function int main () {class time t; t. settime (); t. sethour (); t. setminue (); t. setsecond (); t. showtime (); return 0 ;}