| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Classes - Classes and Arrays
I'm having trouble writing this program for a class. Here's the assignment:
Design a class called Date. The class should store a date in three integers: month, day and year. There should be member functions to print the date in the following forms: 12/25/2009 December 25, 2009 25 December 2009 Demonstrate the class by writing a complete program implementing it. Input validation: Do not accept values for month less than 1 and greater than 12. Do not accept values for day less than 1 and greater than 31. Here's what I have. class Date { private: int month; int day; int year; public: void setMonth(int m) { month = m; } void setDay(int d) { day = d; } void setYear(int y) { year = y; } int getMonth() const { return month; } int getDay() const { return day; } int getYear() const { return year; } Date() Date(int, int,) { if (month > 12 || month < 1) cout << "Please enter a number between 1 and 12 representing the month."; if (day > 31 || day < 1) cout << "Please enter a number between 1 and 31 representing the day."; } Date(int, int, int) void showDate1(); void showDate2(); void showDate3(); }; Now I'm stuck. I think I need to create an array and a ptr that will take the int for the month and point to the corresponding name of the month in the array. But I'm not sure how to write that. I'm probably overthinking everything and making it harder than it is. Any help is greatly appreciated. Aaron |
|
#2
|
|||
|
|||
|
Code:
static const char* monthstrings[] = { // Can be a member of the class
"", // 0
"January", // 1
"February",
// etc etc
};
const char* Date::GetMonthText() { return monthstrings[month]; }
You can then have the functions making those strings call this one to know what text to put in.
__________________
Quote:
|
|
#3
|
|||
|
|||
|
A couple issues with the code.
- After you successfully construct a Date object, you can setDay(12541) and setMonth(-50). - After fixing that, you might want to make the constructors call the setX() functions to avoid duplicating the code. In particular: if you later are to add validation against 30th of february etc, that will give you a single place to update. - I would suggest #include <stdexcept> and then, when the input validation fails, do throw std::range_error(); This will force the program to either handle the fact it did something wrong, or terminate. Debuggers tend to allow you to stop right then and there too. Like being there when a car crashes vs figuring out what happened from only the wrecks. - Error messages should (ideally) go out with cerr rather than cout - it keeps them separate in case of piping (moving your output into the input for another program) and you won't ever need to flush them. -This is more than what the task requires, but it might help you in the future: Code:
#include <sstream>
using namespace std;
string Date::get_mmddddyyyy() {
stringstream s;
s << day << ' ' << getMonthText() << ' ' << year;
return s.str();
}
It's also easy to retrofit such functionality on an existing printing function: a parameter (ostream& cout = cout) where you can optionally pass it a stringstream. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Classes - Classes and Arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|