C++笔试题3

C++笔试题(第3套)
时间:20分钟 总分:100分
姓名: 分数:

  1. 填空题(每小题2分)
    ① 禁止类被继承的关键字是_______________
    ② 强制编译器不生成默认构造函数的关键字是_______________
    ③ 修饰常量成员函数、禁止修改类成员变量的关键字是_______________
    ④ 实现类型强制转换、编译期安全的C++强转换关键字是_______________(写出最常用的一种即可)
    ⑤ 允许lambda表达式捕获全局变量外所有变量的隐式捕获符号是_______________

int main() {
    const int a = 100;
    int &ref = const_cast<int&>(a);
    ref = 200;
    cout << a << " " << ref;
    return 0;
}
输出结果:________________
struct A {
    char c;
    int num;
    short s;
};
int main() {
    cout << sizeof(A);
    return 0;
}
64位平台输出结果:________________
int main() {
    unsigned int a = 10;
    int b = -20;
    if (a + b > 0) cout << "true";
    else  cout << "false";
    return 0;
}
输出结果:________________
  1. 如下去除容器中的奇数
int main() {
    vector<int> v = {1,2,3,4,5};
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (*it % 2 == 0) {
            v.erase(it);
        }
    }
    return 0;
}
问题:________________________________________________________________________________
修改:________________________________________________________________________________
class Base {
public:
    Base() { cout << "B"; }
    ~Base() { cout << "~B"; }
};
class Derived : public Base {
public:
    Derived() { cout << "D"; }
    ~Derived() { cout << "~D"; }
};
int main() {
    Base* p = new Derived();
    delete p;
    return 0;
}
输出结果:________________
  1. 指出代码所有错误
class Base {
public:
    virtual void func(int a) {}
};
class Derived : public Base {
public:
    void func(double a) {}
};
错误说明:________________
template<int N>
struct Sum {
    static const int val = N + Sum<N-1>::val;
};
template<>
struct Sum<0> {
    static const int val = 0;
};
int main() {
    cout << Sum<10>::val;
    return 0;
}
输出结果:________________
class Node {
public:
    shared_ptr<Node> next;
    ~Node() { cout << "del"; }
};
int main() {
    auto p1 = make_shared<Node>();
    auto p2 = make_shared<Node>();
    p1->next = p2;
    p2->next = p1;
    return 0;
}
输出结果:________________
存在问题:________________
int main() {
    int x = 10;
    auto f = [&x](){
        x += 5;
        return x;
    };
    int& y = x;
    y = 20;
    cout << f()  << x;
    return 0;
}
输出结果:________________
posted @ 2026-05-22 19:10  ForwardX10  阅读(7)  评论(0)    收藏  举报