c++实现显示系统当前时间
2008-11-25 13:28
288 查看
小朋友托我写的显示时间的控制台小程序,不熟悉时间类,查了下资料,参考上一篇转载博文.另外关于析构函数有点新的收获,通过指针申请的内存空间在主函数退出前需要显示地去释放空间以调用析构函数.而简单的申明对象在主函数退出时会自动调用析构函数. 下面是我显示系统当前时间的源代码.
/*
程序功能:
显示系统当前时间
*/
#include <iostream>
#include "time.h"
using namespace std;
class Clock
{
public:
Clock(time_t pt=time(NULL))
{
t=pt;
local=localtime(&t);
nHour=local->tm_hour;
nMinute=local->tm_min;
nSecond=local->tm_sec;
}
~Clock()
{
cout<<"clock destruction OK!"<<endl;
}
private:
time_t t;
tm *local;
int nHour;
int nMinute;
int nSecond;
friend ostream& operator<<(ostream& out,Clock& clock)//重载操作符<<,输出时间
{
out<<clock.nHour<<":";
if(clock.nMinute<10)
out<<"0";
out<<clock.nMinute<<":";
if(clock.nSecond<10)
out<<"0";
out<<clock.nSecond;
out<<endl;
delete &clock;//释放空间
return out;
}
};
int main()
{
Clock *clock=new Clock();
cout<<"Local Time is:"<<endl;
cout<<*clock;
return 0;
}
/*
程序功能:
显示系统当前时间
*/
#include <iostream>
#include "time.h"
using namespace std;
class Clock
{
public:
Clock(time_t pt=time(NULL))
{
t=pt;
local=localtime(&t);
nHour=local->tm_hour;
nMinute=local->tm_min;
nSecond=local->tm_sec;
}
~Clock()
{
cout<<"clock destruction OK!"<<endl;
}
private:
time_t t;
tm *local;
int nHour;
int nMinute;
int nSecond;
friend ostream& operator<<(ostream& out,Clock& clock)//重载操作符<<,输出时间
{
out<<clock.nHour<<":";
if(clock.nMinute<10)
out<<"0";
out<<clock.nMinute<<":";
if(clock.nSecond<10)
out<<"0";
out<<clock.nSecond;
out<<endl;
delete &clock;//释放空间
return out;
}
};
int main()
{
Clock *clock=new Clock();
cout<<"Local Time is:"<<endl;
cout<<*clock;
return 0;
}
相关文章推荐
- Linux C++显示系统当前时间 time
- C/C++控制动态显示当前系统时间
- 基于javascript实现动态显示当前系统时间
- C或C++调用Windows系统函数 实现延时 或 获取当前时间的处理
- 基于javascript实现动态显示当前系统时间
- 使用js实现显示系统当前时间并实现倒计时功能并触发模态框(遮罩)功能
- Cocos2d-x中用C++获取当前系统时间,实现跨平台
- C/C++中三种方法显示当前系统时间 localtime time.h
- jsp实现页面实时显示当前系统时间的方法
- C++实现当前时间动态显示的方法
- jsp实现页面实时显示当前系统时间的方法
- web页面显示当前系统时间并定时刷新
- C/C++获取当前系统时间
- VS2010用MFC显示系统当前时间
- 【机房收费系统】时间控件的使用及显示当前电脑的名称
- jquery easyui datebox 时间控件默认显示当前日期的实现方法
- MFC 实现 状态栏动态显示系统时间
- 汇编,实现显示当前时间
- python显示当前系统时间
- C/C++获取当前系统时间