My program is growing but am not noticing any "is a" relationship, or virtual functions... Would have been a nice experience to have those... Any suggestion to add these!
Printable View
My program is growing but am not noticing any "is a" relationship, or virtual functions... Would have been a nice experience to have those... Any suggestion to add these!
is-a and has-a relationships occur as soon as you have more than one class that relates to another class - e.g. you have a contract, that has-a car and has-a driver. A driver "has-a" address, and so on.
Here's the first google hit on "is-a has-a":
http://weblog.raganwald.com/2008/03/is-is-has.html
--
Mats
by using this function i've created...
with this i can set the day the contract is issued and the day it ends. With this info, i need to calculate the number of days the car was hired to bill the customer, but the tricky part is how to determine the number of days by using this structure? say you have issued at 20 June 2008 & returned at to 20 July 2008... Subtracting year - year, month to month, day to day using if statements can be cumbersome... On the other hand i may just subtract seconds i think, by passing the above structure (curr_tm) to localtime function, which will return the seconds.... BUT! will localtime be happy with this structure with only four members initialized (hour, min, mon, year, day)?Code:// Get the local date and time of system
tm getTime()
{
time_t rawtime;
struct tm *timeinfo, curr_tm;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
/* Initialize time structure */
curr_tm = *(timeinfo);
curr_tm.tm_hour += 1;
curr_tm.tm_min += 1;
curr_tm.tm_mon += 1;
curr_tm.tm_year += 1900;
return curr_tm;
}
difftime gives you seconds from one date to another, so you need a function to convert between from internal date fromat to a time_t value, so that you can feed in the in/out date/time of to difftime. Converting seconds to days is just a case of dividing by 86400 (60s * 60m * 24h = 86400 seconds).
--
Mats
by trying to test difftime, i wrote this small program
To my surprise, both timeinfoE & timeinfoS has exactly the same values... Any reason for this?Code:int main ()
{
time_t rawtime;
struct tm *timeinfoS=NULL;
struct tm *timeinfoE=NULL;
// Initialize start time
time ( &rawtime );
timeinfoS = localtime ( &rawtime );
timeinfoS->tm_hour -= 1;
timeinfoS->tm_min -= 1;
timeinfoS->tm_mon -= 1;
timeinfoS->tm_year -= 1900;
// Convert from tm to time_t
time_t start = mktime ( timeinfoS );
// delay about
for(int x = 0; x < 2000; x++) {
for(int y = 0; y < 2000000; y++) {
}
}
// Initialize end time
time ( &rawtime );
timeinfoE = localtime ( &rawtime );
timeinfoE->tm_hour -= 1;
timeinfoE->tm_min -= 1;
timeinfoE->tm_mon -= 1;
timeinfoE->tm_year -= 1900;
// Convert from tm to time_t
time_t end = mktime ( timeinfoE );
double dif = difftime (end,start);
printf ("It took you %.2lf seconds to do nothing.\n", dif );
return 0;
}
The code inside localtime would be something like this:
Since the static lt variable is always the same one, it would be the same address for both calls.Code:struct tm *localTime( ... )
{
static struct tm lt;
...
return <
}
You need to copy the value returned into your own variable.
--
Mats
Code:// Get the local date and time of system
tm Date::getCurrentTime()
{
time_t rawtime;
struct tm *timeinfo, curr_tm;
time( &rawtime ); // error C2064: term does not evaluate to a function taking 1 arguments
timeinfo = localtime ( &rawtime );
curr_tm = *(timeinfo);
return curr_tm;
}
Do you have a declaration of time elsewhere?
Btw, this code can be simplified:
time_t rawtime;
struct tm* timeinfo;
time( &rawtime )
timeinfo = localtime ( &rawtime );
return *timeinfo;
Why use a temporary at all?