C++入门到实战教程 全章节配套练习题+答案解析
本手册对应教程13个章节,每章分为基础巩固题(必做)和进阶拔高题(选做),每题附带【解题原理】【完整代码】【结果说明】,零基础可直接对照练习、纠错、复盘,全覆盖考点、易错点、核心重难点。
第1章 为什么学习C++ 配套习题
一、基础巩固(概念背诵)
1、简述C++编译型、静态类型、多范式三大核心特性的含义。
2、列举至少4个C++主流应用领域。
二、参考答案+解析
第1题解析:
1. 编译型:整体代码编译为机器码后运行,执行效率高、无解释开销;
2. 静态类型:定义变量必须指定类型,编译阶段完成类型检查,程序更稳定;
3. 多范式:同时支持面向过程、面向对象、泛型编程,适配各类开发场景。
第2题答案:游戏开发、嵌入式开发、自动驾驶、操作系统底层、高并发后端、算法竞赛、区块链底层。
第2章 开发环境准备 配套习题
一、基础巩固
1、手写第一个C++输出程序,输出“环境搭建完成,开始C++学习!”。
2、简述C++代码从cpp文件到运行的完整编译流程。
二、参考答案
#include<iostream>usingnamespacestd;intmain(){cout<<"环境搭建完成,开始C++学习!"<<endl;return0;}编译流程答案:源代码.cpp → 预处理 → 编译汇编 → 链接 → 可执行文件.exe → 运行输出结果。
第3章 变量、数据类型与输入输出 配套习题
一、基础巩固
1、定义int、double、char、bool、string五种变量并打印输出。
2、通过cin接收用户输入的姓名和年龄,格式化输出个人信息。
3、定义圆周率常量PI,计算并输出半径为5的圆的面积。
二、参考答案
#include<iostream>#include<string>usingnamespacestd;intmain(){// 1. 基础数据类型输出intnum=10;doublescore=95.5;charch='B';boolflag=false;string str="C++练习";cout<<num<<" "<<score<<" "<<ch<<" "<<flag<<" "<<str<<endl;// 2. 交互式输入个人信息string name;intage;cout<<"请输入姓名:";cin>>name;cout<<"请输入年龄:";cin>>age;cout<<"姓名:"<<name<<",年龄:"<<age<<endl;// 3. 常量计算圆面积constdoublePI=3.14159;intr=5;doublearea=PI*r*r;cout<<"半径为5的圆面积:"<<area<<endl;return0;}第4章 控制流:条件与循环 配套习题
一、基础巩固
1、输入分数,判断并输出等级:90及以上优秀、80-89良好、60-79及格、60以下不及格。
2、使用for循环输出1~100所有整数。
3、使用while循环计算1~10累加和。
二、进阶拔高
4、输出1~50,遇到3的倍数跳过,遇到40直接结束循环。
三、参考答案
#include<iostream>usingnamespacestd;intmain(){// 1. 成绩等级判断intscore;cout<<"请输入成绩:";cin>>score;if(score>=90)cout<<"优秀"<<endl;elseif(score>=80)cout<<"良好"<<endl;elseif(score>=60)cout<<"及格"<<endl;elsecout<<"不及格"<<endl;// 2. for输出1-100for(inti=1;i<=100;i++)cout<<i<<" ";cout<<endl;// 3. while累加1-10intsum=0,i=1;while(i<=10){sum+=i;i++;}cout<<"1~10累加和:"<<sum<<endl;// 4. break+continue综合练习for(intj=1;j<=50;j++){if(j==40)break;if(j%3==0)continue;cout<<j<<" ";}return0;}第5章 函数与作用域 配套习题
一、基础巩固
1、封装函数实现两数乘法,主函数调用输出结果。
2、使用函数声明+分离定义的方式,实现减法函数。
3、区分全局变量与局部变量,写出同名变量优先级效果。
二、参考答案
#include<iostream>usingnamespacestd;// 函数声明intsub(inta,intb);// 乘法函数intmul(inta,intb){returna*b;}// 全局变量intnum=666;intmain(){// 调用乘法函数cout<<"10*8="<<mul(10,8)<<endl;// 调用减法函数cout<<"20-7="<<sub(20,7)<<endl;// 作用域测试intnum=999;cout<<"局部变量num:"<<num<<endl;return0;}// 函数定义intsub(inta,intb){returna-b;}第6章 指针与引用 配套习题
一、基础巩固
1、通过指针修改变量数值,打印地址和数值变化。
2、通过引用交换两个整数的值。
二、参考答案
#include<iostream>usingnamespacestd;// 引用交换数值voidswapNum(int&a,int&b){inttemp=a;a=b;b=temp;}intmain(){// 1. 指针操作intx=100;int*p=&x;cout<<"原数值:"<<x<<endl;*p=200;cout<<"指针修改后:"<<x<<endl;cout<<"变量地址:"<<&x<<" 指针存储地址:"<<p<<endl;// 2. 引用交换intm=10,n=20;swapNum(m,n);cout<<"交换后 m="<<m<<" n="<<n<<endl;return0;}第7章 数组、字符串与容器 配套习题
一、基础巩固
1、定义数组存储5个成绩,遍历求和、求平均值。
2、实现字符串拼接、获取长度。
3、使用vector动态添加5个数字并遍历输出。
二、参考答案
#include<iostream>#include<string>#include<vector>usingnamespacestd;intmain(){// 1. 数组求和平均值intscore[5]={80,90,77,88,95};intsum=0;for(inti=0;i<5;i++)sum+=score[i];cout<<"总分:"<<sum<<" 平均分:"<<sum/5.0<<endl;// 2. 字符串操作string s1="Hello";string s2=" C++";cout<<"拼接结果:"<<s1+s2<<" 长度:"<<(s1+s2).size()<<endl;// 3. vector容器vector<int>v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);for(autoval:v)cout<<val<<" ";return0;}第8章 类与对象 配套习题
一、基础巩固
1、定义一个学生类,包含私有姓名、成绩,通过构造函数初始化,公开方法展示信息。
2、实现简单继承:父类动物,子类狗,复用父类属性。
二、参考答案
#include<iostream>#include<string>usingnamespacestd;// 1. 学生类封装classStudent{private:string name;intscore;public:Student(string n,ints){name=n;score=s;}voidshow(){cout<<"学生:"<<name<<" 成绩:"<<score<<endl;}};// 2. 继承练习classAnimal{public:string type;voidinfo(){cout<<"这是动物类"<<endl;}};classDog:publicAnimal{public:voidbark(){cout<<"汪汪汪"<<endl;}};intmain(){Students("小明",92);s.show();Dog d;d.type="犬类";d.info();d.bark();return0;}第9章 STL标准模板库 配套习题
一、基础巩固
1、使用vector存储乱序数组,利用sort排序并输出。
2、遍历vector并输出所有元素。
二、参考答案
#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;intmain(){vector<int>v={9,3,7,1,6,2};sort(v.begin(),v.end());cout<<"排序后:";for(autox:v){cout<<x<<" ";}return0;}第10章 文件操作与异常处理 配套习题
一、基础巩固
1、将自定义文本写入test.txt,再读取打印。
2、捕获除数为0的异常,避免程序崩溃。
二、参考答案
#include<iostream>#include<fstream>#include<string>usingnamespacestd;intmain(){// 1. 文件读写ofstreamfout("test.txt");fout<<"C++文件操作练习\n";fout.close();ifstreamfin("test.txt");string line;while(getline(fin,line)){cout<<line<<endl;}fin.close();// 2. 异常处理try{inta=20,b=0;if(b==0)throw"除数不能为0!";cout<<a/b<<endl;}catch(constchar*err){cout<<"异常:"<<err<<endl;}return0;}第11章 学生成绩管理系统 拓展习题
一、必做拓展功能
基于原项目源码,新增两个核心功能:
1、成绩从高到低排序展示
2、根据学号修改对应学生成绩
二、拓展后完整源码
#include<iostream>#include<vector>#include<string>#include<fstream>#include<algorithm>usingnamespacestd;structStudent{string name;intid;intscore;};vector<Student>stuList;// 排序比较函数boolcmp(Student s1,Student s2){returns1.score>s2.score;}voidaddStudent(){Student s;cout<<"请输入学生姓名:";cin>>s.name;cout<<"请输入学生学号:";cin>>s.id;cout<<"请输入学生成绩:";cin>>s.score;stuList.push_back(s);cout<<"✅ 添加成功!\n";}voidshowAll(){if(stuList.empty()){cout<<"暂无数据!\n";return;}cout<<"学号\t姓名\t成绩\n";for(auto&s:stuList){cout<<s.id<<"\t"<<s.name<<"\t"<<s.score<<endl;}}// 新增:成绩排序voidsortScore(){sort(stuList.begin(),stuList.end(),cmp);cout<<"✅ 成绩排序完成!\n";showAll();}// 新增:修改成绩voidupdateScore(){intid,newScore;cout<<"请输入要修改的学号:";cin>>id;for(auto&s:stuList){if(s.id==id){cout<<"请输入新成绩:";cin>>newScore;s.score=newScore;cout<<"✅ 成绩修改成功!\n";return;}}cout<<"未找到该学生!\n";}voidsaveData(){ofstreamfout("student_data.txt");for(auto&s:stuList){fout<<s.id<<" "<<s.name<<" "<<s.score<<endl;}fout.close();cout<<"✅ 数据已保存!\n";}voidmenu(){cout<<"\n===== 升级版学生管理系统 =====\n";cout<<"1. 添加学生\n2. 查看所有学生\n3. 成绩排序\n4. 修改成绩\n5. 保存退出\n";cout<<"请选择功能:";}intmain(){intop;while(true){menu();cin>>op;if(op==1)addStudent();elseif(op==2)showAll();elseif(op==3)sortScore();elseif(op==4)updateScore();elseif(op==5){saveData();break;}elsecout<<"输入错误!\n";}return0;}第12、13章 综合复盘习题
综合实操题
独立开发简易控制台计算器:
1、支持加减乘除运算
2、捕获除数为0异常
3、循环输入,可选择退出
4、封装计算函数,模块化编写
综合题参考答案
#include<iostream>usingnamespacestd;// 计算函数doublecalc(doublea,doubleb,charop){if(op=='+')returna+b;if(op=='-')returna-b;if(op=='*')returna*b;if(op=='/'){if(b==0)throw0;returna/b;}throw1;}intmain(){doublex,y,res;charop;while(true){cout<<"\n请输入算式(例:10+5),输入0退出:";cin>>x;if(x==0)break;cin>>op>>y;try{res=calc(x,y,op);cout<<"结果:"<<res<<endl;}catch(interr){if(err==0)cout<<"错误:除数不能为0!\n";elsecout<<"错误:运算符非法!\n";}}cout<<"程序退出!\n";return0;}