By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General Discussion - c++ question

Hello,

Just realized I have a c++ assignment due tommorow and that I need to download cygwin again (long story) to be able to make it.

That takes a long time, and I'm going to bed soon.  While I have an idea for how to code this quickly, I need to make sure it is perfect.  Can someone write up a batch of code in case my solution to the assignment doesn't work the first time through (i.e. so I can skip debugging etc)?  Thanks.

Here is what I need to do:

"Write a C++ class called "Time" that represents quantities of time. Note that this is not a time of day, but an amount of time, e.g., 2 hours, 33 minutes. Your class should implement necessary class variables and functions, and in particular you must overload +, -, ==, !=, <<, and >>. << should output time in the HH:MM:SS format, and >> should take input in the same format.

Don't worry about representing increments of time larger than hours or smaller than seconds.

In your main(), you should take input from stdin (using >>) to create two Time objects. Then test to see if they are equal. If not, indicate which is larger."

Any help would be greatly appreciated... 

 



People are difficult to govern because they have too much knowledge.

When there are more laws, there are more criminals.

- Lao Tzu

Around the Network

Meh :)

I won't do the whole thing here/now, but it seems pretty trivial. The key (IMO) is representing time in seconds ONLY (i.e. as a single int) - and then it becomes pretty easy.

i.e.

bool operator == (const Time & rhs)
{
return m_seconds == rhs.m_seconds;
}

Time operator + (const Time & rhs)
{
Time t;

t.Set(m_seconds + rhs.m_seconds); // or use another constructor...
return t;
}

int m_seconds;
...

The only tricky bit is parsing the input into seconds - and even that's pretty easy. Break down input string into three ints - then:

m_second = (hours * 3600) + (minutes * 60) + seconds;

...something like that anyway!



Gesta Non Verba

Nocturnal is helping companies get cheaper game ratings in Australia:

Game Assessment website

Wii code: 2263 4706 2910 1099

uh..what is rhs?

 



People are difficult to govern because they have too much knowledge.

When there are more laws, there are more criminals.

- Lao Tzu

Right-hand side.



Reality has a Nintendo bias.

hmm...don't know what right-hand side is either...(probably shouldn't sleep during class either)

i try to make my programs as simple as possible.  i'm a minimalist



People are difficult to govern because they have too much knowledge.

When there are more laws, there are more criminals.

- Lao Tzu

Around the Network
shams said:
Meh :)

I won't do the whole thing here/now, but it seems pretty trivial. The key (IMO) is representing time in seconds ONLY (i.e. as a single int) - and then it becomes pretty easy.

i.e.

bool operator == (const Time & rhs)
{
return m_seconds == rhs.m_seconds;
}

Time operator + (const Time & rhs)

{
Time t;

t.Set(m_seconds + rhs.m_seconds); // or use another constructor...
return t;
}

int m_seconds;
...

The only tricky bit is parsing the input into seconds - and even that's pretty easy. Break down input string into three ints - then:

m_second = (hours * 3600) + (minutes * 60) + seconds;

...something like that anyway!

the operator+ takes 2 arguments, say lhs and rhs. unless you make it as a member function, but you didn't do that here :)

yeah, you can have a "total_seconds" variable in your class... or just make the comparisons on the fly. since this is an assignment just pick the one you're more comfortable with.

good luck!

edit: same comment with operator==



the Wii is an epidemic.

you guys are making me nervous now...

and cygwin is still only 10% loaded. bah

i think he is mainly concerned with how we overload the functions...



People are difficult to govern because they have too much knowledge.

When there are more laws, there are more criminals.

- Lao Tzu

I didn't write the class wrapper - they are all meant to be member functions - sorry I should have mentioned :)

class Time
{
....

private :
int m_seconds;
};

...etc.



Gesta Non Verba

Nocturnal is helping companies get cheaper game ratings in Australia:

Game Assessment website

Wii code: 2263 4706 2910 1099

TheSource said:

hmm...don't know what right-hand side is either...

Oh, sorry then. I assumed you didn't know what the mnemonic meant.

Rhs there is just the name of the variable. You're overloading operators there, so e.g. when you have a+b the operator+(...) "method" will be called on "object" a with parameter b. Since b is on the right-hand side of + in the a+b expression, it's one possible, albeit common practice to call the parameter to operator+(...) by the name rhs. But you might as well call it x, or something else.

I hope that was more helpful.



Reality has a Nintendo bias.
TheSource said:

you guys are making me nervous now...

and cygwin is still only 10% loaded. bah

i think he is mainly concerned with how we overload the functions...

You have studied operators... right? Know how they work? 

 



Gesta Non Verba

Nocturnal is helping companies get cheaper game ratings in Australia:

Game Assessment website

Wii code: 2263 4706 2910 1099