1. 预备知识
基本概念:变量,数据类型,函数
流程图
UML统一建模语言
算法:解决一个问题的一系列步骤,如菜谱
2. C++背景
创始人Bjarne Stroustrup
内存管理:static, automatic, dynamic, garbage
3. 主函数基本部分剖析
注释/**/
变量
赋值=
输入输出
判断
循环
类,对象
文件读取写入
4. 数据类型
1byte char(字符), bool(布尔)
2bytes short(短型)
4bytes int(整数), long(长型), float(浮点数)
8bytes double(双浮点数)
5. 变换
隐式,int i = 5 + 11.12; // i = 17
显式,float f = 12.11; i = static_cast<int>(f);
6. Debug调试
设置断点
7. 表达式
数学 + - * / %
布尔 true false
判断 < > <= >= == != && ||
8. 循环
do{} while(true);
while(true){}
for(初始化; 判断; 递增){}
9. 随机数
#include<stdlib.h>
#include<time.h>
// a, b为界
(rand()%(b-a+1)) + a
// 更随机
srand(time(0));
rand();
10. 用户定义函数
ReturnType functionName( ParameterList )
{
// Code
return value;
}
拷贝值传递 ReturnType function( Type param )
引用值传递 ReturnType function( Type& param )
11. 预定义函数
<string> string getline(); int length(); string substr(int pos); string substr(int start, int length)
<cmath> sqrt(); pow(); sin(); cos(); fabs()
<iomanip> fixed; left/right; setfill(char fillchar); setprecision(int num); setw(int width)
<cstalib> int rand(); void srand(int num); int abs(int num); void exist(int num)
<cctype> bool isalpha(char c); bool isdigit(char c)
12. 默认参数
ReturnType functionName(Type param = defaultValue);
13. 函数重载 // 参数不同
ReturnType functionName(Type1 param1);
ReturnType functionName(Type2 param2);
14. 类
对象的蓝图(为什么需要类?重用,封装数据)
包括实例数据,构造器/析构器,函数(方法)(.h文件声明,.cpp文件定义)
实例化ClassName objectname(parameters);
封装Encapsulation
private(自己可访问)
protected(公共可访问)
public(继承可访问)
继承Inheritance
关系Is-a
多态Polymorphism
函数行为
15. 数组
Type arrayName[sizeNum];
固定大小
篱笆柱边界错误
16. 指针
Type* pointerName = &variable;
解引用 *pointerName
17. 向量vector
#include<vector>
vector<Type> vectorName;
vector<Type> vectorName(sizeNum);
vector<Type> vectorName(sizeNum, defaultValue);
18. 抽象数据类型(Abstract Data Type, ADT)
由程序员自己创造
包含不同的基本数据类型
用.(dot)操作符访问
比较时要每个数据单独比较
创建时可初始化
Struct StructName {
int foo;
float bar;
}
19. 排序
选择,插入,冒泡,快速,归并
20. 文件操作
#include<fstream>
// 读取
ifstream inputFile;
inputFile.open("fileName.txt", ios::in);
inputFile.eof();
inputFile.getline(storeVariable, sizeNum)
// 写入
ofstream outputFile;
outputFile.open("fineName.txt", ios::out);
outputFile << stringVariable << endl;
21. 异常处理
try {
// buggy code
throw errorCode; // 用户自定义
} catch(int error) {
// check here and handle it
}
基本概念:变量,数据类型,函数
流程图
UML统一建模语言
算法:解决一个问题的一系列步骤,如菜谱
2. C++背景
创始人Bjarne Stroustrup
内存管理:static, automatic, dynamic, garbage
3. 主函数基本部分剖析
注释/**/
变量
赋值=
输入输出
判断
循环
类,对象
文件读取写入
4. 数据类型
1byte char(字符), bool(布尔)
2bytes short(短型)
4bytes int(整数), long(长型), float(浮点数)
8bytes double(双浮点数)
5. 变换
隐式,int i = 5 + 11.12; // i = 17
显式,float f = 12.11; i = static_cast<int>(f);
6. Debug调试
设置断点
7. 表达式
数学 + - * / %
布尔 true false
判断 < > <= >= == != && ||
8. 循环
do{} while(true);
while(true){}
for(初始化; 判断; 递增){}
9. 随机数
#include<stdlib.h>
#include<time.h>
// a, b为界
(rand()%(b-a+1)) + a
// 更随机
srand(time(0));
rand();
10. 用户定义函数
ReturnType functionName( ParameterList )
{
// Code
return value;
}
拷贝值传递 ReturnType function( Type param )
引用值传递 ReturnType function( Type& param )
11. 预定义函数
<string> string getline(); int length(); string substr(int pos); string substr(int start, int length)
<cmath> sqrt(); pow(); sin(); cos(); fabs()
<iomanip> fixed; left/right; setfill(char fillchar); setprecision(int num); setw(int width)
<cstalib> int rand(); void srand(int num); int abs(int num); void exist(int num)
<cctype> bool isalpha(char c); bool isdigit(char c)
12. 默认参数
ReturnType functionName(Type param = defaultValue);
13. 函数重载 // 参数不同
ReturnType functionName(Type1 param1);
ReturnType functionName(Type2 param2);
14. 类
对象的蓝图(为什么需要类?重用,封装数据)
包括实例数据,构造器/析构器,函数(方法)(.h文件声明,.cpp文件定义)
实例化ClassName objectname(parameters);
封装Encapsulation
private(自己可访问)
protected(公共可访问)
public(继承可访问)
继承Inheritance
关系Is-a
多态Polymorphism
函数行为
15. 数组
Type arrayName[sizeNum];
固定大小
篱笆柱边界错误
16. 指针
Type* pointerName = &variable;
解引用 *pointerName
17. 向量vector
#include<vector>
vector<Type> vectorName;
vector<Type> vectorName(sizeNum);
vector<Type> vectorName(sizeNum, defaultValue);
18. 抽象数据类型(Abstract Data Type, ADT)
由程序员自己创造
包含不同的基本数据类型
用.(dot)操作符访问
比较时要每个数据单独比较
创建时可初始化
Struct StructName {
int foo;
float bar;
}
19. 排序
选择,插入,冒泡,快速,归并
20. 文件操作
#include<fstream>
// 读取
ifstream inputFile;
inputFile.open("fileName.txt", ios::in);
inputFile.eof();
inputFile.getline(storeVariable, sizeNum)
// 写入
ofstream outputFile;
outputFile.open("fineName.txt", ios::out);
outputFile << stringVariable << endl;
21. 异常处理
try {
// buggy code
throw errorCode; // 用户自定义
} catch(int error) {
// check here and handle it
}
本文介绍了C++编程的基础知识,包括变量、数据类型、流程控制、函数定义与使用、类的概念及其实现、数组与指针的区别等。同时,还探讨了调试技巧、文件操作以及异常处理等内容。

被折叠的 条评论
为什么被折叠?



