【问题标题】:Classes and Constructor类和构造函数
【发布时间】:2021-08-14 15:29:31
【问题描述】:
using namespace std;
#include <iostream>

class Person {
public:
    int age;
    Person(int initialAge);
    void amIOld();
    void yearPasses();
};

Person::Person(int initialAge)
{
    if (initialAge > 0) {
        initialAge = age;
    }
    else if (initialAge < 0) {
        cout << "Age is not valid, setting age to 0. \n";
        age = 0;
    }

    // Add some more code to run some checks on initialAge
}

void Person::amIOld()
{

    if (age < 13) {
        cout << "You are young. \n";
    }
    else if (age >= 13 && age <= 18) {
        cout << "You are a teenager. \n";
    }
    else if (age > 18) {
        cout << "You are old. \n";
    }
    // Do some computations in here and print out the correct statement to the console
}

void Person::yearPasses()
{
    age = age + 1;
    // Increment the age of the person in here
}

int main()
{
    int t;
    int age;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> age;
        Person p(age);
        p.amIOld();
        for (int j = 0; j < 3; j++) {
            p.yearPasses();
        }
        p.amIOld();

        cout << '\n';
    }

    return 0;
}

输入 4 -1 10 16 18

预期输出:

年龄无效,将年龄设置为 0。 你很年轻。 你还年轻。

你还年轻。 你是个少年。

你是个少年。 你老了。

你老了。 你老了。

我得到的输出是:

年龄无效,将年龄设置为 0。 你很年轻。 你还年轻。

你还年轻。 你还年轻。

你还年轻。 你还年轻。

你还年轻。 你还年轻。

【问题讨论】:

  • initialAge=age; - 这行有什么问题?
  • initialAge == 0你不初始化成员时
  • 为什么我得到的输出是“你还年轻”。不管我提供什么年龄?
  • @SakshiMishra 你明白initialAge=age;age=initialAge; 之间的区别吗?

标签: c++ class constructor


【解决方案1】:

在你的类改变构造函数中

initialAge = age;

age = initialAge;

因为您必须将 initialAge 的值设置为年龄

【讨论】:

    【解决方案2】:

    问题出在构造函数中。 使用初始化列表来避免此类问题。 像

    Person::Person(int initialAge):age(initialAge > 0 ? initialAge : 0)
    {}
    

    【讨论】:

      【解决方案3】:

      您的代码中有一些逻辑错误。

      第一个是在你的构造函数中,你应该设置age = initialAge你做了相反的事情。

      第二个是如果你想让它在 18 岁时说“你老了”,你应该将最后一个 else 子句更改为 else if(age &gt;= 18),并将第二个 if 子句更改为 else if(age &gt;= 13 &amp;&amp; age &lt; 18)。否则,它说你 18 岁时还年轻。

      如果您正在使用小类或对它们进行试验,我建议您在类中声明和定义您的方法。它使阅读更容易。

      这将是您的代码的一个更简单的版本,可以按照您的预期工作:

      #include <iostream>
      using namespace std;
      
      class Person {
      public:
          int age;
          Person(int initialAge){
              if (initialAge >= 0) 
                  age = initialAge;
              else if (initialAge < 0) {
                  cout << "Age is not valid, setting age to Zero.\n";
                  age = 0;
              }
          }
          void amIOld(){
              if (age < 13) 
                  cout << "You are young.\n";
              else if (age >= 13 && age < 18) 
                  cout << "You are a teenager.\n";
              else 
                  cout << "You are old.\n";   
          }
          void yearPasses(){
              age += 1;
          }
      };
      int main()
      {
          int t, age;
          cin >> t;
          for (int i = 0; i < t; i++) {
              cin >> age;
              Person p(age);
              p.amIOld();
              for (int j = 0; j < 3; j++) 
                  p.yearPasses();
              p.amIOld();
              cout << '\n';
          }
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-21
        • 1970-01-01
        • 2011-09-13
        • 2014-11-21
        • 1970-01-01
        • 1970-01-01
        • 2015-10-25
        • 2012-09-03
        • 2012-10-23
        相关资源
        最近更新 更多