C++二级操作题真题【内附解析】

简介: 本系列C++编程题来自题库cpp.code2ji.cn,涵盖四大核心知识点:①类与对象(Score总评计算,考察构造函数、字符串拷贝及四舍五入);②继承与多态(Shape图形解码,实现虚函数调用);③链式栈实现(Entry结点与内存管理);④抽象类与接口设计(Switchable灯光控制)。题目含填空、纠错与详解,适合巩固面向对象编程基础。

来源题库:cpp.code2ji.cn
在这里插入图片描述


题目一:总评计算(Score 类)

题目描述
设计一个 Score 类,用于存储课程名、学号、平时成绩、期中成绩、期末成绩。

  • 通过构造函数初始化全部数据
  • 修正学号拷贝错误
  • 实现 getFinal() 函数计算总评成绩,公式为:
    平时20% + 期中30% + 期末50%,结果四舍五入为整数
  • 输出学号、课程名和总评成绩

题库出处cpp.code2ji.cn
在这里插入图片描述


初始代码(带空格待填)

#include <iostream>
#include <cstring>
using namespace std;

class Score {
   
public:
    Score(const char *the_course, const char *the_id, int the_normal, int the_midterm, int the_end_of_term)
        : course(the_course), normal(the_normal), midterm(the_midterm), end_of_term(the_end_of_term) {
   
        // ERROR **********found**********
        strcpy(__1__, __2__);
    }
    const char *getCourse() const {
    return course; }
    // ERROR **********found**********
    const char *getID() const {
    return __3__; }
    int getNormal() const {
    return normal; }
    int getMidterm() const {
    return midterm; }
    int getEndOfTerm() const {
    return end_of_term; }
    int getFinal() const;  // 计算总评成绩

private:
    const char *course;     // 课程名称
    char student_id[12];    // 学号
    int normal;             // 平时成绩
    int midterm;            // 期中成绩
    int end_of_term;        // 期末成绩
};

// ERROR **********found**********
int Score::getFinal() const {
   
    return (int)(normal * 0.2 + midterm * 0.3 + end_of_term * 0.5 + 0.5);
}

int main() {
   
    char English[] = "英语";
    Score score(English, "12345678", 68, 83, 92);
    cout << "学号:" << score.getID() << "   ";
    cout << "课程:" << score.getCourse() << "   ";
    cout << "总评成绩:" << score.getFinal() << endl;
    return 0;
}

填空答案

位置 填写内容 说明
1 student_id 目标数组,存放学号字符串
2 the_id 源字符串,构造函数参数传入的学号
3 student_id 返回学号成员变量地址,注意是数组名代表指针

代码逐句讲解

Score(const char *the_course, const char *the_id, int the_normal, int the_midterm, int the_end_of_term)
    : course(the_course), normal(the_normal), midterm(the_midterm), end_of_term(the_end_of_term) {
   
    strcpy(student_id, the_id);
}
  • 构造函数使用初始化列表给课程名和成绩赋值。
  • 注意学号是 char student_id[12],数组不能直接赋值,必须用 strcpy 复制字符串。
  • 这里传入的学号 the_id 复制到成员数组 student_id
const char *getID() const {
    return student_id; }
  • 返回学号字符数组的地址,student_id 本身是指向字符数组的指针。
  • 注意不要写成 &student_id,那是数组地址,不是字符串指针。
int Score::getFinal() const {
   
    return (int)(normal * 0.2 + midterm * 0.3 + end_of_term * 0.5 + 0.5);
}
  • 计算总评成绩,平时占20%,期中占30%,期末占50%。
  • 最后加 0.5 是四舍五入技巧,转换成整数。
int main() {
   
    char English[] = "英语";
    Score score(English, "12345678", 68, 83, 92);
    cout << "学号:" << score.getID() << "   ";
    cout << "课程:" << score.getCourse() << "   ";
    cout << "总评成绩:" << score.getFinal() << endl;
    return 0;
}
  • 测试程序,构造一个英语课程成绩对象。
  • 依次输出学号、课程和总评成绩。

题目二:图形解码(抽象类与派生类)

题目描述
给定抽象类 Shape 和派生类 Triangle,实现一个 show(Shape *shape) 函数,输出图形名称、周长、面积。

题库出处cpp.code2ji.cn
在这里插入图片描述


初始代码(需补全 show 函数)

#include "shape.h"
#include <iostream>
using namespace std;

Triangle::Triangle(Point a, Point b, Point c) : p1(a), p2(b), p3(c) {
   }

string Triangle::name() {
   
    return "三角形";
}

double Triangle::perimeter() {
   
    return p1.distance(p2) + p2.distance(p3) + p3.distance(p1);
}

double Triangle::area() {
   
    double a = p1.distance(p2);
    double b = p2.distance(p3);
    double c = p3.distance(p1);
    double s = (a + b + c) / 2.0;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}

//**********found**********
void show(Shape *shape) {
    cout << "此图形是一个" << shape->name()
         << ",  周长=" << shape->perimeter()
         << ",  面积=" << shape->area() << endl;
}

int main() {
    Triangle *tri = new Triangle(Point(0,2), Point(2,0), Point(0,0));
    show(tri);
    return 0;
}

代码逐句讲解

void show(Shape *shape) {
   
  • 接收一个 Shape 类指针,可以指向基类或派生类对象。
cout << "此图形是一个" << shape->name()
     << ",  周长=" << shape->perimeter()
     << ",  面积=" << shape->area() << endl;
  • 通过虚函数调用输出图形名称、周长、面积。
  • 多态机制:shape->name() 实际调用派生类 Trianglename()
int main() {
   
    Triangle *tri = new Triangle(Point(0,2), Point(2,0), Point(0,0));
    show(tri);
    return 0;
}
  • 创建一个三角形对象并调用 show,演示多态输出。

题目三:链式栈操作

题目描述
实现基于链表的栈类 Stack,支持 pushpop

  • 数据项类 Entry 包含数据和指向下一结点的指针。
  • 主函数中将数组元素依次压栈,再依次弹栈输出。

题库出处cpp.code2ji.cn

1.00


初始代码(待填空)

#include <iostream>
using namespace std;

class Entry {
   
public:
    Entry* next;
    int data;

    //**********found**********
    Entry(Entry* n, int d) : next(n), data(d) { }
};

class Stack {
    Entry* top;
public:
    Stack() : top(0) { }
    ~Stack() {
        while (top != 0) {
            Entry* tmp = top;
            //**********found**********
            top = top->next;
            delete tmp;
        }
    }
    void push(int data) {
        //**********found**********
        top = new Entry(top, data);
    }
    int pop() {
        if (top == 0) return 0;
        //**********found**********
        int result = top->data;
        top = top->next;
        return result;
    }
};

int main() {
    int a[] = {0,1,2,3,4,5,6,7,8,9};
    Stack s;

    for (int i = 0; i < 10; i++) {
        cout << a[i] << ' ';
        s.push(a[i]);
    }
    cout << endl;

    for (int i = 0; i < 10; i++) {
        cout << s.pop() << ' ';
    }
    cout << endl;
    return 0;
}

代码逐句讲解

Entry(Entry* n, int d) : next(n), data(d) {
    }
  • 构造函数初始化,next 指向传入的结点,data 存储传入数据。
~Stack() {
   
    while (top != 0) {
   
        Entry* tmp = top;
        top = top->next;
        delete tmp;
    }
}
  • 析构函数循环释放所有结点,避免内存泄漏。
void push(int data) {
   
    top = new Entry(top, data);
}
  • 新结点成为栈顶,next 指向旧栈顶。
int pop() {
   
    if (top == 0) return 0;
    int result = top->data;
    top = top->next;
    return result;
}
  • 弹出栈顶数据,更新栈顶指针。

题目四:灯光开关(多态和控制)

题目描述
基类 Switchable 表示可开关的设备,派生类 Lamp 表示灯,Button 控制设备开关。

  • 实现 Switchable::isOn() 返回设备状态
  • Switchable::getDeviceName() 为纯虚函数
  • Button 构造函数正确初始化设备指针
  • Button::push() 切换设备状态

题库出处cpp.code2ji.cn
在这里插入图片描述


初始代码(待补全)

#include <iostream>
using namespace std;

class Switchable {
   
    bool is_on;
public:
    Switchable() : is_on(false) {
    }
    void switchOn() {
    is_on = true; }
    void switchOff() {
    is_on = false; }
    //**********found**********
    bool isOn() { return is_on; }
    //**********found**********
    virtual const char *getDeviceName() = 0;
};

class Lamp : public Switchable {
public:
    const char *getDeviceName() { return "Lamp"; }
};

class Button {
    Switchable *device;
public:
    //**********found**********
    Button(Switchable &dev) : device(&dev) { }
    bool isOn() { return device->isOn(); }
    void push() {
        //**********found**********
        if (isOn())
            device->switchOff();
        else
            device->switchOn();
    }
};

int main() {
    Lamp lamp;
    Button button(lamp);
    cout << "灯的状态:" << (lamp.isOn() ? "开" : "关") << endl;
    cout << "按钮的状态:" << (button.isOn() ? "开" : "关") << endl;

    button.push();

    cout << "灯的状态:" << (lamp.isOn() ? "开" : "关") << endl;
    cout << "按钮的状态:" << (button.isOn() ? "开" : "关") << endl;

    return 0;
}

代码逐句讲解

bool isOn() {
    return is_on; }
  • 返回当前设备状态,true 为开,false 为关。
virtual const char *getDeviceName() = 0;
  • 纯虚函数,派生类必须重写。
Button(Switchable &dev) : device(&dev) {
    }
  • 构造函数用设备引用初始化设备指针。
void push() {
   
    if (isOn())
        device->switchOff();
    else
        device->switchOn();
}
  • 按钮按下时,切换设备状态。

相关文章
|
8天前
|
人工智能 安全 Linux
【OpenClaw保姆级图文教程】阿里云/本地部署集成模型Ollama/Qwen3.5/百炼 API 步骤流程及避坑指南
2026年,AI代理工具的部署逻辑已从“单一云端依赖”转向“云端+本地双轨模式”。OpenClaw(曾用名Clawdbot)作为开源AI代理框架,既支持对接阿里云百炼等云端免费API,也能通过Ollama部署本地大模型,完美解决两类核心需求:一是担心云端API泄露核心数据的隐私安全诉求;二是频繁调用导致token消耗过高的成本控制需求。
5162 9
|
16天前
|
人工智能 JavaScript Ubuntu
5分钟上手龙虾AI!OpenClaw部署(阿里云+本地)+ 免费多模型配置保姆级教程(MiniMax、Claude、阿里云百炼)
OpenClaw(昵称“龙虾AI”)作为2026年热门的开源个人AI助手,由PSPDFKit创始人Peter Steinberger开发,核心优势在于“真正执行任务”——不仅能聊天互动,还能自动处理邮件、管理日程、订机票、写代码等,且所有数据本地处理,隐私完全可控。它支持接入MiniMax、Claude、GPT等多类大模型,兼容微信、Telegram、飞书等主流聊天工具,搭配100+可扩展技能,成为兼顾实用性与隐私性的AI工具首选。
21101 114
|
7天前
|
JavaScript Linux API
保姆级教程,通过GACCode在国内使用Claudecode、Codex!
保姆级教程,通过GACCode在国内使用Claudecode、Codex!
4644 1
保姆级教程,通过GACCode在国内使用Claudecode、Codex!
|
12天前
|
人工智能 安全 前端开发
Team 版 OpenClaw:HiClaw 开源,5 分钟完成本地安装
HiClaw 基于 OpenClaw、Higress AI Gateway、Element IM 客户端+Tuwunel IM 服务器(均基于 Matrix 实时通信协议)、MinIO 共享文件系统打造。
8061 7
|
14天前
|
人工智能 JavaScript API
保姆级教程:OpenClaw阿里云/本地部署配置Tavily Search skill 实时联网,让OpenClaw“睁眼看世界”
默认状态下的OpenClaw如同“闭门造车”的隐士,仅能依赖模型训练数据回答问题,无法获取实时新闻、最新数据或训练截止日期后的新信息。2026年,激活其联网能力的最优方案是配置Tavily Search技能——无需科学上网、无需信用卡验证,每月1000次免费搜索额度完全满足个人需求,搭配ClawHub技能市场,还能一键拓展天气查询、邮件管理等实用功能。
8075 5

热门文章

最新文章