【发布时间】:2011-09-25 15:19:23
【问题描述】:
我正在开发一个项目,该项目在头文件中定义了一个类(时间),目标是在我的主函数中使用该类来确定两次之间的差异。我已经讨论了这个类,但我无法理解使用为我定义的类并在 main.js 中使用它的访问器函数的概念。
我将发布标题以及到目前为止我所做的事情,希望有人能澄清我需要做什么,因为我了解目标以及完成它需要做什么,但我似乎无法将其转化为可用的代码...希望有人能用比我的老师和我的课文更能理解像我这样的新手程序员的术语来表达这一点。
标题:
#ifndef CCC_TIME_H
#define CCC_TIME_H
/**
A class that describes a time of day
(between 00:00:00 and 23:59:59)
*/
class Time
{
public:
/**
Constructs a time of day.
@param hour the hours
@param min the minutes
@param sec the seconds
*/
Time(int hour, int min, int sec);
/**
Constructs a Time object that is set to
the time at which the constructor executes.
*/
Time();
/**
Gets the hours of this time.
@return the hours
*/
int get_hours() const;
/**
Gets the minutes of this time.
@return the minutes
*/
int get_minutes() const;
/**
Gets the seconds of this time.
@return the seconds
*/
int get_seconds() const;
/**
Computes the seconds between this time and another.
@param t the other time
@return the number of seconds between this time and t
*/
int seconds_from(Time t) const;
/**
Adds a number of seconds to this time.
@param s the number of seconds to add
*/
void add_seconds(int s);
private:
int time_in_secs;
};
#endif
___________________
using namespace std;
int main()
{
int t;
string x;
cout<< "This program will test your typing speed."<< endl;
cout<< "Type the following:"<<endl;
cout<<" "<<endl;
cout<< "The quick brown fox jumped over the lazy dog"<< endl;
Time startTime;
getline(cin, x, '\n');
if(x == "The quick brown fox jumped over the lazy dog")
{
Time endTime;
int durationInSeconds = endTime.seconds_from(startTime);
t = durationInSeconds;
}
else{cout<<"Invalid text entered";}
cout<<"You finished in: " << t << "seconds." endl;
system ("pause");
return 0;
}
【问题讨论】:
-
指定这是什么语言可能会有所帮助。可以猜测它是 C++,但也可能是其他几种。
-
我真的很感激任何帮助,我不希望任何人为我写这个我真的可以使用一些帮助(例如,我做错了什么,我必须创建开始和结束时间的变量?等)
-
你应该在你的问题中添加一个作业标签。
-
这应该是做什么的?
void Time(int hour, int min, int sec);这似乎是 Time(...) 构造函数的声明,您已经从 H 文件中获得了该构造函数。我怀疑你需要构造一个时间变量。 -
是的,我认为同样的事情,我最初有 Time() 但我不知道我是否应该有参数或者它们是隐含的?