gpt4 book ai didi

c++ - 如何在 'int main' 中使用头函数?

转载 作者:行者123 更新时间:2023-11-28 08:16:36 25 4
gpt4 key购买 nike

我正在开发一个项目,该项目在头文件中定义了一个类 (Time),目标是在我的主函数中使用该类来确定两个时间之间的差异。我已经复习了 this is class 但我无法理解使用为我定义的类并在 main 中使用它的访问器函数的概念。

我会发布标题和我到目前为止所做的事情,希望有人能澄清我需要做什么,因为我理解目标和我需要做什么来实现它,但我似乎无法将其转化为可用的代码...希望有人能用比我的老师和我的课文更容易让像我这样的新手程序员理解的术语来表达。

标题:

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

最佳答案

我想您在为 Time 实例声明对象时遇到了一些问题。我承认 C++ 语法有时会让我犯规,但我认为您需要将 void Time(int hour, int min, int sec); 替换为 Time startTime; 之类的内容; ,并将 time_t now(); 替换为 Time stopTime;

然后执行 int durationInSeconds = stopTime.seconds_from(startTime); 并将 durationInSeconds 报告为打字所花费的时间。

关于c++ - 如何在 'int main' 中使用头函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7546430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com