编程题#1
下面程序输出的结果是:
0
5
请填空:
#include <iostream>
using namespace std;
class A {
public:
int val;
// 在此处补充你的代码
};
main() {
A a;
cout << a.val << endl;
a.GetObj() = 5;
cout << a.val << endl;
}
输入 无
输出
0
5
#include <iostream>
using namespace std;
class A {
public:
int val;
A(){val=0;}
int &GetObj()
{return val;}
};
main()
{
A a;
cout << a.val << endl;
a.GetObj() = 5;
cout << a.val << endl;
}
编程题 #2
下面程序的输出是:10
请补足Sample类的成员函数。不能增加成员变量。
#include <iostream>
using namespace std;
class Sample{
public:
int v;
Sample(int n):v(n) { }
// 在此处补充你的代码
};
int main() {
Sample a(5);
Sample b = a;
cout << b.v;
return 0;
}
输入:无
输出:10
#include <iostream>
using namespace std;
class Sample{
public:
int v;
Sample(int n):v(n) { }
Sample (const Sample &c1)
{v=c1.v*2;}
};
int main() {
Sample a(5);
Sample b=a;
cout << b.v;
return 0;
}
编程题 #3
下面程序的输出结果是:
5,5
5,5
#include <iostream>
using namespace std;
class Base {
public:
int k;
Base(int n):k(n) { }
};
class Big {
public:
int v; Base b;
// 在此处补充你的代码
};
int main() {
Big a1(5); Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
return 0;
}
输入:无
输出
5,5
5,5
#include <iostream>
using namespace std;
class Base {
public:
int k;
Base(int n):k(n) { }
};
class Big {
public:
int v; Base b;
// 在此处补充你的代码
};
int main() {
Big a1(5); Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
return 0;
}