当前位置:首页 > 通信资讯 > 正文

c++ 时间函数(c语言的时间函数)

实现类的定义,以及调用

Clock时间类的头文件Clock.h

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 //#pragma once #ifndef _CLOCK_H_ #define _CLOCK_H_ class Clock { public: void Init(int hour, int minute, int second); void Display(); void Update(); int GetHour(); int GetMinute(); int GetSecond(); void SetHour(int hour); void SetMinute(int minute); void SetSecond(int second); private: int hour_; int minute_; int second_; }; #endif // _CLOCK_H_

Clock时间类的实现文件Clock.cpp

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include "Clock.h" #include <iostream> #include <Windows.h> using namespace std; void Clock::Display()//显示类对象的时间成员变量 { cout<<hour_<<":"<<minute_<<":"<<second_<<endl; } void Clock::Init(int hour, int minute, int second)//将时间初始化 { hour_ = hour; minute_ = minute; second_ = second; } void Clock::Update()//时钟对象的递进增加 { Sleep(1000); //正常延时1秒钟 second_++; //秒累加 if (second_ == 60) { minute_++; second_ = 0; } if (minute_ == 60) { hour_++; minute_ = 0; } if (hour_ == 24) { hour_ = 0; } } int Clock::GetHour()//获取小时信息 { return hour_; } int Clock::GetMinute()//获取分钟信息 { return minute_; } int Clock::GetSecond()//获取秒信息 { return second_; } void Clock::SetHour(int hour)//设置小时信息 { hour_ = hour; } void Clock::SetMinute(int minute)//设置分钟信息 { minute_ = minute; } void Clock::SetSecond(int second)//设置秒信息 { second_ = second; }

main——时间运行主函数 main.cpp

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include "Clock.h" #include <Windows.h> #include <conio.h> int main(void) { Clock tt; tt.Init(0, 0, 0); //初始化时间 tt.Display(); //将初始化的时间显示 char input_key; input_key=getch(); //等待任意键按下 while(input_key!=27) //判断这个键是否是Esc退出键 { if (kbhit()) //判断是否有键按下 { input_key=getch(); } tt.Update(); tt.Display(); } return 0; }

以上所述就是本文的全部内容了,希望能够对大家学习C++有所帮助。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。