Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; My program is growing but am not noticing any "is a" relationship, or virtual functions... Would have been a nice ...

  1. #136
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    polymorphism/ "is a"

    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!

  2. #137
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #138
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    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
    My program does not show inheritance AFAIK ... just encapsulation

  4. #139
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    My program does not show inheritance AFAIK ... just encapsulation
    Yes, so you don't have the "is-a" structure there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #140
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    return/issue dates

    by using this function i've created...

    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;
    }
    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)?

  6. #141
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #142
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    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

    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 &#37;.2lf seconds to do nothing.\n", dif );
    
    	return 0;
    }
    To my surprise, both timeinfoE & timeinfoS has exactly the same values... Any reason for this?

  8. #143
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by csonx_p View Post
    by trying to test difftime, i wrote this small program

    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;
    }
    To my surprise, both timeinfoE & timeinfoS has exactly the same values... Any reason for this?
    timeinfoE & timeinfoS seem to be pointing at the same memory because when i change timeinfoE , timeinfoES gets changed to the same value..

  9. #144
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    timeinfoE & timeinfoS seem to be pointing at the same memory because when i change timeinfoE , timeinfoES gets changed to the same value..
    Of course, it's a static variable in the function that is returned, so it IS the same pointer value.

    You need to copy the result.

    I'd also recommend using Sleep() [windows] or sleep() [linux/unix] to "wait for a bit".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #145
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Of course, it's a static variable in the function that is returned, so it IS the same pointer value.
    I don't understand....

  11. #146
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    I don't understand....
    The code inside localtime would be something like this:
    Code:
    struct tm *localTime( ... )
    {
       static struct tm lt; 
       ... 
       return &lt;
    }
    Since the static lt variable is always the same one, it would be the same address for both calls.

    You need to copy the value returned into your own variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #147
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    The code inside localtime would be something like this:
    Code:
    struct tm *localTime( ... )
    {
       static struct tm lt; 
       ... 
       return &lt;
    }
    Since the static lt variable is always the same one, it would be the same address for both calls.

    You need to copy the value returned into your own variable.

    --
    Mats
    Oh! cool

  13. #148
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Strange error?

    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;
    }

  14. #149
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #150
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    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?
    oh yes, as a string variable, thnx....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 08:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2002, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21