| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
.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; IncDate: Increments the date Add Days(num): Add (num) of days to the date |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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++;
}
}
|
|
#5
|
|||
|
|||
|
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!
|
|
#6
|
|||
|
|||
|
Re: .h user defined classes
Quote:
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|