如何在 C++ 中使用 OOP 添加 2 个用户输入值并使用 showDetails() 打印

How to add 2 user input values using OOP in C++ and print with showDetails()

提问人:Kaiki 提问时间:12/9/2022 更新时间:12/9/2022 访问量:81

问:

#include <iostream>

using namespace std;

class Arithmetic{
    private:
        int x;
        int y;
    
    public:
        Arithmetic(int num1, int num2){
            x = num1;
            y = num2;
        }
        
        Arithmetic(){
        }
        
        ~Arithmetic(){
        }
        
        int getNum1(){
            return x;
        }
        
        int getNum2(){
            return y;
        }
        
        int add(){
            return getNum1() + getNum2();
        }
        
        void showDetails(){
            cout << add();
        }
};

int main(){
    
    Arithmetic a;
    
    int num1, num2;
    
    cout << "Enter Num1: ";
            cin >> num1;
    cout << "Enter Num2: ";
            cin >> num2;        
    a.showDetails();
    
    return 0;
}

我的任务是在 C++ 中使用面向对象的编程添加 2 个用户输入值。我一直在尝试在主电源内部和外部使用,但由于某种原因它不起作用。另外,我被告知不要使用二传手,而只使用吸气器。出于某种原因,我仍然不明白如何打印.cinshowDetails()

附言我没有C++经验,只有Java。

C++ OOP 整数 IOSTREAM CIN

评论

0赞 273K 12/9/2022
Arithmetic a(num1, num2);
0赞 tadman 12/9/2022
提示:operator+。这里的功能有点毫无意义。当您已经有权访问该数据时,无需调用这些函数。add
0赞 sweenish 12/9/2022
当然,在 Java 中,您知道如何使用数据构建对象吗?你把数据放进了 和 ,然后对它们没有执行任何操作。你应该做的是获取输入,然后将输入提供给你的构造函数。您的操作顺序是错误的。num1num2Arithmetic
0赞 PaulMcKenzie 12/9/2022
附言我没有C++的经验,只在Java中 - 好吧,至少你没有犯许多Java犯的错误,那就是:.-- .Arithmetic* a = new Arithmetic(num1, num2);

答:

0赞 sweenish 12/9/2022 #1

首先,小代码回顾:

#include <iostream>

using namespace std;  // Bad practice

class Arithmetic{
    private:
        int x;  // Prefer default member initialization
        int y;
    
    public:
        Arithmetic(int num1, int num2){
            // Prefer using the initialization section
            x = num1;
            y = num2;
        }
        
        Arithmetic(){
          // Should be defaulted and paired with default member initialization
        }
        
        ~Arithmetic(){  // Unnecessary; Rule of Zero
        }
        
        int getNum1(){  // Should be const
            return x;
        }
        
        int getNum2(){  // Should be const
            return y;
        }
        
        int add(){
            // No need to invoke getters, and should be const
            return getNum1() + getNum2();
        }
        
        void showDetails(){
            cout << add();
        }
};

int main(){
    
    Arithmetic a;
    
    int num1, num2;
    
    cout << "Enter Num1: ";
            cin >> num1;  // Poor formatting
    cout << "Enter Num2: ";
            cin >> num2;

    // This is where your issue occurs. You never put the data into the object.
    // Declare 'a' here, and use your parameterized constructor.         
    a.showDetails();
    
    return 0;
}

您的问题在很大程度上是操作顺序。

#include <iostream>

class Arithmetic {
 private:
  int x = 0;
  int y = 0;

 public:
  Arithmetic(int num1, int num2) : x(num1), y(num2) {}

  Arithmetic() = default;

  int getNum1() const { return x; }

  int getNum2() const { return y; }

  int add() const { return x + y; }

  void showDetails() const { std::cout << add(); }
};

int main() {
  int num1;
  int num2;

  std::cout << "Enter Num1: ";
  cin >> num1;
  std::cout << "Enter Num2: ";
  cin >> num2;

  Arithmetic a(num1, num2);
  a.showDetails();

  return 0;
}

输出:

❯ ./a.out 
Enter Num1: 5
Enter Num2: 7
12

我保留了 getters,但对于所提出的问题,它们可以被删除。