20152:补足程序2
总时间限制:
1000ms
内存限制:
65536kB
// 在此处补充你的代码
描述
补足程序使得其输出结果是:
40
#include <iostream> #include <string> using namespace std; struct A { int n; A() { }; A(int n_ ):n(n_) { }
}; int main() { A c; const A a(10); c = a + A(30); cout << c.n << endl; return 0; }
输入
无
输出
40
样例输入
无
样例输出
40
注意实际上a带有const关键字,所以函数需要有const,小技巧,C++对const要求严格,注意调试测试
#include <iostream>
#include <string>
using namespace std;
struct A {
int n;
A() { };
A(int n_) :n(n_) { }
// 在此处补充你的代码
friend A operator+(const A &a, const A &b)
{
int num_ = a.n + b.n;
return A(num_);
}
};
int main()
{
A c;
const A a(10);
c = a + A(30);
cout << c.n << endl;
system("pause");
return 0;
}