C++数字时钟输出系统当前时间

刚刚学习类与对象,一个小练习~

时间精确到秒~

#include 
#include 
#include 
#include 

using namespace std;

class Clock{
private:
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
    void checkTime(){//私有成员函数
        if (year<0||month<0||month>12||day<0||day>31||hour<0||hour>23||minute<0||minute>59||second<0||second>59) {
            cout<<"illegal time\n";
            exit(1);//退出程序
        }
    }
public:
    void setTime(int newYear,int newMonth,int newDay,int newHour,int newMinute,int newSecond){
        year=newYear;
        month=newMonth;
        day=newDay;
        hour=newHour;
        minute=newMinute;
        second=newSecond;
        checkTime();//调用私有成员函数,检验时间的合法性
    }
    void showTime(){
        cout<<"Current time:"<tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
    c.showTime();
    return 0;
}

 

你可能感兴趣的:(C++数字时钟输出系统当前时间)