簡體   English   中英

如何在 C++ 中使用 OOP 添加 2 個用戶輸入值並使用 showDetails() 打印

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

#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 個用戶輸入值。我一直在嘗試在 main 內部和 main 外部使用cin但由於某種原因它不起作用。 另外,有人告訴我不要使用 setter 而只使用 getter。 出於某種原因,我仍然不明白如何打印showDetails()

PS 我沒有 C++ 的經驗,只有 Java。

首先,一個迷你代碼審查:

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

Output:

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

我把 getters 留在了里面,但是對於所問的問題,它們可以被刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM