C ++将变量传递给函数的类

时间:2011-09-29 19:17:28

标签: c++ class

我有一个类“Month”,它有void成员函数和void参数传递。我需要重载构造函数(我认为),但编译器不喜欢我的尝试。

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;
}

就像输出月份数字的方式一样,我正在尝试为outputMonthNumber做同样的事情,但无法绕过它。

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();
}

类的所有成员函数都传递void参数。我理解如何通过一个,但我似乎无法正确地过载。

3 个答案:

答案 0 :(得分:1)

这样做:

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;
};

答案 1 :(得分:1)

除非使用函数,否则不能直接从字母初始化月份。

部首:

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文件:

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;
    }
}

因为我知道会有怀疑者,在http://ideone.com/73TNb为CharsToInt测试案例。作为一个注释,这个神奇的技巧只适用于某些处理器。对于某些编译器,您可能需要重新排列abr中的字母。

答案 2 :(得分:0)

你真正想要的是根据字符串输入设置月份,对吗?试试这个:

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;
}

简而言之,使用std :: find在数组条目中查找传入的名称。 std :: find将返回一个指向string的指针,从中我们减去monthNames数组的地址。这给了我们一个0-11的值(如果没有找到,则为12),我们加1。