C++ 如何使用time类和函数显示时间 #包括 #包括 使用名称空间std; 上课时间 { 受保护的: 国际人力资源; 公众: 无效设置时间() { 现在时间=时间(0); tm*ltm=localtime(&now); 显示(1+ltm->tm_小时); } 无效显示(INTA) { hr=a; cout
一旦我从另一个答案中得到了这个答案,那么请尝试以下方法:C++ 如何使用time类和函数显示时间 #包括 #包括 使用名称空间std; 上课时间 { 受保护的: 国际人力资源; 公众: 无效设置时间() { 现在时间=时间(0); tm*ltm=localtime(&now); 显示(1+ltm->tm_小时); } 无效显示(INTA) { hr=a; cout,c++,class,C++,Class,一旦我从另一个答案中得到了这个答案,那么请尝试以下方法: #include <iostream> #include <ctime> using namespace std; class time { protected: int hr; public: void settime() { time_t now = time(0); tm *ltm = loc
#include <iostream>
#include <ctime>
using namespace std;
class time
{
protected:
int hr;
public:
void settime()
{
time_t now = time(0);
tm *ltm = localtime(&now);
display(1 + ltm->tm_hour);
}
void display(int a)
{
hr=a;
cout<<hr;
}
};
int main( )
{
time t;
t.settime();
return 0;
}
time\u t=time(0);//立即获取时间
struct tm*now=localtime(&t);
cout tm_year+1900)事实上,这不是问题的正确答案(代码)。问题中明确要求创建构造函数。因此无需定义settime()单独运行。您应该创建一个构造函数,并将当前时间代码放在该构造函数中。我在下面给出了一个代码。请澄清您的问题,伙计
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< endl;
#包括
#包括
使用名称空间std;
上课时间
{
受保护的:
国际人力资源;
公众:
时间()
{
现在时间=时间(0);
tm*ltm=localtime(&now);
显示(1+ltm->tm_小时);
}
无效显示(INTA)
{
hr=a;
如果要使用当前时间初始化类对象成员,则不能。
在类和类的构造函数中添加time_t变量和tm类型指针
#include <iostream>
#include <ctime>
using namespace std;
class time
{
protected:
int hr;
public:
time()
{
time_t now = time(0);
tm *ltm = localtime(&now);
display(1 + ltm->tm_hour);
}
void display(int a)
{
hr=a;
cout<<hr;
}
}
int main( )
{
time t;
return 0;
}
在构造函数中
class time {
private :
time_t now;
tm *ltm;
...
};
您需要在类中使用tm
或time\t
类型的私有表示来存储时间
您需要使用当前时间在构造函数中初始化它
如有必要,添加一种方法以根据时间打印时间
/*
*时间
*1.提供一个能够使用当前
在C++中声明的时间()函数的时间
用于初始化的对象的标准库标头
时间课。
*
*创建日期:2014年8月8日
*作者:米罗
*/
#包括
#包括
使用名称空间std;
#ifndef时间_
#定义时间_
上课时间{
私人:
现在是时候了;
结构tm*ltm;
公众:
无效勾号();
void showSec();
时间();
虚拟时间();
tm*&getLtm();
无效setLtm(tm*<m);
time_t getNow()常量;
void setNow(时间现在);
};
#endif/*时间*/
/*
*Time.cpp
*
*创建日期:2014年8月8日
*作者:
*/
#包括
使用名称空间std;
#包括“Time.h”
#包括
时间::时间(){
//TODO自动生成的构造函数存根
setNow(现在);
setLtm(ltm);
}
时间::~Time(){
//TODO自动生成的析构函数存根
}
无效时间::勾选(){
int secund=ltm->tm_sec++;
请解释一下:我不太了解这门课,这是真的吗
time::time()
{
now = time(0);
tm = localtime(&now);
}
/*
* Time.h
* 1.Provide a constructor that is capable of using the current
time from the time() function declared in the C++
Standard Library header <ctime> to initialize an object of
the Time class.
*
* Created on: Aug 8, 2014
* Author: miro
*/
#include <ctime>
#include <iostream>
using namespace std;
#ifndef TIME_H_
#define TIME_H_
class Time {
private:
time_t now;
struct tm *ltm;
public:
void tick();
void showSec();
Time();
virtual ~Time();
tm*& getLtm() ;
void setLtm( tm*& ltm);
time_t getNow() const;
void setNow(time_t now);
};
#endif /* TIME_H_ */
/*
* Time.cpp
*
* Created on: Aug 8, 2014
* Author:
*/
#include <iostream>
using namespace std;
#include "Time.h"
#include <ctime>
Time::Time() {
// TODO Auto-generated constructor stub
setNow(now);
setLtm(ltm);
}
Time::~Time() {
// TODO Auto-generated destructor stub
}
void Time::tick() {
int secund = ltm->tm_sec++;
cout << secund << " ";
//cout << tm;
// time_t t = time(0); // get time now
// cout << t;
// struct tm * now = localtime( & t );
// cout << (now->tm_year + 1900) << '-'
// << (now->tm_mon + 1) << '-'
// << now->tm_mday << '/'
// << now->tm_hour << ':'
// << now->tm_min << ':'
// << now->tm_sec++
// << endl;
//now->tm_sec++;
}
tm*& Time::getLtm() {
return ltm;
}
void Time::setLtm(tm*& ltm) {
this->ltm = ltm;
}
time_t Time::getNow() const {
return now;
}
void Time::setNow(time_t now) {
this->now = now;
}
void Time::showSec() {
now = time(0);
ltm = localtime(&now);
cout << (ltm->tm_hour) << ':' << ltm->tm_min << ':' << ltm->tm_sec++;
}