Greetings,
Looking through your statement, I did notice the "Use a private member function to set the month name based on the month number" quote.
Let's disect this statment, as it holds the key of the entire answer. Let's take it from the end, back.
Firstly, "based on the month number" seems to state that the month number has to be given by the user. Secondly, "Use a private member function" tells us we need to create a function in the private section of our class. Lastly, "set the month name" must tell us that this must be a variable; either public or private. I would suggest private as noting the fact they want a private function.
Let's take a closer look:
class Date {
public:
void SetDate(int, int, int);
void ShowDate(void);
private:
char *MonthName(int);
int year, month, day;
};
This may not make sense, and I added some stuff in there they don't talk about. There is one function called SetDate() in the public area. Why is this here? So we can change year, month, and day in our private section without access warnings/errors.ShowDate() on the other hand will print the final string. Our MonthName() function will be designed to take the month number in and return the name corresponding to the month given. Let me help you get started and show you how SetDate() will work:
void Date::SetDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
Remember, once you are inside the class function, you have direct access to private members. Since year, month, and day are initialized in the private section of the class, our function SetDate() takes our variables from anywhere and modifies the private members behind the scenes.
The function MonthName is not hard at all, just use a simple if/else statement, and all will fall under place:
char *Date::MonthName(int m) {
if (m == 1)
return "January";
return NULL;
}
Simple, if it is 1 return the string January, though if not, return nothing.
ShowDate() will use year, month and day, convert month to string and display to the screen what the current year/month/day string it is.
I hope this helps. If you have any further questions, please feel free to ask.
-Stack Overflow