I have a class “Month” that has void member functions and void parameter passing. I need to overload the constructor (I think) but the compiler doesn’t like my attempts.
class Month
{
public:
Month(char firstLetter, char secondLetter, char thirdLetter); //?
Month::Month(int m) : month(m) {} //Want above ^ to look like this
Month();
void outputMonthNumber();
void outputMonthLetters();
//~Month(); // destructor
private:
int month;
};
void Month::outputMonthNumber()
{
if (month >= 1 && month <= 12)
cout << "Month: " << month << endl;
else
cout << "Not a real month!" << endl;
}
void Month::outputMonthLetters()
{
const char * monthNames[] = {
"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"
};
const char * output = "The number is not a month!";
if (month >= 1 && month <= 12)
output = monthNames[month - 1];
cout << output;
}
Just like how outputting the month numbers were done, I’m trying to do the same for outputMonthNumber but can’t wrap my head around it.
int main(void)
{
int num;
char firstLetter, secondLetter, thirdLetter;
cout << "give me a number between 1 and 12 and I'll tell you the month name: ";
cin >> num;
Month myMonth(num);
myMonth.outputMonthLetters();
cout << endl << "Give me a 3 letter month and I'll give you the month #: ";
cin >> firstLetter >> secondLetter >> thirdLetter;
/*===How would I pass the parameters to the class?===*/
//Month herMonth(firstLetter, secondLetter, thirdLetter);
//herMonth.outputMonthNumber();
}
All member functions of class pass void parameters. I understand how to do it for passing one, but I can’t seem to get it to overload correctly.
Do it like so: