How to use a header function in 'int main'?
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 an开发者_开发知识库d 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;
}
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.
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
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.
#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?
精彩评论