Classes

Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Classes

 
0
  #1
Apr 5th, 2005
Hello ladies and gents,

Ive written this class time in wich I have to use operator+ to increase the hour with a certain amount of minutes like this:

time t0 (23, 59), t1; // t0 = 23.59
t1 = t1 + 120; // t1 = 1.59

Problem is, I can't get those 120 minutes into the operator+

I know that the calculation part isn't correct yet, but I'll sort that out later, I first want to be able to get those 120 minutes into the class :!:
Please, do not give the solution as to how I should do the calculation, ONLY how I can get those 120 minutes into the class

This is the program Ive written sofar,
  1. class time
  2. {
  3. public:
  4.  
  5. time (int uren = 0, int minuten = 0) : hours (uren), minutes (minuten) {}
  6.  
  7. time operator+ (const time &a)
  8. {
  9. while (a.minutes > 0)
  10. {
  11. if (minutes >= 60)
  12. {
  13. minutes = a.minutes - 60;
  14. hours += 1;
  15. }
  16. else
  17. minutes +=minutes;
  18. }
  19.  
  20. return (hours, minutes);
  21. }
  22.  
  23. void print()const
  24. {
  25. cout<< "The time is now: " << hours << "Hr " << minutes << "." <<endl;
  26. }
  27.  
  28. private:
  29. int hours, minutes;
  30. };
  31.  
  32. int main()
  33. {
  34. time t0(23, 59), t1;
  35.  
  36. t0.print();
  37.  
  38. t1 = t0 + 120;
  39. t1.print();
  40.  
  41. return 0;
  42. }


Thanks for the assistance
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Classes

 
0
  #2
Apr 5th, 2005
You may want to choose a different name other than time, but since you are adding minutes, wouldn't it be like this?
  1. mytime operator+ (int min)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Classes

 
0
  #3
Apr 5th, 2005
Why choose a different name

When I change it the way you say, I get two error messages:

error C2143: syntax error : missing ';' before '+'
error C2501: 'mytime' : missing storage-class or type specifiers
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Classes

 
0
  #4
Apr 5th, 2005
Originally Posted by JoBe
Why choose a different name
Because time is a standard function.

Originally Posted by JoBe
When I change it the way you say, I get two error messages:

error C2143: syntax error : missing ';' before '+'
error C2501: 'mytime' : missing storage-class or type specifiers
Yeah, if you change the class name, you have to change it everywhere. I needed to do so to get it to compile -- it wouldn't as time (but that's likely a compiler issue).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Classes

 
0
  #5
Apr 5th, 2005
Oh, now I understand, almost got it working tough, even with the use of class time

I'm using Visual C 6.0, wich one do you use?

Only have to figure out how to correctly add the minutes towards the hour, but I'm almost there, I'll show you when it's finished so you could comment on it and perhaps improve it

Ive got other questions aswell, but I'll ask them when I figured out the program.

Thanks for the help Dave
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Classes

 
0
  #6
Apr 5th, 2005
>I'm using Visual C 6.0, wich one do you use?

Borland 5.5, generally.

>Only have to figure out how to correctly add the minutes towards the hour

While minutes is greater than 59, increment hours and subtract 60 from minutes.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Classes

 
0
  #7
Apr 5th, 2005
I GOT IT Wohooooo :cheesy:

  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. class time
  7. {
  8. public:
  9.  
  10. time (int uren = 0, int minuten = 0) : hours (uren), minutes (minuten) {}
  11.  
  12. time operator+ (int min)
  13. {
  14. minutes += min;
  15. while (minutes >= 60)
  16. {
  17. minutes -= 60;
  18. hours += 1;
  19. if (hours >= 24) hours = 0;
  20. }
  21.  
  22. return *this;
  23. }
  24.  
  25. void print()const
  26. {
  27. hours < 10 ? cout<<"The time is now: " << setw(2) << hours <<" Hr "<< minutes << "." <<endl:
  28. cout<<"The time is now: " << setw(0) << hours <<" Hr "<< minutes << "." <<endl;
  29. }
  30.  
  31. private:
  32. int hours, minutes;
  33. };
  34.  
  35. int main()
  36. {
  37. time t0(23, 47), t1;
  38.  
  39. t0.print();
  40.  
  41. t1 = t0 + 59;
  42. t1.print();
  43.  
  44. return 0;
  45. }

Hi guys,

Hehe, Ive got a few questions, but the first one that I'm gonna ask is, what would you do different or what would you simplify in this exercise?

One thing to keep in mind is, in main you have to use this code:
  1.  
  2. time t0(23, 59), t1; // t0 = 23 Hr 59.
  3. t1 = t0 + 120; // t1 = 1 Hr 59.
Rest of the questions will come later, thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Classes

 
0
  #8
Apr 5th, 2005
>what would you do different
I wouldn't waste my time performing the calculations manually:
  1. #include <ctime>
  2. #include <iomanip>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Time {
  8. public:
  9. Time ( int hours = 0, int minutes = 0 )
  10. {
  11. time_t t = time ( 0 );
  12. data = *localtime ( &t );
  13.  
  14. data.tm_hour = hours;
  15. data.tm_min = minutes;
  16.  
  17. normalize();
  18. }
  19.  
  20. Time operator+ ( int minutes )
  21. {
  22. data.tm_min += minutes;
  23. normalize();
  24.  
  25. return *this;
  26. }
  27.  
  28. friend ostream& operator<< ( ostream& out, const Time& t )
  29. {
  30. char save = out.fill ( '0' );
  31.  
  32. out<< setw ( 2 ) << t.data.tm_hour <<':'
  33. << setw ( 2 ) << t.data.tm_min;
  34. out.fill ( save );
  35.  
  36. return out;
  37. }
  38. private:
  39. void normalize()
  40. {
  41. time_t t = mktime ( &data );
  42. data = *localtime ( &t );
  43. }
  44. private:
  45. struct tm data;
  46. };
  47.  
  48. int main()
  49. {
  50. Time t0(23, 59), t1;
  51.  
  52. cout<< t0 <<endl;
  53. t1 = t0 + 120;
  54. cout<< t1 <<endl;
  55. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Classes

 
0
  #9
Apr 6th, 2005
Waw Narue, that's what you could call, changing the program

I'm a bit baffled :eek: and actually don't know wich question to ask first

Well, here it goes:

1) > I wouldn't waste my time performing the calculations manually:
Is that why you are using the header file <ctime> ?

2) When I look at what I wrote and what you wrote, yours is much more complicated! So, what advantage has your version got over mine?
- Speed?
- Portability?
- ...

Questions about my program:

3) What is the difference in using *this and time(hours, minutes) in my program, because I switched these and both work perfectly in returning the values of hours and minutes
- I know '*this' is a pointer, but pointer for/from what ?
- By using time(hours, minutes), is this related towards the constructor ?

4) Maybe a stupid question, but does it matter in wich order you write private and public in a class?

5) Could I write this piece shorter, not using your alternative?
  1. hours < 10 ? cout<<"The time is now: " << setw(2) << hours <<" Hr "<< minutes << "." <<endl:
  2. cout<<"The time is now: " << setw(0) << hours <<" Hr "<< minutes << "." <<endl;

There are other questions about your code, but I'm gonna give it several test drives to see wether I'm able to understand what's happening in your code and will comeback on them.

Thanks for the alternative solution Narue :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Classes

 
0
  #10
Apr 6th, 2005
>Is that why you are using the header file <ctime> ?
The standard library already defines date and time functions and types (inherited from C). The reside in the <ctime> header. The ones I used were the tm struct, time_t, localtime, and mktime.

>yours is much more complicated!
Only if you aren't familiar with date and time handling in the standard library. The complexity in your version comes from operator+, where you have a loop and a bunch of magic numbers. A reader would need to verify that your math is correct as well as the algorithm performing the math. In my version it's a simple matter of making sure that I passed all of the right types to mktime and localtime. So the complexity is only skin deep.

>what advantage has your version got over mine?
Portability because the standard library handles the nitty gritty stuff. Flexibility because while you would need to design, implement, and test new features (such as adding seconds) with extensive algorithmic changes, I would only need to make a few minor and easily checked modifications. And scalability because by adding features your code will grow by leaps and bounds, mine will grow by only a handful of lines even if a lot of new functionality is added.

>What is the difference in using *this and time(hours, minutes) in my program
The former makes a copy of itself and returns it, the latter creates a new object. It's a subtle difference, and both will generally work the same with this class.

>I know '*this' is a pointer, but pointer for/from what ?
this could be better called self. It's a pointer to the object that the member functions are called on behalf of. So if I'm an object of L33tCoder, then this points to me, and *this is me.

>By using time(hours, minutes), is this related towards the constructor ?
Yes, you're calling the constructor directly and returning the result. It's just the same as saying:
  1. time temp ( hours, minutes );
  2. return temp;
In theory at least. Your compiler is more likely to optimize away any temporary objects by returning the constructor directly rather than creating your own temporary object.:
  1. return temp ( hours, minutes ); // More likely to be optimized
>does it matter in wich order you write private and public in a class?
It can, but you're not likely to encounter such a situation for quite a while.

>Could I write this piece shorter, not using your alternative?
  1. cout<<"The time is now: "<< setw(hours < 10 ? 2 : 0) << hours <<" Hr "<< minutes <<"."<<endl;
Would be a start.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC