CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 25th, 2009, 09:39 AM
    Brian_Jones Brian_Jones is offline
    Member
     
    Join Date: Oct 2009
    Posts: 29
    Brian_Jones is an unknown quantity at this point (<10)
    .h user defined classes

    I am taking a c++ class and my instructor gave us an example of a user defined class that will be on our quiz but only gave us most of the answers of this example, as the last two were supposed to be self-explanatory. Looking over my notes I'm not really sure what "IncDate and AddDays" is supposed to look like. The best I've got so far with AddDays is day++, but that can't be right for a user defined class I would think.

    //start example

    Create a class called Date.

    -Day, Month, Year

    It should also contain following member functions in addition to constructor, accessor and mutator methods:
    Display: Print the date to the screen
    IncDate: Increments the date
    Add Days(num) Add (num) of days to the date

    // end example

    here is what he gave us so far:
    Code:
    Private:
    
    int Day
    int month;
    int year;
    
    Public:
    
    Date ()    //constructor
    day = 1;    //initialize day
    month =1;   //initialize month 
    year = 1900;   //initialize year
    
    getDay()
    return day;
    
    getMonth()
    return month;
    
    getYear()
    return year;
    
    setDay(int x)
    day = x;
    
    setMonth(int y)
    month = y;
    
    setYear(int z)
    year = z;
    
    Display()
    cout << day << ”/” << month << “/” << year;
    these last two I'm not really sure of...

    IncDate: Increments the date
    Add Days(num): Add (num) of days to the date
    Reply With Quote
      #2    
    Old November 25th, 2009, 09:58 AM
    GCDEF GCDEF is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2003
    Posts: 8,168
    GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+)
    Re: .h user defined classes

    Your approach really depends on how robust your instructor wants your date class to be and what else he may have told you. Typically date classes will store the date as just a number which represents an offset from a known date and time. For example, in your case January 1, 1900 would be stored as 0, and January 1, 1901 as 365 or 366 depending on whether 1900 was a leap year or not. When you're working with a base number like that, adding days becomes easy. The part that you need to work on is converting from your internal number to a m/d/y format, and that would require knowledge of which years are leap years and how many days are in each month.

    On the other hand, sometimes for simplicity, the instructor may let you assume every year has 365 days and every month has 30 days which can make it easier. Divide the number of days to be added by 365 and add that to the year. Divide the remainder by 30 and add that to the month. Add the remainder to day. If the day is greater than the 30, subtract 30 and add 1 to the month. If the month is greater than 12, subtract 12 and add 1 to the year.

    Hopefully that'll get you started. How you implement it really depends on exactly what the instructor wants.
    Reply With Quote
      #3    
    Old November 25th, 2009, 10:11 AM
    Brian_Jones Brian_Jones is offline
    Member
     
    Join Date: Oct 2009
    Posts: 29
    Brian_Jones is an unknown quantity at this point (<10)
    Re: .h user defined classes

    Your knowledge of years and leap years all makes sense, but how to fit that into a user defined class is what I'm not sure of.
    Reply With Quote
      #4    
    Old November 25th, 2009, 10:17 AM
    Lindley Lindley is online now
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,905
    Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+)
    Re: .h user defined classes

    Well, the first thing to realize is that IncDate is identical to the special case of AddDays(1). Therefore I'd suggest simply implementing it that way.

    As for how AddDays works, think back to when you were first learning addition. You'd add up the ones, then carry, then add the tens, carry if necessary, etc. Your approach here is going to be similar----you add up the days, then carry the month if necessary, then carry the year if necessary.

    Of course, it's more complicated because the number of days you want to carry after depends on the month, and to a certain extent on the year. I would suggest doing something like
    Code:
    while (days > daysInMonth[month])
    {    
          days -= daysInMonth[month]
          month++;
          if (month == 12)
          {
               month = 0;
               year++;
          }
    }
    Of course, that disregards leap-year logic. I'll leave it you to to fit that in.
    Reply With Quote
      #5    
    Old November 25th, 2009, 10:38 AM
    Brian_Jones Brian_Jones is offline
    Member
     
    Join Date: Oct 2009
    Posts: 29
    Brian_Jones is an unknown quantity at this point (<10)
    Re: .h user defined classes

    I see where you're going and looks like the GCDEF was trying to explain that to me as well. I think I was being hard-headed with the mindset that if statements, loops and logic operators were not allowed in .h files, but rather just in .cpp files. Thank you for the help!
    Reply With Quote
      #6    
    Old November 25th, 2009, 10:44 AM
    GCDEF GCDEF is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2003
    Posts: 8,168
    GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+)
    Re: .h user defined classes

    Quote:
    Originally Posted by Brian_Jones View Post
    I see where you're going and looks like the GCDEF was trying to explain that to me as well. I think I was being hard-headed with the mindset that if statements, loops and logic operators were not allowed in .h files, but rather just in .cpp files. Thank you for the help!
    It's allowed, but typically a class is defined in a .h file and implemented in a .cpp file.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:11 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.