| | |
Classes
![]() |
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,
C++ Syntax (Toggle Plain Text)
class time { public: time (int uren = 0, int minuten = 0) : hours (uren), minutes (minuten) {} time operator+ (const time &a) { while (a.minutes > 0) { if (minutes >= 60) { minutes = a.minutes - 60; hours += 1; } else minutes +=minutes; } return (hours, minutes); } void print()const { cout<< "The time is now: " << hours << "Hr " << minutes << "." <<endl; } private: int hours, minutes; }; int main() { time t0(23, 59), t1; t0.print(); t1 = t0 + 120; t1.print(); return 0; }
Thanks for the assistance
You may want to choose a different name other than time, but since you are adding minutes, wouldn't it be like this?
C++ Syntax (Toggle Plain Text)
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
•
•
•
•
Originally Posted by JoBe
Why choose a different name
•
•
•
•
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
"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
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
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
>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.
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
I GOT IT
Wohooooo :cheesy:
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:
Rest of the questions will come later, thanks
Wohooooo :cheesy: C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; class time { public: time (int uren = 0, int minuten = 0) : hours (uren), minutes (minuten) {} time operator+ (int min) { minutes += min; while (minutes >= 60) { minutes -= 60; hours += 1; if (hours >= 24) hours = 0; } return *this; } void print()const { hours < 10 ? cout<<"The time is now: " << setw(2) << hours <<" Hr "<< minutes << "." <<endl: cout<<"The time is now: " << setw(0) << hours <<" Hr "<< minutes << "." <<endl; } private: int hours, minutes; }; int main() { time t0(23, 47), t1; t0.print(); t1 = t0 + 59; t1.print(); return 0; }
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:
C++ Syntax (Toggle Plain Text)
time t0(23, 59), t1; // t0 = 23 Hr 59. t1 = t0 + 120; // t1 = 1 Hr 59.
>what would you do different
I wouldn't waste my time performing the calculations manually:
I wouldn't waste my time performing the calculations manually:
C++ Syntax (Toggle Plain Text)
#include <ctime> #include <iomanip> #include <iostream> using namespace std; class Time { public: Time ( int hours = 0, int minutes = 0 ) { time_t t = time ( 0 ); data = *localtime ( &t ); data.tm_hour = hours; data.tm_min = minutes; normalize(); } Time operator+ ( int minutes ) { data.tm_min += minutes; normalize(); return *this; } friend ostream& operator<< ( ostream& out, const Time& t ) { char save = out.fill ( '0' ); out<< setw ( 2 ) << t.data.tm_hour <<':' << setw ( 2 ) << t.data.tm_min; out.fill ( save ); return out; } private: void normalize() { time_t t = mktime ( &data ); data = *localtime ( &t ); } private: struct tm data; }; int main() { Time t0(23, 59), t1; cout<< t0 <<endl; t1 = t0 + 120; cout<< t1 <<endl; }
I'm here to prove you wrong.
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?
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 :!:
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?
C++ Syntax (Toggle Plain Text)
hours < 10 ? cout<<"The time is now: " << setw(2) << hours <<" Hr "<< minutes << "." <<endl: 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 :!:
>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:
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.:
>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?
Would be a start.
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:
C++ Syntax (Toggle Plain Text)
time temp ( hours, minutes ); return temp;
C++ Syntax (Toggle Plain Text)
return temp ( hours, minutes ); // More likely to be optimized
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?
C++ Syntax (Toggle Plain Text)
cout<<"The time is now: "<< setw(hours < 10 ? 2 : 0) << hours <<" Hr "<< minutes <<"."<<endl;
I'm here to prove you wrong.
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- How do i do chat program using MFC (Microsoft Foundation Classes) and Visual Basic? (Visual Basic 4 / 5 / 6)
- Loading classes from a .jar file (Java)
- Help with Classes (C++)
- Understanding classes (C++)
- Working with objects of different Classes (Java)
- Classes (C++)
- Summer Classes (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: why won't my virtual area function work?
- Next Thread: minor problem with vectors and OOP
| Thread Tools | Search this Thread |
ace_thread api array assignment based binary bitmap borland c++ c++endcode c/c++ char class classes code coding compile connect console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multidimensional multiple news node output pointer problem program programming project python random read recursion reference remote reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






