北京大學MOOC C++程式設計 程式設計與演算法(三)第三週測驗
阿新 • • 發佈:2018-12-16
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;
}