大家好,看看我的代码我正在尝试制作一个程序,要求您以克为单位输入第一个值,第二个值是千克,然后通过重载 + 运算符将千克转换为克,但它不起作用,为什么
#include <iostream>
using namespace std;
class ADD{
private:
int Fval;
int Sval;
public:
ADD(){
cout << "WELCOME TO OUR PROGRAM"<<endl<<"PLEASE ENTER THE FIRST VALUE BY GRAMS :";
cin >> Fval;
cout << "PLEASE ENTER THE SECOND VALUE BY KILOGRAMS :"; cin >> Sval;
}
ADD operator+(ADD& add){
add.Sval *= 1000;
return add;
}
int plus(){
return Fval+Sval;
}
};
int main(){
ADD a1;
cout << "THE TOTAL VALUE = " << a1.plus() << " GRAMS";
}
没有效果看输出
WELCOME TO OUR PROGRAM
PLEASE ENTER THE FIRST VALUE BY GRAMS :2
PLEASE ENTER THE SECOND VALUE BY KILOGRAMS :3
THE TOTAL VALUE = 5 GRAMS
这意味着 + 运算符不会将 3 乘以 1000 为什么?