- 一、基础入门
- 1.C++初识
- 1.1第一个C++程序
- 1.2 注释
- 1.3 变量
- 1.4 常量
- 1.5 关键字
- 1.6 标识符命名规则
- 2.数据类型
- 2.1 整型
- 2.2 sizeof关键字
- 2.3 浮点型
- 2.4 字符型
- 2.5 转义字符
- 2.6 字符串型
- 2.7 布尔型
- 2.8 数据的输入
- 3. 运算符
- 4.程序流程结构
- 4.1顺序结构
- 4.2选择结构
- 4.2.1if条件语句
- 4.2.2 三目运算符
- 4.2.3 switch
- 4.3 循环结构
- 4.3.1 while循环
- 4.3.2 for循环
- 4.4 跳转语句
- 5.数组
- 5.2 一维数组
- 5.2.1 一维数组的定义方式
- 5.2.2 一维数组名的作用
- 5.3 二维数组
- 6.函数
- 6.1概述
- 6.2 函数的定义
- 6.3 函数的分文件编写
#include1.2 注释using namespace std; int main() { cout << "hello worlld" << endl; system("pause"); return 0; }
// 单行注释
变量存在的意义:方便管理内存空间
变量创建的语法:数据类型 变量名 = 变量的初始值;
#include1.4 常量using namespace std; int main() { int a = 10; cout << "a = " << a << endl; system("pause"); return 0; }
C++两种定义常量的方式
1.#define 宏常量,通常在文件上方定义,表示一个常量;
#define 常量名 常量值
2.const 修饰的变量,通常在变量定义前加关键字const,修饰该变量为常量,不可修改
const 数据类型 常量名 = 常量值
#include1.5 关键字 1.6 标识符命名规则using namespace std; #define Day 7 int main() { cout << "a week = " << Day <<"days"<< endl; const int Month = 12; cout << "a year = " << Month <<"months"<< endl; system("pause"); return 0; }
非关键字、字母数字下划线、字母下划线开头、区分大小写
2.数据类型c++在创建变量或常量的时候必须要先指定出相应的数据类型,否则无法分配内存
2.1 整型short、int、long、long long
2.2 sizeof关键字作用:统计数据类型所占内存大小
语法:sizeof(数据类型、变量)
#includeusing namespace std; int main() { cout << "sizeof int = " << sizeof(int) << endl; system("pause"); return 0; }
return 4个字节
2.3 浮点型float(7位有效数字)、double(15-16位有效数字)
2.4 字符型char 用来显示单个字符 char ch = ‘a’ 单引号
note:字符型变量并不是吧字符本身放到内存中存储,而是将对应的ASCII编码放到存储单元中
n 换行
r 回车
b 退格
- char 变量名[] = “字符串值” 双引号
- string 变量名 = “字符串值” #include
作用:用于从键盘获取数据
关键字:cin
语法:cin >> 变量
#includeusing namespace std; int main() { int a = 0; cout << "input a :" << endl; cin >> a; cout << "input a :" << a << endl; system("pause"); return 0; }
#include3. 运算符 4.程序流程结构 4.1顺序结构 4.2选择结构 4.2.1if条件语句using namespace std; int main() { float f = 0; cout << "input float f :" << endl; cin >> f; cout << "float f :" << f << endl; system("pause"); return 0; }
if(condition)
{}
elif()
{}
else
{}
if后面没有分号
c= (a>b?a:b)
4.2.3 switchswitch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
default:执行语句;break;
}
while(循环条件){执行体}
4.3.2 for循环for(int i=0; i<10; i++){执行体}
4.4 跳转语句break、continue、goto
goto:无条件跳转
1.数据类型 数组名[数组长度];
2.数据类型 数组名[数组长度] ={值1,值2…}
3.数据类型 数组名[] ={值1,值2…}
统计数组的长度:sizeof(arr) 代码封装 五个部分: 函数的分文件编写的四个步骤
获取数组在内存中的首地址:cout << arr <
返回值类型、函数名、参数列表、函数体语句、return表达式
1.创建后缀名为.h的头文件
2.创建后缀名为.cpp的源文件
3.在头文件中写函数的声明
4.在源文件中写函数的定义



