dukevin
dukevin

Reputation: 23178

C++ Passing variables into a class to a function

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.

Upvotes: 0

Views: 7100

Answers (3)

Mooing Duck
Mooing Duck

Reputation: 66922

You can't initialize month directly from letters, unless you use a function.

Header:

int CharsToInt(char firstLetter, char secondLetter, char thirdLetter);
class Month
{
public:
    Month(char firstLetter, char secondLetter, char thirdLetter) 
    : month(CharsToInt(firstLetter, secondLetter, thirdLetter)) {}
    Month(int m) : month(m) {}
    Month();

cpp file:

int CharsToInt(char firstLetter, char secondLetter, char thirdLetter)
{
    firstLetter= std::tolower(firstLetter);
    secondLetter = std::tolower(secondLetter);
    thirdLetter = std::tolower(thirdLetter);
    unsigned int abr = (firstLetter<<CHAR_BIT<<CHAR_BIT) | (secondLetter<<CHAR_BIT) | thirdLetter;
    switch (abr) {
    case 'jan': return 1;
    case 'feb': return 2;
    case 'mar': return 3;
    case 'apr': return 4;
    case 'may': return 5;
    case 'jun': return 6;
    case 'jul': return 7;
    case 'aug': return 8;
    case 'sep': return 9;
    case 'oct': return 10;
    case 'nov': return 11;
    case 'dec': return 12;
    default: return -1;
    }
}

Since I know there will be doubters, test case for CharsToInt at http://ideone.com/73TNb. As a note, this magic trick will only work on certain processors. You may have to rearrange the letters in abr for some compilers.

Upvotes: 1

Benjamin Lindley
Benjamin Lindley

Reputation: 103703

What you really want is to set the month based on string input, correct? Try this:

Month(const std::string & month_name)
{
    static const std::string monthNames[] = {
        "Jan","Feb","Mar","Apr","May","Jun",
        "Jul","Aug","Sep","Oct","Nov","Dec"
    };

    month = std::find(monthNames, monthNames + 12, month_name) - monthNames + 1;
}

What, that does, in a nutshell, use std::find to find the passed in name among the array entries. std::find will return a pointer to string, from which we subtract the address of the monthNames array. That gives us a value from 0-11(or 12 if it's not found), to which we add 1.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258608

Do it like so:

class Month
{
public:
    Month(char firstLetter, char secondLetter, char thirdLetter) :
        first(firstLetter),second(secondLetter),third(thirdLetter)
    {
       const char * monthNames[] = {
        "Jan","Feb","Mar","Apr","May","Jun",
        "Jul","Aug","Sep","Oct","Nov","Dec"
       };
       for ( int i = 0 ; i < 12 ; i++ )
          if ( monthNames[i][0] == first && monthNames[i][1] == second && monthNames[i][2] == third )
              month = i + 1;
    }
    Month::Month(int m) : month(m) {}  //Want above ^ to look like this
    Month();
    void outputMonthNumber();
    void outputMonthLetters();
    //~Month();   // destructor
private:
    int month;
    char first;
    char second;
    char third;
};

Upvotes: 1

Related Questions