【问题描述】
编写 2 个函数,分别是 add 与 mul,分别完成 Int 的加法操作与乘法操作。这个 2 个函数均为 void 类型,拥有 3 个参数。其中前 2 个是输入参数,最后 1 个是输出参数。
【样例输入】
3 8
【样例输出】
11 24
#include <iostream>
using namespace std;
class Int{
private:
int value;
public:
Int():value(0){}
Int(int v):value(v){}
int getValue()const{return value;}
void setValue(int v){value=v;}
};
//实现加法的函数
void add(const Int&a,const Int&b,Int&c)
{
c.setValue(a.getValue()+b.getValue());
}
//实现乘法的函数
void mul(const Int&a,const Int&b,Int&c)
{
c.setValue(a.getValue()*b.getValue());
}
int main(){
int x,y;
cin>>x>>y;
Int a(x),b(y),c,d;
add(a,b,c);
mul(a,b,d);
cout<<c.getValue()<<" "<<d.getValue()<<endl;
return 0;
}
【注】此分栏为西安理工大学C++练习题,所有答案仅供同学们参考。