初始C++
注释
变量
常量
关键字
标识符命名规则
数据类型
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
整型
sizeof关键字
浮点型(实型)
有效位数保留七位,带小数点。
这个是保留有效数字位数,不包括小数点。
字符型
转义字符
字符串型
布尔类型 bool
数据的输入
运算符
算术运算符
赋值运算符
比较运算符
逻辑运算符
程序流程结构
选择结构
if语句
int main() {int score = 0;cout << "请输入考试分数:" << endl;cin >> score;if (score > 600){cout << "我考上了一本大学" << endl;if (score > 700){cout << "我考上了北大" << endl;}else if (score > 650){cout << "我考上了清华" << endl;}else{cout << "我考上了人大" << endl;}}else if (score > 500){cout << "我考上了二本大学" << endl;}else if (score > 400){cout << "我考上了三本大学" << endl;}else{cout << "我未考上本科" << endl;}system("pause");return 0;
}
三目运算符
switch语句
循环结构
while循环语句
do...while循环语句
for循环语句
嵌套循环
跳转语句
break语句
continue语句
goto语句
数组
一维数组
冒泡排序
二维数组
函数
函数的定义
函数的调用
值传递
函数的常见样式
函数的声明
函数的分文件编写
指针
指针的定义和使用
每次p的地址是不一样的。
指针所占内存空间
空指针和野指针
const 修饰指针
指针和数组
指针和函数
指针 数组 函数
//冒泡排序函数
void bubbleSort(int * arr, int len) //int * arr 也可以写为int arr[]
{for (int i = 0; i < len - 1; i++){for (int j = 0; j < len - 1 - i; j++){if (arr[j] > arr[j + 1]){int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}
}//打印数组函数
void printArray(int arr[], int len)
{for (int i = 0; i < len; i++){cout << arr[i] << endl;}
}int main() {int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };int len = sizeof(arr) / sizeof(int);bubbleSort(arr, len);printArray(arr, len);system("pause");return 0;
}
结构体
结构体的定义和使用
//结构体定义
struct student
{//成员列表string name; //姓名int age; //年龄int score; //分数
}stu3; //结构体变量创建方式3 int main() {//结构体变量创建方式1struct student stu1; //struct 关键字可以省略stu1.name = "张三";stu1.age = 18;stu1.score = 100;cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 分数:" << stu1.score << endl;//结构体变量创建方式2struct student stu2 = { "李四",19,60 };cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 分数:" << stu2.score << endl;stu3.name = "王五";stu3.age = 18;stu3.score = 80;cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 分数:" << stu3.score << endl;system("pause");return 0;
}
结构体数组
结构体指针
结构体嵌套结构体
//学生结构体定义
struct student
{//成员列表string name; //姓名int age; //年龄int score; //分数
};//教师结构体定义
struct teacher
{//成员列表int id; //职工编号string name; //教师姓名int age; //教师年龄struct student stu; //子结构体 学生
};int main() {struct teacher t1;t1.id = 10000;t1.name = "老王";t1.age = 40;t1.stu.name = "张三";t1.stu.age = 18;t1.stu.score = 100;cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;system("pause");return 0;
}
结构体做函数参数
//学生结构体定义
struct student
{//成员列表string name; //姓名int age; //年龄int score; //分数
};//值传递
void printStudent(student stu )
{stu.age = 28;cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
}//地址传递
void printStudent2(student *stu)
{stu->age = 28;cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;
}int main() {student stu = { "张三",18,100};//值传递printStudent(stu);cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;cout << endl;//地址传递printStudent2(&stu);cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;system("pause");return 0;
}