被调用函数总结或商品是不正确

问题描述 投票:-2回答:2

当我运行这个功能的总和或产品是非常错误的。我把2和3的输入和有一个像负一百万。这同样适用于该产品了。我去后,我问他们想要做的,使其更自然,其计算再添COUT声明。

#include <iostream>
#include <math.h>
#include <string>
using namespace std;

class basicCalculator {
public:
int add(int x,int y) {
    return add1 + add2;
}

int multiply(int x,int y) {
    return multiply1*multiply2;
}

private:
int add1;
int add2;
int multiply1;
int multiply2;

};

int main() {
cout << "What mathematical action do you want?" << endl;
cout << "Press '1' to add two numbers, '2' to multiply two numbers" << endl;

int method;

cin >> method;

int value1;
cin >> value1;
int value2;
cin >> value2;

basicCalculator bc;

switch (method) {
case 1:
    cout << "The sum is " << bc.add(value1, value2) << endl;
    break;
case 2:
    cout << "The product is " << bc.multiply(value1, value2) << endl;
}

}

c++ class switch-statement
2个回答
2
投票

里面你qazxsw POI和POI qazxsw方法,您正在使用(初始化)成员变量而不是实际的参数。

试着用:

add

一个建议:multiplyint add(int x, int y) { return x + y; } 不需要一个对象状态可言,他们可以是静态的,我看不出有任何理由让你声明的所有成员变量。


1
投票

弗雷迪,在你的职责,你必须使用指定的参数为您计算。所以,不是

add

反而

multiply

同样的问题多次。

© www.soinside.com 2019 - 2024. All rights reserved.