如何使用“int main”中的头函数?

发布于 2024-12-06 12:49:32 字数 1945 浏览 2 评论 0原文

我正在开发一个项目,该项目在头文件中定义了一个类(时间),目标是在我的主函数中使用该类来确定两个时间之间的差异。我已经讨论过这个类,但无法理解使用为我定义的类并在 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;
}    

I am working on a project that has a class (Time) defined in a header file and the objective is to use that class in my main function to determine the difference between two times. I have gone over this is class and yet cannot wrap my head around the concept of using the class that has been defined for me and using it's accessor functions in main.

I will post the header and what I have done so far and hopefully somebody can clarify what I need to do because I understand the objective and what I need to do to accomplish it but I just cannot seem to translate that into usable code... Hopefully someone can put this in terms comprehensible to an novice programmer like myself better than both my teacher and my text.

Header:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骄兵必败 2024-12-13 12:49:32

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

然后执行 inturationInSeconds = stopTime.seconds_from(startTime); 并将urationInSeconds报告为键入所花费的时间。

You're having some trouble with your object declarations for instances of Time, I gather. I'll admit that the C++ syntax fouls me up sometimes, but I think you need to replace the void Time(int hour, int min, int sec); with something like Time startTime;, and replace time_t now(); with something like Time stopTime;.

Then do int durationInSeconds = stopTime.seconds_from(startTime); and report durationInSeconds as the time spent typing.

听风念你 2024-12-13 12:49:32

好的,如果我理解你的问题,你应该做的就是在你的 c 类的顶部添加一个包含,所以对于这种情况,你将添加

#include "cccTime.h" 

(或无论文件名是什么,使用“”而不是像平常那样使用 <>用于包含)

,然后您可以像往常一样调用方法

Ok if I understand your question,what you should do is add an include at the top of your c class, so for this case you will add

#include "cccTime.h" 

(or whatever the file name is, use "" and not <> as you normally use for includes)

and then you can call the methods, as you always do

迟到的我 2024-12-13 12:49:32

你不需要知道现在是什么时间。只需使用默认构造函数即可。默认构造函数“构造一个 Time 对象,该对象设置为构造函数执行的时间。”这使得计算某件事需要多长时间变得非常容易:

  Time start_time;
  do_lots_of_stuff ();
  Time end_time;

现在您可以使用 end_time.seconds_from (start_time) 确定从开始到结束经过了多少时间。

You don't need to know what time it is. Just use the default constructor. The default constructor "Constructs a Time object that is set to the time at which the constructor executes." This makes it very easy to time how long something takes:

  Time start_time;
  do_lots_of_stuff ();
  Time end_time;

Now you can use end_time.seconds_from(start_time) to determine how much time elapsed from start to finish.

来日方长 2024-12-13 12:49:32
#include<iostream>
#include<string>

#include "ccc_time.h"

using namespace std;

int main()
{
  //Time start_time;                                                                                                                                                                                               
  string x;

  const string SAMPLE = "The quick brown fox jumped over the lazy dog";

  cout<< "This program will test your typing speed."<< endl;
  cout<< "Type the following:"<<endl;
  cout<<" "<<endl;
  cout<< SAMPLE << endl;

  getline(cin, x, '\n');

  //Time end_time;                                                                                                                                                                                                 

  if(x.compare(SAMPLE) == 0)
    {
      //int res = 0;                                                                                                                                                                                               
      //res = end_time.seconds_from(start_time);                                                                                                                                                                   
      //cout << res << endl;                                                                                                                                                                                       
      //return res;                                                                                                                                                                                                
    } else {
    cout << "Invalid text entered" << endl;
  }

  return 0;
}

我说得对吗?

#include<iostream>
#include<string>

#include "ccc_time.h"

using namespace std;

int main()
{
  //Time start_time;                                                                                                                                                                                               
  string x;

  const string SAMPLE = "The quick brown fox jumped over the lazy dog";

  cout<< "This program will test your typing speed."<< endl;
  cout<< "Type the following:"<<endl;
  cout<<" "<<endl;
  cout<< SAMPLE << endl;

  getline(cin, x, '\n');

  //Time end_time;                                                                                                                                                                                                 

  if(x.compare(SAMPLE) == 0)
    {
      //int res = 0;                                                                                                                                                                                               
      //res = end_time.seconds_from(start_time);                                                                                                                                                                   
      //cout << res << endl;                                                                                                                                                                                       
      //return res;                                                                                                                                                                                                
    } else {
    cout << "Invalid text entered" << endl;
  }

  return 0;
}

Am I right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文