11--2机器当前时间(time库函数)

来源:互联网 发布:下载picsart软件 编辑:程序博客网 时间:2024/06/05 16:46

本函数用于输出机器当前的时间

tim.h:头文件

 

#include<iostream.h>class tim{    public:      tim();//构造函数,初始化数据成员      void print();    private:      int year;      int mon;      int day;      int hour;      int min;      int sec;};tim::tim(){    tm *local;    time_t t;//time_t 类型    t=time(NULL);    local=localtime(&t);    year=local->tm_year+1900;    mon=local->tm_mon+1;    day=local->tm_mday;    hour=local->tm_hour;    min=local->tm_min;    sec=local->tm_sec;}void tim::print(){    cout<<"机器当前时间:"<<endl        <<year<<"年"<<mon<<"月"<<day<<"日"        <<hour<<"时"<<min<<"分"<<sec<<"秒"<<endl;}


主函数:

 

#include <iostream>#include<time.h>#include"tim.h"using namespace std;int main(){    tim a;    a.print();    return 0;}/*class Time{    public:      Time();//构造函数,初始化数据成员      void print();    private:      int year;      int mon;      int day;      int hour;      int min;      int sec;};Time::Time(){    tm *local;    time_t t;    t=time(NULL);    local=localtime(&t);    year=local->tm_year+1901;    mon=local->tm_mon+1;    day=local->tm_mday;    hour=local->tm_hour;    min=local->tm_min;    sec=local->tm_sec;}void Time::print(){    cout<<"机器当前时间:"<<endl        <<year<<"年"<<mon<<"月"<<day<<"日"        <<hour<<"时"<<min<<"分"<<sec<<"秒"<<endl;}*/