Example code for implementing clock C + + Description with Object-oriented thinking:
# include <iostream># include <time.h># include <iomanip># include <windows.h>//# include <unistd.h>using namespace std;// The initialized data comes from the system, subsequent logical operations and display self-implementation class clock{public:clock () {time_t t = time (NULL); struct tm ti = *localtime (&t); hour = ti.tm_hour;min = ti.tm_min;sec = ti.tm_sec;} Void run () {while (1) {System ("CLS"); show (); //finish displaying tick ();//Data Update}}private:void show () {// System ("CLS"); COUT&NBSP;<<&NBSP;SETW (2) << setfill (' 0 ') << hour << ":"; cout << setw (2) << setfill (' 0 ') << min << ":"; cout << setw (2) << setfill (' 0 ') << sec << ":";} Void tick () {Sleep (1);if (++sec == 60) {sec = 0;min += 1;if (++min == 60) {min = 0;hour += 1;if (++hour == 24) {hour = 0;}}}} int hour = 0;int min = 0;int sec = 0;}; Int main (void) {Clock c;c.run ();cout << " Hello World " << endl;return 0;}
This article is from the "enjoy the Joy of Technology" blog, please be sure to keep this source http://liam2199.blog.51cto.com/2879872/1972448
Realization of clock C + + Description with Object-oriented thought