2014-03-12 22 views
0
#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()函數當前時間 - 在C標準庫頭的時間宣佈。 h - 初始化時間類的一個對象。 我對班級不太瞭解,這是真的嗎。 任何人都可以幫助我嗎?如何使用時間類和函數

+0

請破譯這樣的:'我沒有那麼瞭解有關類,並且是這種方式是TRUE' – Drop

回答

0

一旦我從另一個答案中得到了這個在某些地方的SO。試試這個:

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; 

希望它可以幫助..)

1

其實這是不正確的答案(代碼)質疑。有問題明確要求創建構造函數。所以不需要分別定義settime()函數。您應該創建一個構造函數並將當前時間代碼放入該構造函數中。我已經在下面給出了一個代碼。請清楚你的問題傢伙。

#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; 
} 
0

如果您想用當前時間初始化您的類對象成員。 在你的類和構造你的類

class time { 
      private : 
      time_t now; 
      tm *ltm; 
      ... 
     }; 

添加time_t的變量和TM型指針在構造函數中

time::time() 
{ 
     now = time(0); 
     tm = localtime(&now); 
} 
0
  1. 你需要在類tm類型或time_t的私人代表存儲時間。

  2. 您需要使用當前時間在構造函數中初始化它。

  3. 根據需要添加打印時間的方法。

0
/* 
* 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_ */ 
0
/* 
* 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++; 
}