First: I know how to write the program, so I’m not asking for help with that. However, I am pasting a copy of the problem so you can see what the assignment entails. My question is specifically aimed at where do you place variables to keep from making everything global?
Assignment
Design a class called Date that has integer data members to store month, day, and year. The class should have a three-parameter default constructor that allows the date to be set at the time a new Date object is created. If the user creates a Date object without passing any arguments, or if any of the values passed are invalid, the default values of 1, 1, 2001 (i.e., January 1, 2001) should be used. The class should have member functions to print the date in the following formats:
3/15/10
March 15, 2010
15 March 2010
Questions
1) The teacher has instructed us to avoid using magic numbers in our code, so the first question is regarding my implementation of the default constructor:
// These are outside the class.
#define DEFAULT_MONTH 1
#define DEFAULT_DAY 1
#define DEFAULT_YEAR 2001
// This is inside the class definition.
Date(int month = DEFAULT_MONTH, int day = DEFAULT_DAY, int year = DEFAULT_YEAR);
Is this correct?
2) The class needs access to an array of string objects which hold the month names so I can use them for date outputs which display the month name instead of the month number. I used an enum for the numeric month (which will be used for the switch).
const enum MONTH_IDS { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
const string MONTH_NAMES[NUM_MONTHS] = { "January", "February", "March",
"April", "May", "June", "July", "August", "September", "October",
"November", "December" };
The question for this part, is where do you place them?
Some things I can’t do…
I am not allowed to use static class members yet because that will be covered in the next chapter. We also have not gone over pointers, but we can use references.
Thanks for your help!
I would ask the instructor but he is out of town and the assignment is due tomorrow.
1) Defines are ugly.
static const intmembers are what I would do, but you can’t … How about enums?2) A static member array is just what you need. But since you can’t … maybe static local variables: