C++ 基础语法完全入门指南 - 针对零基础小白
第一部分:C++是什么?为什么学习它?
1.1 C++简介
C++就像是一种与计算机对话的语言,它:
- 高效快速:直接操作计算机硬件,运行速度快
- 功能强大:可以用来开发游戏、操作系统、科学计算等
- 学习曲线:开始有点难,但掌握后非常强大
1.2 第一个C++程序 - “Hello World”
#include<iostream>// 包含输入输出库intmain(){// 主函数,程序从这里开始执行 std::cout <<"Hello, World!"<< std::endl;// 输出文字到屏幕return0;// 程序正常结束}逐行解释:
#include <iostream>:告诉编译器"我要用输入输出功能"int main():程序的主入口,就像房子的正门std::cout << "Hello, World!":在屏幕上显示文字return 0;:告诉操作系统"程序运行成功"
第二部分:C++基础语法概念
2.1 变量 - 数据的容器
#include<iostream>usingnamespace std;// 这样就不用每次都写 std::intmain(){// 定义变量 - 类型 变量名 = 值;int age =25;// 整数类型double height =1.75;// 小数类型char grade ='A';// 单个字符 string name ="张三";// 字符串bool isStudent =true;// 布尔值 (true/false)// 使用变量 cout <<"姓名: "<< name << endl; cout <<"年龄: "<< age << endl; cout <<"身高: "<< height <<"米"<< endl; cout <<"成绩: "<< grade << endl; cout <<"是学生吗? "<< isStudent << endl;return0;}2.2 基本数据类型
| 类型 | 含义 | 例子 |
|---|---|---|
int | 整数 | 10, -5, 1000 |
double | 小数 | 3.14, -2.5, 0.0 |
char | 字符 | 'A', '1', '#' |
string | 字符串 | "hello", "中文" |
bool | 布尔值 | true, false |
第三部分:运算符和表达式
3.1 数学运算
#include<iostream>usingnamespace std;intmain(){int a =10;int b =3; cout <<"a = "<< a <<", b = "<< b << endl; cout <<"加法: "<< a + b << endl;// 13 cout <<"减法: "<< a - b << endl;// 7 cout <<"乘法: "<< a * b << endl;// 30 cout <<"除法: "<< a / b << endl;// 3 (整数除法) cout <<"取余: "<< a % b << endl;// 1// 小数运算double x =10.0;double y =3.0; cout <<"小数除法: "<< x / y << endl;// 3.33333return0;}3.2 比较和逻辑运算
#include<iostream>usingnamespace std;intmain(){int a =10, b =20;// 比较运算 cout <<"a == b: "<<(a == b)<< endl;// 相等,输出 0 (false) cout <<"a != b: "<<(a != b)<< endl;// 不等,输出 1 (true) cout <<"a < b: "<<(a < b)<< endl;// 小于,输出 1 (true) cout <<"a > b: "<<(a > b)<< endl;// 大于,输出 0 (false)// 逻辑运算bool condition1 =true;bool condition2 =false; cout <<"条件1 AND 条件2: "<<(condition1 && condition2)<< endl;// 与运算 cout <<"条件1 OR 条件2: "<<(condition1 || condition2)<< endl;// 或运算 cout <<"NOT 条件1: "<<(!condition1)<< endl;// 非运算return0;}第四部分:控制流程
4.1 if-else 条件判断
#include<iostream>usingnamespace std;intmain(){int score; cout <<"请输入你的分数: "; cin >> score;// 从键盘输入if(score >=90){ cout <<"优秀!"<< endl;}elseif(score >=80){ cout <<"良好"<< endl;}elseif(score >=60){ cout <<"及格"<< endl;}else{ cout <<"不及格,要加油了!"<< endl;}return0;}4.2 for 循环
#include<iostream>usingnamespace std;intmain(){// 打印 1 到 5 cout <<"for循环示例:"<< endl;for(int i =1; i <=5; i++){ cout <<"当前数字: "<< i << endl;}// 计算 1 到 100 的和int sum =0;for(int i =1; i <=100; i++){ sum += i;// 等价于 sum = sum + i;} cout <<"1到100的和是: "<< sum << endl;return0;}4.3 while 循环
#include<iostream>usingnamespace std;intmain(){// while循环示例int count =1; cout <<"while循环示例:"<< endl;while(count <=5){ cout <<"计数: "<< count << endl; count++;// 重要:不要忘记这个,否则会无限循环!}// 用户输入控制循环int number; cout <<"请输入数字(输入0退出): "; cin >> number;while(number !=0){ cout <<"你输入了: "<< number << endl; cout <<"请输入数字(输入0退出): "; cin >> number;} cout <<"程序结束"<< endl;return0;}第五部分:函数 - 代码的模块化
5.1 定义和使用函数
#include<iostream>usingnamespace std;// 函数定义:返回类型 函数名(参数)intadd(int a,int b){return a + b;}// 没有返回值的函数voidprintWelcome(string name){ cout <<"欢迎, "<< name <<"!"<< endl;// void函数不需要return语句}// 判断是否为偶数的函数boolisEven(int number){return(number %2==0);}intmain(){// 使用函数int result =add(5,3); cout <<"5 + 3 = "<< result << endl;printWelcome("小明");int num =10;if(isEven(num)){ cout << num <<" 是偶数"<< endl;}else{ cout << num <<" 是奇数"<< endl;}return0;}第六部分:数组和容器
6.1 数组基础
#include<iostream>usingnamespace std;intmain(){// 定义数组:类型 数组名[大小]int scores[5]={85,92,78,90,88};// 访问数组元素(从0开始!) cout <<"第一个分数: "<< scores[0]<< endl; cout <<"第二个分数: "<< scores[1]<< endl;// 遍历数组 cout <<"所有分数: ";for(int i =0; i <5; i++){ cout << scores[i]<<" ";} cout << endl;// 修改数组元素 scores[2]=95;// 修改第三个元素// 计算平均分int total =0;for(int i =0; i <5; i++){ total += scores[i];}double average = total /5.0; cout <<"平均分: "<< average << endl;return0;}第七部分:结合OpenCV的C++实践
7.1 用我们学的C++知识理解OpenCV代码
#include<opencv2/opencv.hpp>usingnamespace cv;usingnamespace std;// 这个函数演示了如何用我们学过的C++概念voidexplainOpencvCode(){// 1. 变量定义 - Mat是OpenCV的图像类型 Mat image;// 定义一个图像变量// 2. 函数调用 - imread是读取图像的函数 image =imread("test.jpg");// 调用函数,传递参数"test.jpg"// 3. 条件判断 - 检查图像是否加载成功if(image.empty()){// empty()返回bool值 cout <<"无法加载图像!"<< endl;return;// 提前结束函数}// 4. 循环 - 处理图像的每个像素for(int row =0; row < image.rows; row++){// 遍历行for(int col =0; col < image.cols; col++){// 遍历列// 获取像素的BGR值 Vec3b pixel = image.at<Vec3b>(row, col);// 修改像素 - 增加红色分量 pixel[2]=min(255, pixel[2]+50);// [2]是红色通道}}// 5. 函数调用 - 显示图像imshow("处理后的图像", image);waitKey(0);// 等待按键}// 简单的运动检测函数(用我们学过的所有概念)voidsimpleMotionDetection(){ VideoCapture cap(0);// 创建视频捕获对象// 条件判断if(!cap.isOpened()){ cout <<"无法打开摄像头!"<< endl;return;} Mat previousFrame, currentFrame; cap >> previousFrame;// 读取第一帧// 循环 - 处理视频的每一帧while(true){ cap >> currentFrame;// 读取当前帧// 条件判断if(currentFrame.empty())break;// 转换为灰度图(简化处理) Mat grayPrevious, grayCurrent;cvtColor(previousFrame, grayPrevious, COLOR_BGR2GRAY);cvtColor(currentFrame, grayCurrent, COLOR_BGR2GRAY);// 计算帧间差异 Mat diff;absdiff(grayPrevious, grayCurrent, diff);// 显示结果imshow("原始视频", currentFrame);imshow("运动检测", diff);// 更新前一帧 previousFrame = currentFrame.clone();// 按键检查if(waitKey(30)==27)break;// ESC键退出}}intmain(){// 调用我们的函数simpleMotionDetection();return0;}第八部分:常见错误和调试技巧
8.1 新手常见错误
#include<iostream>usingnamespace std;voidcommonMistakes(){// 错误1: 忘记分号// int x = 10 // 错误!缺少分号int x =10;// 正确// 错误2: 使用未初始化的变量int y;// cout << y; // 错误!y没有初始值 y =20;// 正确:先赋值再使用// 错误3: 数组越界int arr[3]={1,2,3};// cout << arr[5]; // 错误!访问不存在的元素// 错误4: 无限循环// while (true) { // cout << "无限循环!"; // } // 错误!没有退出条件// 正确做法:int count =0;while(count <5){ cout << count <<" "; count++;}}学习计划建议
第一周:基础语法
- 每天学习2个概念
- 完成所有示例代码
- 重点:变量、运算符、输入输出
第二周:控制流程
- 练习if-else判断
- 掌握for和while循环
- 做小练习:计算器、猜数字游戏
第三周:函数和数组
- 学习函数定义和调用
- 理解数组的使用
- 项目:学生成绩管理系统
第四周:OpenCV结合
- 用C++基础理解OpenCV代码
- 运行简单的计算机视觉程序
- 项目:简单的运动检测
记住这些要点:
- 每个语句以分号结束
- 变量使用前要先声明
- 数组下标从0开始
- 循环要有结束条件
- 多写注释,方便理解
// 好的注释习惯#include<iostream>usingnamespace std;/* * 这是一个多行注释 * 可以描述函数的功能 */intmain(){// 这是单行注释int number =10;// 定义并初始化变量 cout <<"学习C++就像学走路,"<<"开始可能会摔倒,"<<"但坚持下去就能奔跑!"<< endl;return0;}