程序填空题¶
基础题型
#include <iostream>
using namespace std;
class Base {
private:
int a;
public:
Base(int a=0)【填空1】{}
virtual const char* what_am_i() const {
return "Base\n";
}
【填空2】~Base() {}
};
class Derived:public Base {
char *p;
public:
Derived(char *p)【填空3】{
【填空4】;
【填空5】;
}
Derived(const Derived& obj)【填空6】{
【填空7】;
【填空8】;
}
【填空9】 what_am_i()【填空10】{
return "Derived\n";
}
Derived& operator=(const Derived &rhs) {
if(【填空11】)
return *this;
【填空12】;
【填空13】;
【填空14】;
【填空15】;
【填空16】;
}
void strings() const {
cout << p << endl;
}
~Derived() {
【填空17】;
}
};
void main()
{
Base *p;
p = new Derived("hello");
Derived *q;
q = 【填空18】;
if(【填空19】)
q->strings();
cout << p->what_am_i();
cout << (*p).what_am_i();
【填空20】;
}
答案
:a(a)virtual:Base()this->p = new char[strlen(p)+1]strcpy(this->p, p):Base(obj)p = new char[strlen(obj.p)+1];strcpy(p, obj.p)const char*constthis == &rhsdelete[] p;Base::operator=(rhs)p = new char[strlen(rhs.p)+1]strcpy(p, rhs.p)return *this;delete[] pdynamic_cast<Derived*>(p)qdelete p
图形程序
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159
class Shape {
private:
int ID;
static int counter;
public:
Shape() : ID(counter++) {}
int objectID() const { return ID; }
virtual void error() const ;
virtual double area() = 0;
static int getcounter() { return counter; }
};
【填空1】
/* Default error handling function provided by base class Shape, to display default code for error.*/
【填空2】
class Ellipse: public Shape {
private:
int lax,sax;
static int counter;
public:
Ellipse(int l,int s): 【填空3】{
if (lax!=sax) counter++;
}
【填空4】
/* Ellipse class to handle errors */
【填空5】
static int getcounter() { return counter; }
};
【填空6】
class Circle: public Ellipse {
public:
Circle(int r): 【填空7】{
【填空8】
}
static int getcounter() { return counter; }
【填空9】
/* The Circle class does not want to make any special behavior for the error */
【填空10】
private:
static int counter;
};
【填空11】
class Rectangle: public Shape {
protected:
int width,length;
static int counter;
public:
Rectangle(int w,int l): 【填空12】{
【填空13】;
}
【填空14】
/* Rectangle class to handle errors */
【填空15】
static int getcounter() { return counter; }
};
【填空16】
class Square: public Rectangle {
public:
Square(int r): 【填空17】{
【填空18】;
}
【填空19】
/* The Square class does not want to make any special behavior for the error */
【填空20】
static int getcounter() { return counter; }
private:
static int counter;
};
【填空21】
class Triangle: public Shape {
int a,b,c;
static int counter;
public:
Triangle(int a, int b, int c): 【填空22】{
counter++;
}
【填空23】
/* Rectangle class to handle errors */
【填空24】
static int getcounter() { return counter; }
};
【填空25】
int main () {
Shape *list[6] = {
new Ellipse(2,4), new Circle(3),
new Rectangle(3,5), new Square(4),
new Triangle(1,2,2),new Ellipse(1,3)};
for (int i=0; i<6; i++) {
cout << list[i]->area() << '\n';
list[i]->error();
}
cout << Ellipse::getcounter() << endl; //output: 2
cout << Circle::getcounter() << endl; //output: 1
cout << Rectangle::getcounter() << endl; //output: 1
cout << Square::getcounter() << endl; //output: 1
cout << Triangle::getcounter() << endl; //output: 1
}
答案
int Shape::counter = 0;void Shape::error() const { cout << "Shape error ID=" << objectID() << endl; }lax(l), sax(s)double area() override { return PI * lax * sax; }void error() const override { cout << "Ellipse error ID=" << objectID() << endl; }int Ellipse::counter = 0;Ellipse(r, r)counter++;// Circle does not override error (uses Ellipse's)// (nothing)int Circle::counter = 0;width(w), length(l)if (width != length) counter++;double area() override { return width * length; }void error() const override { cout << "Rectangle error ID=" << objectID() << endl; }int Rectangle::counter = 0;Rectangle(r, r)counter++;// Square does not override error (uses Rectangle's)// (nothing)int Square::counter = 0;a(a), b(b), c(c)double area() override { double s = (a+b+c)/2.0; return sqrt(s*(s-a)*(s-b)*(s-c)); }void error() const override { cout << "Triangle error ID=" << objectID() << endl; }int Triangle::counter = 0;
模版函数
#include <iostream>
using namespace std;
template <typename T>
class Array {
public:
Array() {
data = new T[BLK_SIZE];
next = 【填空1】 ;
}
~Array() {
delete [] data;
delete next;
}
T& operator[](int i);
void iterate(void (*f)(T&));
private:
【填空2】 *data; // data of type T
static const int BLK_SIZE=32; // fixed block size
【填空3】 *next; // the next array block
};
template <typename T>
T& 【填空4】 operator[](int i) {
if (i < BLK_SIZE) {
return 【填空5】;
} else {
if (next == NULL) {
next = new 【填空6】;
}
return (*next)[i-BLK_SIZE];
}
}
template <typename T>
void 【填空7】 iterate(void (*f)(T&)) {
for (int i = 0; i < BLK_SIZE; i++) {
f(data[i]);
}
if (next != NULL) {
next-> 【填空8】;
}
}
int main() {
Array 【填空9】 a;
int size = 100;
cin >> size;
for (int i = 0; i < size; i++) {
a[i] = i;
}
a.iterate([](int &x) { cout << x << endl; });
}
答案
NULL(或nullptr)TArray<T>Array<T>::data[i]Array<T>Array<T>::iterate(f)<int>
STL 迭代器
#include <functional>
#include <iostream>
#include <vector>
【填空1】 <class InputIt1, class InputIt2, class T, class BinaryOp1, class BinaryOp2>
T inner_product(InputIt1 first1, InputIt2 last1, InputIt2 first2, T init,
【填空2】 op1, 【填空3】 op2)
{
while (first1 != last1)
{
init = 【填空4】(init, op2(【填空5】));
++first1;
【填空6】;
}
return init;
}
int main() {
std::vector<int> a{0, 1, 2, 3, 4};
std::vector<int> b{5, 4, 2, 3, 1};
int r1 = inner_product(a.begin(), a.end(), b.begin(), 0, std::plus<>(),
std::multiplies<>());
std::cout << "Inner product of a and b: " << r1 << '\n';
int r2 = inner_product(a.begin(), a.end(), b.begin(), 0, std::plus<>(),
std::equal_to<>());
std::cout << "Number of pairwise matches between a and b: " << r2 << '\n';
}
答案
templateBinaryOp1BinaryOp2op1*first1, *first2++first2
额
The following C++ program outputs a string ending with P1P3. Please fill in the blanks in the code based on the given part of the output, and then write the output of the program.
#include <iostream>
using std::cout;
using std::endl;
class P {
public:
static bool flag;
int x;
P *left, *right;
P(P* left = nullptr, P* right = nullptr) : x(0), left(left), right(right) {}
~P() {
if (flag) {
if (left != nullptr) { delete left; }
if (right != nullptr) { delete right; }
} else {
if (right != nullptr) { delete right; }
if (left != nullptr) { delete left; }
}
cout << "P" << x;
}
};
class S : public P {
public:
S(P* left = nullptr, P* right = nullptr)【填空1】(2分) {}
~S() {
cout << "S" << x;
}
};
【填空2】(2分)
int main() {
S* p1 = new S;
【填空3】(2分)
S* p2 = new S;
p2->x = 2;
S s(p1, p2);
【填空4】(2分)
return 0;
}
The output of the program is: (Please write the entire output, including the P1P3 in the end of the output.) 【填空5】
答案
: P(left, right)bool P::flag = falsep1->x = 1s.x = 3S3P2P1P3