1:返回什么才好呢
程序填空,使其按要求输出
#include <iostream>
using namespace std;
class A {
public:
int val;
A(int
// 在此处补充你的代码
};
int main()
{
int m,n;
A a;
cout << a.val << endl;
while(cin >> m >> n) {
a.GetObj() = m;
cout << a.val << endl;
a.GetObj() = A(n);
cout << a.val<< endl;
}
return 0;
}
输入
多组数据,每组一行,是整数 m 和 n
输出
先输出一行:
123
然后,对每组数据,输出两行,第一行是m,第二行是n
样例输入
2 3
4 5
样例输出
123
2
3
4
5
代码难点
a.GetObj() = A(n);刚开始对这句话怎么都不理解,现在看来就是this指针的应用,首先a.GetObj()的返回对象为A&类型也就是* this指针,this此时的当前对象为a,这里*this=A(n),表示this->A(n),指向a对象里面的构造函数A(int n)。
函数返回值如果不是引用,就不能放在赋值号左边
答案代码
#include <iostream>
using namespace std;
class A {
public:
int val;
A(int
n)
{
val=n;
}
A()
{
val=123;
}
A& GetObj()
{
return *this;
}
};
int main()
{
int m,n;
A a;
cout << a.val << endl;
while(cin >> m >> n) {
a.GetObj() = m;
cout << a.val << endl;
a.GetObj() = A(n);
cout << a.val<< endl;
}
return 0;
}
2:Big & Base 封闭类问题
程序填空,输出指定结果
#include <iostream>
#include <string>
using namespace std;
class Base {