【问题描述】
编写 2 个函数,分别是 add 与 mul,分别完成 Int 的加法操作与乘法操作。2 个函数均拥有 2 个 Int 常引用的形参,返回类型均为 Int。
【样例输入】
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;}
};
//实现加法的函数
Int add(Int const&a,Int const&b)
{
int c;
c=a.getValue()+b.getValue();
return c;
}
//实现乘法的函数
Int mul(Int const&a,Int const&b)
{
int c;
c=a.getValue()*b.getValue();
return c;
}
int main(){
int x,y;
cin>>x>>y;
Int a(x),b(y);
Int c = add(a,b);
Int d = mul(a,b);
cout<<c.getValue()<<" "<<d.getValue()<<endl;
return 0;
}
【注】此分栏为西安理工大学C++练习题,所有答案仅供同学们参考。