MOOC-程序设计与算法(三)C++面向对象程序设计 https://www.icourse163.org/course/PKU-1002029030
配套测试题-OpenJudge http://cxsjsxmooc.openjudge.cn/2018t3fallall
001:简单的swap
描述
填空,使得程序输出结果是:
5,3
#include <iostream>
using namespace std;
class A
{
public:
int x;
int getX() { return x; }
};
void swap(
// 在此处补充你的代码
)
{
int tmp = a.x;
a.x = b.x;
b.x = tmp;
}
int main()
{
A a,b;
a.x = 3;
b.x = 5;
swap(a,b);
cout << a.getX() << "," << b.getX();
return 0;
}
输入
无
输出
5,3
样例输入
无
样例输出
5,3
这题主要考察传引用
源代码
#include <iostream>
using namespace std;
class A
{
public:
int x;
int getX() { return x; }
};
void swap(
class A & a, class A & b
)
{
int tmp = a.x;
a.x = b.x;
b.x = tmp;
}
int main()
{
A a,b;
a.x = 3;
b.x = 5;
swap(a,b);
cout << a.getX() << "," << b.getX();
return 0;
}
003:好怪异的返回值
描述
填空,使得程序输出指定结果
#include <iostream>
using namespace std;
// 在此处补充你的代码
getElement(int * a, int i)
{
return a[i];
}
int main()
{
int a[] = {1,2,3};
getElement(a,1) = 10;
cout << a[1] ;
return 0;
}
输入
无
输出
10
样例输入
无
样例输出
10
这道题考察将引用作为函数的范围值
源代码
#include <iostream>
using namespace std;
int &
getElement(int * a, int i)
{
return a[i];
}
int main()
{
int a[] = {1,2,3};
getElement(a,1) = 10;
cout << a[1] ;
return 0;
}
004:神秘的数组初始化
描述
填空,使得程序输出指定结果
#include <iostream>
using namespace std;
int main()
{
int * a[] = {
// 在此处补充你的代码
};
*a[2] = 123;
a[3][5] = 456;
if(! a[0] ) {
cout << * a[2] << "," << a[3][5];
}
return 0;
}
输入
无
输出
123,456
样例输入
无
样例输出
123,456
这道题考察数组的初始化
源代码
#include <iostream>
using namespace std;
int main()
{
int * a[] = {
0
};
*a[2] = 123;
a[3][5] = 456;
if(! a[0] ) {
cout << * a[2] << "," << a[3][5];
}
return 0;
}
005:编程填空:学生信息处理程序
描述
实现一个学生信息处理程序,计算一个学生的四年平均成绩。
要求实现一个代表学生的类,并且类中所有成员变量都是【私有的】。
补充下列程序中的 Student 类以实现上述功能。
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;
class Student {
// 在此处补充你的代码
};
int main() {
Student student; // 定义类的对象
student.input(); // 输入数据
student.calculate(); // 计算平均成绩
student.output(); // 输出数据
}
输入
输入数据为一行,包括:
姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。
其中姓名为由字母和空格组成的字符串(输入保证姓名不超过20个字符,并且空格不会出现在字符串两端),年龄、学号和学年平均成绩均为非负整数。信息之间用逗号隔开。
输出
输出一行数据,包括:
姓名,年龄,学号,四年平均成绩。
信息之间用逗号隔开。
样例输入
Tom Hanks,18,7817,80,80,90,70
样例输出
Tom Hanks,18,7817,80
这道题考察类的成员变量与成员函数的定义与使用
源代码
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;
class Student {
private:
char firstName[20];
int space;
char lastName[20];
int age;
int id;
int Score[4];
float avgScore;
public:
void input()
{
//scanf("%[A-Za-z] %[A-Za-z],%d,%d,%d,%d,%d,%d", &firstName,&lastName ,&age,&id,&Score[0],&Score[1],&Score[2],&Score[3]);
scanf("%[A-Za-z]",&firstName);
char tmp;
tmp=getchar();
if(tmp==',')
{
space=0;
scanf("%d,%d,%d,%d,%d,%d", &age,&id,&Score[0],&Score[1],&Score[2],&Score[3]);
}
else
{
space=1;
//lastName[0]=tmp;
scanf("%[A-Za-z],%d,%d,%d,%d,%d,%d",&lastName,&age,&id,&Score[0],&Score[1],&Score[2],&Score[3]);
}
}
void calculate()
{
avgScore=(Score[0]+Score[1]+Score[2]+Score[3])/4.0;
}
void output()
{
if (space==1)
cout<<firstName<<" "<<lastName<<","<<age<<","<<id<<","<<avgScore;
else
cout<<firstName<<","<<age<<","<<id<<","<<avgScore;
}
};
int main() {
Student student; // 定义类的对象
student.input(); // 输入数据
student.calculate(); // 计算平均成绩
student.output(); // 输出数据
}
006:奇怪的类复制
描述
程序填空,使其输出9 22 5
#include <iostream>
using namespace std;
class Sample {
public:
int v;
// 在此处补充你的代码
};
void PrintAndDouble(Sample o)
{
cout << o.v;
cout << endl;
}
int main()
{
Sample a(5);
Sample b = a;
PrintAndDouble(b);
Sample c = 20;
PrintAndDouble(c);
Sample d;
d = a;
cout << d.v;
return 0;
}
输入
无
输出
9
22
5
样例输入
None
样例输出
9
22
5
这道题考察无参构造函数、复制构造函数、类型转换构造函数的定义
源代码
#include <iostream>
using namespace std;
class Sample {
public:
int v;
Sample()
{
}
Sample(int a)
{
//cout<<"transf\n";
v=a;
}
Sample(const Sample &a)
{
//cout<<"copy\n";
v=a.v+2;
}
};
void PrintAndDouble(Sample o)
{
cout << o.v;
cout << endl;
}
int main()
{
Sample a(5);
Sample b = a;
PrintAndDouble(b);
Sample c = 20;
PrintAndDouble(c);
Sample d;
d = a;
cout << d.v;
return 0;
}
007:返回什么才好呢
描述
程序填空,使其按要求输出
#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
这道题考察无参构造函数、类型转换构造函数、复制构造函数、this指针
源代码
#include <iostream>
using namespace std;
class A {
public:
int val;
A(int
a)
{
val=a;
}
A()
{
val=123;
}
A(const A &a)
{
val=a.val;
}
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;
}
008:超简单的复数类
描述
下面程序的输出是:
3+4i
5+6i
请补足Complex类的成员函数。不能加成员变量。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
double r,i;
public:
void Print() {
cout << r << "+" << i << "i" << endl;
}
// 在此处补充你的代码
};
int main() {
Complex a;
a = "3+4i"; a.Print();
a = "5+6i"; a.Print();
return 0;
}
输入
无
输出
3+4i
5+6i
样例输入
无
样例输出
3+4i
5+6i
这道题考察的是无参构造函数、类型转换构造函数、字符与整型之间的转换关系
源代码
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
double r,i;
public:
void Print() {
cout << r << "+" << i << "i" << endl;
}
Complex(){}
Complex(char a[])
{
char x, y;
char *add;
add=strchr(a,'+');
r=*(add-1)-48;
i=*(add+1)-48;
}
};
int main() {
Complex a;
a = "3+4i"; a.Print();
a = "5+6i"; a.Print();
return 0;
}
009:哪来的输出
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果
#include <iostream>
using namespace std;
class A {
public:
int i;
A(int x) { i = x; }
// 在此处补充你的代码
};
int main()
{
A a(1);
A * pa = new A(2);
delete pa;
return 0;
}
输入
无
输出
2
1
样例输入
无
样例输出
2
1
这道题考察析构函数
源代码
#include <iostream>
using namespace std;
class A {
public:
int i;
A(int x) { i = x; }
~A()
{
cout<<i<<endl;
}
};
int main()
{
A a(1);
A * pa = new A(2);
delete pa;
return 0;
}
011:Big & Base 封闭类问题
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
int k;
Base(int n):k(n) { }
};
class Big
{
public:
int v;
Base b;
// 在此处补充你的代码
};
int main()
{
int n;
while(cin >>n) {
Big a1(n);
Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
}
}
输入
多组数据,每组一行,是一个整数
输出
对每组数据,输出两行,每行把输入的整数打印两遍
样例输入
3
4
样例输出
3,3
3,3
4,4
4,4
这道题考察封闭类的类型转换构造函数以及复制构造函数
源代码
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
int k;
Base(int n):k(n) { }
};
class Big
{
public:
int v;
Base b;
Big(int x):v(x),b(x)
{
}
Big(const Big &y):v(y.v),b(y.v)
{
}
};
int main()
{
int n;
while(cin >>n) {
Big a1(n);
Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
}
}
019:编程填空:统计动物数量
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
代码填空,使得程序能够自动统计当前各种动物的数量
#include <iostream>
using namespace std;
// 在此处补充你的代码
void print() {
cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}
int main() {
print();
Dog d1, d2;
Cat c1;
print();
Dog* d3 = new Dog();
Animal* c2 = new Cat;
Cat* c3 = new Cat;
print();
delete c3;
delete c2;
delete d3;
print();
}
输入
无
输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
样例输入
None
样例输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
这道题考察 类的继承、类的静态成员变量、派生类的构造与析构
欢迎关注公众号: