简体   繁体   English

C ++类:传递参数

[英]C++ Class: Passing a parameter

I'm just learning classes so I'm trying something basic. 我只是在学习课程,所以我在尝试一些基本的东西。 I have a class called Month as shown below. 我有一个名为“月”的课程,如下所示。 For my first test, I want to supply a number from 1 through 12 and output the name of the month ie. 对于我的第一个测试,我想提供一个从1到12的数字并输出月份的名称,即。 1 = Jan. 1 =一月

class Month
{
public:
    Month (char firstLetter, char secondLetter, char thirdLetter);    // constructor
    Month (int monthNum);
    Month();
    void outputMonthNumber();
    void outputMonthLetters();
    //~Month();   // destructor
private:
    int month;
};
Month::Month()
{
    //month = 1; //initialize to jan
}
void Month::outputMonthNumber()
{
  if (month >= 1 && month <= 12)
    cout << "Month: " << month << endl;
  else
    cout << "Not a real month!" << endl;
}

void Month::outputMonthLetters()
{
  switch (month)
    {
    case 1:
      cout << "Jan" << endl;
      break;
    case 2:
      cout << "Feb" << endl;
      break;
    case 3:
      cout << "Mar" << endl;
      break;
    case 4:
      cout << "Apr" << endl;
      break;
    case 5:
      cout << "May" << endl;
      break;
    case 6:
      cout << "Jun" << endl;
      break;
    case 7:
      cout << "Jul" << endl;
      break;
    case 8:
      cout << "Aug" << endl;
      break;
    case 9:
      cout << "Sep" << endl;
      break;
    case 10:
      cout << "Oct" << endl;
      break;
    case 11:
      cout << "Nov" << endl;
      break;
    case 12:
      cout << "Dec" << endl;
      break;
    default:
      cout << "The number is not a month!" << endl;
    }
}

Here is where I have a question. 这是我有问题的地方。 I want to pass "num" into the outputMonthLetters function. 我想将“ num”传递给outputMonthLetters函数。 How do I do this? 我该怎么做呢? The function is void, but there must be some way to get the variable into the class. 该函数是无效的,但是必须有某种方法可以将变量添加到类中。 Do I have to make the "month" variable public? 我必须公开“ month”变量吗?

int main(void)
{
    Month myMonth;
    int num;
    cout << "give me a number between 1 and 12 and I'll tell you the month name: ";
    cin >> num;
    myMonth.outputMonthLetters();
}

What you probably want to do is something like this: 您可能想做的是这样的:

int num;
cout << "give me a number between 1 and 12 and I'll tell you the month name: ";
cin >> num;
Month myMonth(num);
myMonth.outputMonthLetters();

Note that myMonth isn't declared until it's needed, and the constructor taking the month number is called after you determine what month number you are looking for. 请注意,直到需要时才声明myMonth,并在确定要查找的月份号之后调用采用月份号的构造函数。

Try using a parameter on the Method 尝试在方法上使用参数

void Month::outputMonthLetters(int num);

Than you could do: 比您可以做的:

Month myMonth;
int num;
cout << "give me a number between 1 and 12 and I'll tell you the month name: ";
cin >> num;
myMonth.outputMonthLetters(num);

I'm not the C++ guru, but don't you have to create an instance of Month? 我不是C ++专家,但是您是否不必创建Month的实例?

Change your 改变你的

void Month::outputMonthLetters() 

to

static void Month::outputMonthLetters(int num) 
{
    switch(num) {
    ...
    }
}

ie add a parameter to the method, and (optionally) make it static. 即向该方法添加参数,并(可选)使其为静态。 But this is not a very good example of a class to start with... 但这不是一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM