欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

北京大学MOOC C++程序设计 程序设计与算法(三)第三周测验

程序员文章站 2024-03-01 09:06:22
...

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 

答案:

#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 {
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 n):b(n)
	{
	    v = n;
	}
};
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:编程填空:统计动物数量

代码填空,使得程序能够自动统计当前各种动物的数量

#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

答案:

#include <iostream>
using namespace std;
class Animal
{
public:
    static int number;
    Animal()
    {
        number++;
    }
    virtual ~ Animal()
    {
        number--;
    }
};
class Dog:public Animal
{
public:
    static int number;
    Dog()
    {
        number++;
    }
    ~Dog()
    {
        number--;
    }
};
class Cat:public Animal
{
public:
    static int number;
    Cat()
    {
        number++;
    }
    ~Cat()
    {
        number--;
    }
};
int Animal::number = 0;
int Cat::number = 0;
int Dog::number = 0;
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();
}

4:这个指针哪来的

填空,按要求输出

#include <iostream>
using namespace std;

struct A
{
	int v;
	A(int vv):v(vv) { }
// 在此处补充你的代码
};

int main()
{
	const A a(10);
	const A * p = a.getPointer();
	cout << p->v << endl;
	return 0;
}

输入

输出

10

答案:

#include <iostream>
using namespace std;

struct A
{
	int v;
	A(int vv):v(vv) { }
    const A * getPointer() const
    {
        return this;
    }
};

int main()
{
	const A a(10);
	const A * p = a.getPointer();
	cout << p->v << endl;
	return 0;
}

 

 

相关标签: cpp