Skip to content

程序填空题

基础题型
#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;
}
答案
  1. :a(a)
  2. virtual
  3. :Base()
  4. this->p = new char[strlen(p)+1]
  5. strcpy(this->p, p)
  6. :Base(obj)
  7. p = new char[strlen(obj.p)+1];
  8. strcpy(p, obj.p)
  9. const char*
  10. const
  11. this == &rhs
  12. delete[] p;
  13. Base::operator=(rhs)
  14. p = new char[strlen(rhs.p)+1]
  15. strcpy(p, rhs.p)
  16. return *this;
  17. delete[] p
  18. dynamic_cast<Derived*>(p)
  19. q
  20. delete 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
}
答案
  1. int Shape::counter = 0;
  2. void Shape::error() const { cout << "Shape error ID=" << objectID() << endl; }
  3. lax(l), sax(s)
  4. double area() override { return PI * lax * sax; }
  5. void error() const override { cout << "Ellipse error ID=" << objectID() << endl; }
  6. int Ellipse::counter = 0;
  7. Ellipse(r, r)
  8. counter++;
  9. // Circle does not override error (uses Ellipse's)
  10. // (nothing)
  11. int Circle::counter = 0;
  12. width(w), length(l)
  13. if (width != length) counter++;
  14. double area() override { return width * length; }
  15. void error() const override { cout << "Rectangle error ID=" << objectID() << endl; }
  16. int Rectangle::counter = 0;
  17. Rectangle(r, r)
  18. counter++;
  19. // Square does not override error (uses Rectangle's)
  20. // (nothing)
  21. int Square::counter = 0;
  22. a(a), b(b), c(c)
  23. double area() override { double s = (a+b+c)/2.0; return sqrt(s*(s-a)*(s-b)*(s-c)); }
  24. void error() const override { cout << "Triangle error ID=" << objectID() << endl; }
  25. 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; });
}
答案
  1. NULL(或 nullptr
  2. T
  3. Array<T>
  4. Array<T>::
  5. data[i]
  6. Array<T>
  7. Array<T>::
  8. iterate(f)
  9. <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';
}
答案
  1. template
  2. BinaryOp1
  3. BinaryOp2
  4. op1
  5. *first1, *first2
  6. ++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】

答案
  1. : P(left, right)
  2. bool P::flag = false
  3. p1->x = 1
  4. s.x = 3
  5. S3P2P1P3