1.C++初识
1.1C++初识的基本结构
# include <iostream>
using namespace std;
int main ( )
{
cout << "Hello World" << endl;
system ( "pause" ) ;
return 0 ;
}
1.2注释
在代码中加一些说明和解释,方便自己或其他程序员阅读代码
1.2.1注释的两种格式
1.2.1.1单行注释
通常放在一行代码的上方,或者一条语句的末尾,对该行代码说明
1.2.1.2多行注释
1.2.1.3演示
# include <iostream>
using namespace std;
int main ( )
{
cout << "hello world" << endl;
system ( "pause" ) ;
return 0 ;
}
1.3变量
1.3.1语法
数据类型 变量名 = 初始值;
1.3.2演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 10 ;
cout << "num= " << num << endl;
system ( "pause" ) ;
return 0 ;
}
1.4常量
1.4.1定义常量的两种方式
1.4.1.1 #define宏常量
# define 常量名 常量值
1.4.1.2 const修饰的常量
通常在变量定义前面加关键字const,修饰该变量为常量,不可修改
const 数据类型 常量名 = 常量值
1.4.2常量演示
# include <iostream>
using namespace std;
# define Day 7
int main ( )
{
const int MONTH = 12 ;
cout << "一周总共有多少天" << Day << "天" << endl;
cout << "一年有多少月份" << MONTH << "个" << endl;
system ( "pause" ) ;
return 0 ;
}
1.5关键字
关键字 是C++中预留保留的单词在定义变量 或常量 时候,不要用关键字
1.5.1不常见的关键字
asm typedef auto inline typeid bool dynamic_cast signed typename mutable union catch explict static_cast virtual operator wchar_t friend register
1.5.2演示
# include <iostream>
using namespace std;
int main ( )
{
system ( "pause" ) ;
return 0 ;
}
1.6标识符命名规则
1.6.1规则
C++规定给标识符 (变量、常量)命名时,有一套自己的规则
标识符 不能是关键字 标识符 只能由字母 、数字 、下划线 组成第一个字符必须为字母 或下划线 标识符种字母区分大小写
1.6.2演示
# include <iostream>
using namespace std;
int main ( )
{
int _2df = 16 ;
int fde = 15 ;
system ( "pause" ) ;
return 0 ;
}
2.数据类型
C++规定在创建一个变量或常量时,必须要指定出相应的数据类型 ,否则无法给变量分配内存
2.1整型
整型变量表示的是整数类型的数据 C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同
数据类型 占用空间 取值范围 short(短整型) 2字节 -2^15 ~ 2^15 - 1 int(整型) 4字节 -2^31 ~ 2^31-1 long(长整型) windows为4字节,Linux为4字节(32位),8字节(64位) -2^31 ~ 2^31 - 1 long long(长长整型) 8字节 -2^63 ~ 2^63-1
2.1.2演示
# include <iostream>
using namespace std;
int main ( )
{
short num = 20 ;
int num1 = 20 ;
long num2 = 20 ;
long long num3 = 20 ;
system ( "pause" ) ;
return 0 ;
}
2.2sizeof关键字
利用sizeof 关键字可以统计数据类型所占内存大小
2.2.1语法
sizeof ( 数据类型 / 变量)
2.2.2演示
# include <iostream>
using namespace std;
int main ( )
{
short num1 = 10 ;
cout << "short占用的内存空间" << sizeof ( num1) << endl;
system ( "pause" ) ;
return 0 ;
}
2.3实型(浮点型)
2.3.1浮点型变量分为两种
单精度float 双精度double 区别: 表示的有效数字范围不同 默认情况下,会默认显示六位有效数字
数据类型 占用空间 有效数字范围 float 4字节 7位有效数字 double 8字节 15~16位有效数字
2.3.2演示
# include <iostream>
using namespace std;
int main ( )
{
float f1 = 3.14f ;
double d1 = 3.14 ;
double d2 = 3.2e3 ;
system ( "pause" ) ;
return 0 ;
}
2.4字符型
字符型变量用于显示单个字符 C和C++中字符型变量只占用1个字节 字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码 放到存储单元
2.4.1语法
在现实字符型变量时,用单引号括 起来,不要用双引号 单引号 只能有一个字符,不可以是字符串
char ch = 'a' ;
2.4.2演示
# include <iostream>
using namespace std;
int main ( )
{
char sh = 'a' ;
cout << "字符型变量所占的内存" << sizeof ( sh) << endl;
system ( "pause" ) ;
return 0 ;
}
2.5 转义字符
用于表示一些不能显示出来的ASCII字符 常见的转义字符表
转义字符 含义 ASCII(十进制) \a 警报 007 \b 退格(BS),将当前位置移到前一列 008 \f 换页(FF),将当前位置移到下页开头 012 \n 换行(LF),将当前位置移到下一行开头 010 \r 回车(CR),将当前位置移到本行开头 013 \t 水平制表(HT),(跳到下一个TAB位置) 009 \v 垂直制表(VT) 011 \\ 代表一个反斜线字符"\" 092 ’ 代表一个单引号 039 " 代表一个双引号 034 ? 代表一个问好 063
2.5.1演示
# include <iostream>
using namespace std;
int main ( )
{
cout << "Hello World \n" << endl;
cout << "\\" << endl;
cout << "Hello \t" << endl;
system ( "pause" ) ;
return 0 ;
}
2.6字符串型
2.6.1字符串的两种形式
2.6.1.1C风格字符串
char 变量名[ ] = 字符串量
2.6.1.2C++风格字符串
string 变量名 = 字符串量
注意点: 使用string时,需要写一个头文件 #include<string>
2.6.2演示
# include <iostream>
using namespace std;
# include <string>
int main ( )
{
char str[ ] = "法外狂徒张三" ;
cout << str << endl;
string strs = "法外狂徒张三" ;
cout << strs << endl;
system ( "pause" ) ;
return 0 ;
}
2.7布尔类型
布尔数据类型代表真 /假 的值 bool类型 只有两个值
true — 真 (本质是1) false — 假 (本质是0) bool类型 占1个字节大小
2.7.1演示
# include <iostream>
using namespace std;
int main ( )
{
bool flag = true ;
cout << flag << endl;
bool flag1 = false ;
cout << flag1 << endl;
cout << "布尔类型占用空间大小:" << sizeof ( flag) << endl;
system ( "pause" ) ;
return 0 ;
}
2.8数据输入
2.8.1语法
cin >> 变量
2.8.2演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 10 ;
cin >> num;
cout << "你输入的数字是:" << num << endl;
system ( "pause" ) ;
return 0 ;
}
3.运算符
运算符类型 作用 算术运算符 用于处理四则运算 赋值运算符 用于将表达式的值赋给变量 比较运算符 用于表达式的比较,并返回一个bool值 逻辑运算符 用于根据表达式的值返回bool值
3.1算术运算符
运算符 术语 示例 结果 + 正号 +3 3 - 负号 -3 -3 + 加 10+5 15 - 减 10-5 5 * 乘 10*5 50 / 除 10/5 2 % 取模 10 % 5 0 ++ 前置递增 a = 2;b = ++a a = 3;b = 3 ++ 后置递增 a = 2;b = a++ a = 3;b = 2 – 前置递减 a = 2; b = --a a = 1; b = 1 – 后置递减 a = 2,b = a– a = 1; b = 2
3.1.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 5 ;
int num1 = num++ ;
cout << "num = " << num << ";num1 = " << num1 << endl;
cout << "num / num1 = " << num / num1 << endl;
system ( "pause" ) ;
return 0 ;
}
3.2赋值运算符
用于将表达式的值赋给变量 赋值运算符包括以下几个符号
运算符 术语 示例 结果 = 赋值 a = 2 += 加等 a = 0;a += 2 a = 2 -= 减等 a = 5;a -= 2 a = 3 *= 乘等 a = 5; a *= 2 a = 10 /= 除等 a = 5; a /= 2 a = 2 %= 模等 a = 5; a %= 2 a = 1
3.2.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 5 ;
int num1 = 2 ;
cout << "(num *= num1) = " << ( num *= num1) << endl;
cout << "(num %= num1) = " << ( num %= num1) << endl;
system ( "pause" ) ;
return 0 ;
}
3.3比较运算符
用于比较表达式的比较,并返回一个bool值 比较运算符包含以下符号
运算符 术语 示例 结果 == 相等于 4 == 3 0 != 不等于 4 != 3 1 < 小于 4 < 3 0 > 大于 4 > 3 1 <= 小于等于 4 <= 3 0 >= 大于等于 4 >= 3 1
3.3.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num1 = 8 ;
int num2 = 10 ;
cout << ( num1 <= num2) << endl;
cout << ( num1 != num2) << endl;
cout << ( num1 > num2) << endl;
system ( "pause" ) ;
return 0 ;
}
3.4逻辑运算符
用于根据表达式的值返回bool值 逻辑运算符有以下符号
运算符 术语 示例 结果 ! 非 !a 如果a为假,则!a为真 && 与 a && b 如果a和b都为真,则为真 || 或 a || b 若a和b都为假,则为假
3.4.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 10 ;
cout << ! num << endl;
bool flag = true ;
cout << ( num && flag) << endl;
cout << ( num || flag) << endl;
system ( "pause" ) ;
return 0 ;
}
4.程序流程结构
C/C++支持最基本的三种程序运行结构:顺序结构 、选择结构 、循环结构
顺序结构 :程序按顺序执行,不发生跳转选择结构 :依据条件 是否满足,有选择的执行相应功能循环结构 :依据条件 是否满足,循环多次执行某段代码
4.1选择结构
4.1.1if语句
4.1.1.1if语句有三种形式
4.1.1.1.1单行格式if语句
if ( 条件) { 条件满足执行的语句}
# include <iostream>
using namespace std;
int main ( )
{
int score = 0 ;
cout << "请输入你的高考分数:" << endl;
cin >> score;
if ( score > 650 ) {
cout << "恭喜你考上了211大学" << endl;
}
system ( "pause" ) ;
return 0 ;
}
4.1.1.1.2多行格式if语句
if ( 条件) { 满足条件执行} else { 不满足条件执行}
# include <iostream>
using namespace std;
int main ( )
{
int score = 0 ;
cout << "请输入你的成绩:" << endl;
cin >> score;
if ( score > 620 )
{
cout << "恭喜你被我校录取" << endl;
}
else {
cout << "很抱歉,你未被我校录取" << endl;
}
system ( "pause" ) ;
return 0 ;
}
4.1.1.1.3多条件的if语句
if ( 条件1 ) { 满足条件1 执行} else if ( 条件2 ) { 满足条件2 执行} . . . . else { 未满足以上全部条件执行}
# include <iostream>
using namespace std;
int main ( )
{
int score = 0 ;
cout << "请输入你的分数" << endl;
cin >> score;
cout << "你输入的成绩为:" << score << endl;
if ( score > 650 )
{
cout << "你是我省高考成绩的前5%" << endl;
} else if ( score > 600 ) {
cout << "你是我省高考成绩的前10%" << endl;
}
else {
cout << "你的成绩未纳入统计" << endl;
}
system ( "pause" ) ;
return 0 ;
}
4.1.1.1.4嵌套if语句
if ( 条件1 ) { if ( 条件1 _1) { 满足条件1 _1执行语句} else { 未满足条件1 _1执行语句} } else { 未满足条件1 执行执行语句}
4.1.2三目运算
表达式1 ? 表达式2 : 表达式3
4.1.2.1演示
# include <iostream>
using namespace std;
int main ( )
{
double pig1 = 0 ;
double pig2 = 0 ;
double pig3 = 0 ;
cout << "请输入一号猪的体重:" << endl;
cin >> pig1;
cout << "请输入二号猪的体重:" << endl;
cin >> pig2;
cout << "请输入三号猪的体重:" << endl;
cin >> pig3;
pig1 < pig2 ? ( pig2 < pig3 ? ( cout << "最重的小猪pig3:" << pig3 << endl) : ( cout << "最重的小猪pig2:" << pig2 << endl) ) : ( pig1 < pig3? ( cout << "最重的小猪pig3:" << pig3 << endl) : ( cout << "最重的小猪pig1:" << pig1 << endl) ) ;
system ( "pause" ) ;
return 0 ;
}
4.1.3switch语句
switch ( 表达式)
{
case 结果1 : 执行语句; break ;
case 结果2 : 执行语句; break ;
. . .
default : 执行语句; break ;
}
4.1.3.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 0 ;
cout << "请输入需要执行的代码:" << endl;
cin >> num;
switch ( num) {
case 1 : cout << "请输入密码:" << endl;
break ;
case 2 : cout << "请输入账号:" << endl;
break ;
case 3 : cout << "请输入类型:" << endl;
break ;
case 0 : cout << "转接人工!" << endl;
break ;
default : cout << "重听一次" << endl;
}
system ( "pause" ) ;
return 0 ;
}
4.2循环结构
4.2.1while循环语句
while ( 循环条件) { 循环语句 }
4.2.1.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 0 ;
cout << "请输入你需要打印的次数:" << endl;
cin >> num;
while ( num > 0 ) {
cout << "打印剩余" << num << "次数" << endl;
num-- ;
}
system ( "pause" ) ;
return 0 ;
}
4.2.2do…while循环语句
do { 循环语句} while ( 循环条件)
注意点: 与while的区别在于do…while会先执行一次循环语句,在判断循环条件
4.2.2.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 0 ;
cout << "请输入你想循环的次数" << endl;
cin >> num;
do {
cout << "do...while语句会先执行一次循环语句" << endl;
num-- ;
} while ( num > 0 ) ;
system ( "pause" ) ;
return 0 ;
}
4.2.3for循环语句
for ( 起始表达式; 条件表达式;末尾循环体) { 循环语句; }
4.2.3.1演示
# include <iostream>
using namespace std;
int main ( )
{
for ( int num = 10 ; num > 0 ; num-- ) {
cout << "循环的剩余" << num << "次数" << endl;
}
system ( "pause" ) ;
return 0 ;
}
4.2.4嵌套循环
4.2.4.1演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 0 ;
cout << "请输入你想打印的矩阵图像" << endl;
cin >> num;
while ( num > 0 ) {
for ( int i = 10 ; i > 0 ; i-- ) {
cout << "*\t" ;
}
cout << endl;
num-- ;
}
system ( "pause" ) ;
return 0 ;
}
4.3跳转语句
4.3.1break语句
4.3.1.1break使用场景
出现在switch条件语句中 ,作用是终止case并跳出switch 出现在循环语句中 ,作用是跳出当前循环语句 出现在嵌套循环中 ,跳出最近的内层循环语句
4.3.1.2演示
# include <iostream>
using namespace std;
int main ( )
{
cout << "请选择副本难度" << endl;
cout << "1、普通" << endl;
cout << "2、中等" << endl;
cout << "3、困难" << endl;
cout << "4、DLC" << endl;
int selType = 0 ;
cin >> selType;
switch ( selType) {
case 1 :
cout << "你选择的游戏难度为普通" << endl;
break ;
case 2 :
cout << "你选择的游戏难度为中等" << endl;
break ;
case 3 :
cout << "你选择的游戏难度为困难" << endl;
break ;
case 4 :
cout << "你选择的游戏难度为DLC" << endl;
break ;
default :
cout << "你选择的游戏难度无效" << endl;
}
int num = 0 ;
cout << "请输入你要循环的次数" << endl;
cin >> num;
while ( num > 0 ) {
if ( num == 5 ) {
cout << "循环次数无效,即将退出" << endl;
break ;
}
cout << "循环次数有效" << endl;
num-- ;
}
int nums = 0 ;
cout << "请输入你要执行的次数,缺偶数位图标" << endl;
cin >> nums;
while ( nums > 0 ) {
for ( int i = 10 ; i > 0 ; i-- ) {
cout << "* " ;
if ( i < 4 ) {
break ;
}
}
cout << endl;
nums-- ;
}
system ( "pause" ) ;
return 0 ;
}
4.3.2continue语句
在循环语句中,跳过本次循环中余下的尚未执行的语句 ,继续执行下一次循环
4.3.2.1演示
# include <iostream>
using namespace std;
int main ( )
{
for ( int i = 100 ; i > 0 ; i-- ) {
if ( i % 2 == 0 ) {
continue ;
}
cout << i << endl;
}
system ( "pause" ) ;
return 0 ;
}
4.3.3goto语句
goto 标记;
4.3.3.1演示
# include <iostream>
using namespace std;
int main ( )
{
cout << "1、****" << endl;
cout << "2、****" << endl;
goto FLAG;
cout << "3、****" << endl;
cout << "4、****" << endl;
cout << "5、****" << endl;
FLAG:
cout << "6、****" << endl;
cout << "7、****" << endl;
system ( "pause" ) ;
return 0 ;
}
5.数组
5.1概述
所谓数组 ,就是一个集合 ,里面存在了相同类型的数据元素 数组中的每个数据元素都是相同的数据类型 数组是由连续的内存位置 组成的
5.2一维数组
5.2.1一维数组定义方式
一维数组 定义的三种定义
数据类型 数组名[数组长度] 数据类型 数组名[数组长度] = {值1,值2,值3,…} 数据类型 数组名[ ] = {值1,值2,…}
5.2.1.1演示
# include <iostream>
using namespace std;
int main ( )
{
int arr[ 10 ] ;
arr[ 0 ] = 10 ;
arr[ 1 ] = 20 ;
cout << arr[ 1 ] << endl;
int arr2[ 5 ] = { 10 , 20 , 30 , 40 , 50 } ;
cout << arr2[ 2 ] << endl;
int arr3[ ] = { 10 , 20 , 30 , 40 } ;
cout << arr3[ 2 ] << endl;
system ( "pause" ) ;
return 0 ;
}
5.2.2一维数组数组名
一维数组 名称的用途
可以统计整个数组在内存中的长度 可以获取数组在内存中的首地址 注意点: 数组名是一个常量 ,不能修改
5.2.2.1演示
# include <iostream>
using namespace std;
int main ( )
{
int arr[ 5 ] = { 10 , 20 , 30 , 40 , 50 } ;
cout << "获取数组在内存中的大小:" << sizeof ( arr) << endl;
cout << "获取数组中每个元素在内存中的大小:" << sizeof ( arr[ 0 ] ) << endl;
cout << "获取数组的长度:" << sizeof ( arr) / sizeof ( arr[ 0 ] ) << endl;
cout << "数组首地址为:" << arr << endl;
cout << "数组中第一个元素地址为:" << & arr[ 0 ] << endl;
system ( "pause" ) ;
return 0 ;
}
5.2.3冒泡排序
5.2.3.1演示
# include <iostream>
using namespace std;
int main ( )
{
int arr[ ] = { 12 , 10 , 6 , 20 , 15 , 30 , 45 , 20 } ;
for ( int i = 0 ; i < sizeof ( arr) / sizeof ( arr[ 0 ] ) - 1 ; i++ )
{
for ( int j = 0 ; j < sizeof ( arr) / sizeof ( arr[ 0 ] ) - 1 - i; j++ )
{
if ( arr[ j] > arr[ j+ 1 ] )
{
int temp = arr[ j+ 1 ] ;
arr[ j + 1 ] = arr[ j] ;
arr[ j] = temp;
}
}
}
for ( int len = 0 ; len < sizeof ( arr) / sizeof ( arr[ 0 ] ) ; len++ ) {
cout << arr[ len] << "\t" ;
}
cout << endl;
system ( "pause" ) ;
return 0 ;
}
5.3二维数组
5.3.1二维数组定义方式
二维数组定义的四种方式
数据类型 数组名[行数][列数] 数据类型 数组名[行数][列数] = {{数据1,数据2,数据3,…},{数据4,数据5,数据6,…},{数据7,数据8,数据9,…},…} 数据类型 数组名[行数][列数] = {数据1,数据2,数据3,…} 数据类型 数组名[][列数] = {数据1,数据2,数据3,…}
5.3.1.1演示
# include <iostream>
using namespace std;
int main ( )
{
int arr1[ 2 ] [ 3 ] ;
arr1[ 0 ] [ 0 ] = 12 ;
arr1[ 0 ] [ 1 ] = 16 ;
arr1[ 0 ] [ 2 ] = 15 ;
cout << arr1[ 0 ] [ 1 ] << endl;
int arr2[ 3 ] [ 3 ] = { { 2 , 8 , 6 } , { 5 , 9 , 4 } , { 1 , 101 , 15 } } ;
cout << arr2[ 1 ] [ 2 ] << endl;
int arr3[ 2 ] [ 3 ] = { 12 , 15 , 16 , 18 } ;
cout << arr3[ 1 ] [ 0 ] << endl;
int arr4[ ] [ 3 ] = { 12 , 16 , 14 } ;
cout << arr4[ 0 ] [ 1 ] << endl;
system ( "pause" ) ;
return 0 ;
}
5.3.2二位数组数组名
5.3.2.1演示
# include <iostream>
using namespace std;
int main ( )
{
int arr[ 2 ] [ 2 ] = { { 13 , 21 } , { 16 , 19 } } ;
cout << "二维数组占用空间大小:" << sizeof ( arr) << endl;
cout << "二位数组第一行占用空间大小:" << sizeof ( arr[ 0 ] ) << endl;
cout << "二位数组第一个元素占用空间为:" << sizeof ( arr[ 0 ] [ 0 ] ) << endl;
cout << "二维数组的行数为:" << sizeof ( arr) / sizeof ( arr[ 0 ] ) << endl;
cout << "二维数组的列数为:" << sizeof ( arr[ 0 ] ) / sizeof ( arr[ 0 ] [ 0 ] ) << endl;
cout << "二位数组的首地址:" << arr << endl;
cout << "二维数组的第一个元素首地址:" << & arr[ 0 ] [ 0 ] << endl;
system ( "pause" ) ;
return 0 ;
}
6.函数
6.1概述
将一段经常使用的代码封装起来,减少重复代码 一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能
6.2函数的定义
6.2.1实现函数的5个步骤
函数的定义一般主要有5个步骤
返回值类型
函数名 参数列表 函数体语句 return 表达式
如果函数没有返回值 ,return可以不写 或return;
6.2.2函数的语法
返回值类型 函数名 ( 参数列表)
{
函数体语句;
return 表达式;
}
6.2.3函数演示
# include <iostream>
using namespace std;
int add ( int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
int main ( )
{
int sum = add ( 10 , 20 ) ;
cout << sum << endl;
system ( "pause" ) ;
return 0 ;
}
6.3函数的调用
6.3.1函数调用的语法
函数名( 参数)
6.3.2函数调用的演示
# include <iostream>
using namespace std;
double decl ( double d1, double d2)
{
double result = d1 - d2;
return result;
}
int main ( )
{
double d1 = 12.45 ;
double d2 = 5.689 ;
double result = decl ( d1, d2) ;
cout << "计算的结果为:" << result << endl;
system ( "pause" ) ;
return 0 ;
}
6.4值传递
所谓值传递,就是函数调用时实参将数值传入给形参 值传递时,如果形参发生变化 ,并不会影响实参
6.4.1值传递演示
# include <iostream>
using namespace std;
void swap ( int num1, int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
}
int main ( )
{
int num1 = 100 ;
int num2 = 98 ;
swap ( num1, num2) ;
system ( "pause" ) ;
return 0 ;
}
6.5函数常见样式
6.5.1常见函数的4中样式
void noParamter ( )
{
int num = 10 ;
cout << "函数样式之一 无参无返" << endl;
}
void noReturn ( int num)
{
cout << "输入的参数为:" << num << endl;
}
int hasReturn ( )
{
int num = 15 ;
int sum = num * num;
return sum;
}
int hasParamter ( int num)
{
int result = num * 10 ;
return result;
}
6.5.2函数常见样式的演示
# include <iostream>
using namespace std;
void noParamter ( )
{
int num = 10 ;
cout << "函数样式之一 无参无返" << endl;
}
void noReturn ( int num)
{
cout << "输入的参数为:" << num << endl;
}
int hasReturn ( )
{
int num = 15 ;
int sum = num * num;
return sum;
}
int hasParamter ( int num)
{
int result = num * 10 ;
return result;
}
int main ( )
{
int num = 18 ;
noParamter ( ) ;
noReturn ( num) ;
int sum = hasReturn ( ) ;
cout << "无参有返的计算结果:" << sum << endl;
int result = hasParamter ( num) ;
cout << "有参有返的计算结果为: " << result << endl;
system ( "pause" ) ;
return 0 ;
}
6.6函数的声明
告诉编译器函数名称及如何调用函数,函数的实际主体可以单独定义 函数的声明可以多次 ,但是函数的定义只能有一次
6.6.1函数声明的演示
# include <iostream>
using namespace std;
int max ( int a, int b) ;
int main ( )
{
int num1 = 88 ;
int num2 = 99 ;
cout << "取最大数:" << max ( num1, num2) << endl;
system ( "pause" ) ;
return 0 ;
}
int max ( int a, int b)
{
return a < b ? b : a;
}
6.7函数的分文件编写
6.7.1函数分文件编写的4个步骤
# include <iostream>
using namespace std;
void swap ( int a, int b) ;
# include "函数的头文件.h"
void swap ( int num1, int num2)
{
int tmep = num1;
num1 = num2;
num2 = tmep;
cout << "交换数据之后" << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
}
# include <iostream>
using namespace std;
# include "函数的头文件.h"
int main ( )
{
int a = 15 ;
int b = 19 ;
swap ( a, b) ;
system ( "pause" ) ;
return 0 ;
}
7.指针
7.1指针的基本概念
可以通过指针间接访问内存 内存编号时从0开始记录 的,一般用十六进制数字 表示 可以利用指针变量保存地址
7.2指针变量的定义和使用
数据类型 * 变量名
7.2.1指针变量的定义和使用的演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 10 ;
int * p;
p = & num;
cout << "num的地址为:" << & num << endl;
cout << "指针p为: " << p << endl;
* p = 156 ;
cout << "num = " << num << endl;
cout << "*p = " << * p << endl;
system ( "pause" ) ;
return 0 ;
}
7.3指针所占空间
7.3.1指针所占空间的演示
# include <iostream>
using namespace std;
int main ( )
{
int num = 10 ;
int * p;
p = & num;
cout << "指针的地址为:" << p << endl;
cout << "指针所占的内存大小:" << sizeof ( p) << endl;
cout << "指针所占的内存大小:" << sizeof ( int * ) << endl;
cout << "指针所占的内存大小:" << sizeof ( double * ) << endl;
cout << "指针所占的内存大小:" << sizeof ( long * ) << endl;
cout << "指针所占的内存大小:" << sizeof ( long long * ) << endl;
cout << "指针所占的内存大小:" << sizeof ( short * ) << endl;
system ( "pause" ) ;
return 0 ;
}
7.4空指针和野指针
空指针:指针变量指向内存中编号为0的空间 用途: 初始化指针变量 注意: 空指针指向的内存是不可以访问的 0~255之间的内存编号是系统占用的,因此不可以访问
7.4.1空指针的演示
# include <iostream>
using namespace std;
int main ( )
{
int * p = NULL ;
system ( "pause" ) ;
return 0 ;
}
7.4.2野指针的演示
# include <iostream>
using namespace std;
int main ( )
{
int * p = ( int * ) 0x12FA2 ;
system ( "pause" ) ;
return 0 ;
}
7.5const修饰指针
7.5.1const修饰指针有三种情况
7.5.1.1const修饰指针 — 常量指针
常量指针特点: 指针的指向可以修改 ,但是指针指向的值不可以改
# include <iostream>
using namespace std;
int main ( )
{
int num = 20 ;
const int * p = & num;
cout << "当前指针的地址为:" << p << endl;
cout << "当前指针指向的内容:" << * p << endl;
cout << "num = " << num << endl;
int nums = 89 ;
p = & nums;
cout << "当前指针的地址为:" << p << endl;
cout << "当前指针指向的内容:" << * p << endl;
system ( "pause" ) ;
return 0 ;
}
7.5.1.2const修饰常量 — 指针常量
7.5.1.3const即修饰指针,又修饰常量
7.6指针和数组
7.6.1指针和数组演示
# include <iostream>
using namespace std;
int main ( )
{
int arr[ ] = { 15 , 16 , 79 , 23 , 16 } ;
int * p = arr;
cout << "当前指针指向的内容" << * p << endl;
p++ ;
cout << "当前指针指向的内容" << * p << endl;
cout << arr << endl;
system ( "pause" ) ;
return 0 ;
}
7.7指针和函数
7.7.1指针和函数的演示
# include <iostream>
using namespace std;
void swap ( int * p1, int * p2) ;
int main ( )
{
int a = 10 ;
int b = 15 ;
cout << & a << endl;
cout << & b << endl;
swap ( & a, & b) ;
cout << & a << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system ( "pause" ) ;
return 0 ;
}
void swap ( int * p1, int * p2)
{
int temp = * p1;
* p1 = * p2;
* p2 = temp;
cout << "a = " << * p1 << endl;
cout << "b = " << * p2 << endl;
}
7.8指针、数组、函数
# include <iostream>
using namespace std;
void bubbleSort ( int * arr, int len) ;
# include "冒泡排序的头文件.h"
void bubbleSort ( int * arr, int len)
{
int * p = arr;
cout << * p << endl;
for ( int i = 0 ; i < len - 1 ; i++ ) {
for ( int j = 0 ; j < len - i - 1 ; j++ ) {
if ( arr[ j] > arr[ j+ 1 ] ) {
int temp = arr[ j] ;
arr[ j] = arr[ j+ 1 ] ;
arr[ j+ 1 ] = temp;
}
}
}
cout << "排序后的数组: " << endl;
for ( int k = 0 ; k < len; k++ ) {
cout << * p << " " ;
p++ ;
}
cout << endl;
}
# include <iostream>
using namespace std;
# include "冒泡排序的头文件.h"
int main ( )
{
int arr[ ] = { 15 , 32 , 68 , 96 , 12 , 16 , 30 } ;
int len = sizeof ( arr) / sizeof ( arr[ 0 ] ) ;
bubbleSort ( arr, len) ;
system ( "pause" ) ;
return 0 ;
}
8.结构体
8.1结构体基本概念
结构体属于用户自定义的数据模型,允许用户存储不同的数据模型
8.2结构体定义和使用
8.2.1语法
struct 结构体名 { 结构体成员列表}
struct Student
{
string name;
int age;
float score;
} ;
8.2.2结构体创建变量的方法有三种
8.2.2.1方式一
struct 结构体 变量名
struct Student stu1;
stu1. name = "法外狂徒张三" ;
stu1. age = 20 ;
stu1. score = 89.8f ;
cout << "姓名:" << stu1. name << ",年龄:" << stu1. age << ",成绩:" << stu1. score << endl;
8.2.2.2方式二
struct 结构体名 变量名 = { 成员1 值, 成员2 值, . . . }
struct Student stu2 = { "富甲一方田七" , 19 , 85.6f } ;
cout << "姓名:" << stu2. name << ",年龄:" << stu2. age << ",成绩:" << stu2. score << endl;
8.2.2.3方式三
定义结构体时顺便创建变量
struct Person {
string name;
int age;
float Hight;
float weigth;
} person;
person. age = 18 ;
person. name = "知识渊博朝九" ;
person. Hight = 1.86 ;
person. weigth = 75 ;
cout << "姓名:" << person. name << ",年龄:" << person. age << ",身高:" << person. Hight << ",体重:" << person. weigth << endl;
8.2.3结构体定义和使用的演示
# include <iostream>
using namespace std;
# include <string>
int main ( )
{
struct Student
{
string name;
int age;
float score;
} ;
struct Student stu1;
stu1. name = "法外狂徒张三" ;
stu1. age = 20 ;
stu1. score = 89.8f ;
cout << "姓名:" << stu1. name << ",年龄:" << stu1. age << ",成绩:" << stu1. score << endl;
struct Student stu2 = { "富甲一方田七" , 19 , 85.6f } ;
cout << "姓名:" << stu2. name << ",年龄:" << stu2. age << ",成绩:" << stu2. score << endl;
struct Person {
string name;
int age;
float Hight;
float weigth;
} person;
person. age = 18 ;
person. name = "知识渊博朝九" ;
person. Hight = 1.86 ;
person. weigth = 75 ;
cout << "姓名:" << person. name << ",年龄:" << person. age << ",身高:" << person. Hight << ",体重:" << person. weigth << endl;
system ( "pause" ) ;
return 0 ;
}
8.3结构体数组
8.3.1结构体数组语法
struct 结构体名 数组名[ 元素个数] = { { } , { } , { } , . . . }
8.3.2结构体数组演示
# include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
} ;
int main ( )
{
struct student arr[ 3 ] = {
{ "张三" , 18 , 89 } ,
{ "李四" , 19 , 96 } ,
{ "王五" , 20 , 87 }
} ;
cout << sizeof ( arr) << endl;
cout << sizeof ( arr[ 0 ] . name) << endl;
arr[ 1 ] . name = "田七" ;
for ( int i = 0 ; i < 3 ; i++ ) {
cout << "姓名:" << arr[ i] . name << ",年龄:" << arr[ i] . age << ",成绩:" << arr[ i] . score << endl;
}
system ( "pause" ) ;
return 0 ;
}
8.4结构体指针
通过指针访问结构体中的成员 利用操作符 -> 可以通过结构体指针访问结构体属性
8.4.1结构体指针的演示
# include <iostream>
using namespace std;
# include <string>
struct student
{
string name;
int age;
char sex;
} ;
int main ( )
{
struct student stu = { "法外狂徒张三" , 19 , '1' } ;
struct student * p = & stu;
cout << "姓名: " << p-> name << ",年龄: " << p-> age << ",性别: " << p-> sex << endl;
system ( "pause" ) ;
return 0 ;
}
8.5结构体嵌套结构体
8.5.1结构体嵌套结构体演示
# include <iostream>
using namespace std;
# include <string>
struct student
{
string name;
int age;
double score;
} ;
struct teacher
{
int id;
string name;
int age;
struct student stu;
} ;
int main ( )
{
struct student stu = { "法外狂徒张三" , 19 , 89 } ;
struct teacher tea = { 1023 , "罗翔" , 38 , stu } ;
cout << "老师的编号: " << tea. name << ",老师带的学生的姓名: " << tea. stu. name << endl;
system ( "pause" ) ;
return 0 ;
}
8.6结构体做函数参数
8.6.1传递方式有两种
# include <iostream>
using namespace std;
struct student {
string name;
int age;
double score;
} ;
student updatedStudentInfo ( student stu)
{
stu. age = 16 ;
stu. score = 98.1 ;
return stu;
}
int main ( )
{
struct student stu = { "法外狂徒张三" , 18 , 98.8 } ;
student stuInfo = updatedStudentInfo ( stu) ;
cout << "学生姓名: " << stuInfo. name << ",学生年龄:" << stuInfo. age << ",学生成绩: " << stuInfo. score << endl;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
struct student {
string name;
int age;
double score;
} ;
student updatedStudentInfoPath ( struct student * stu) ;
student updatedStudentInfoPath ( struct student * stu)
{
stu-> age = 16 ;
stu-> score = 96.8 ;
return * stu;
}
int main ( )
{
struct student stu = { "法外狂徒张三" , 18 , 98.8 } ;
student* p = & stu;
student stuInfoP = updatedStudentInfoPath ( p) ;
cout << "学生姓名: " << stuInfoP. name << ",学生年龄:" << stuInfoP. age << ",学生成绩: " << stuInfoP. score << endl;
cout << stu. score << endl;
system ( "pause" ) ;
return 0 ;
}
8.7结构体中const使用场景
8.7.1结构体中const使用场景演示
# include <iostream>
using namespace std;
# include <string>
struct student
{
string name;
int age;
float score;
} ;
void printStudentInfo ( student stu) ;
void printStuInfoP ( student* stu) ;
void printStudentInfo ( student stu)
{
cout << "学生姓名: " << stu. name << ",学生年龄:" << stu. age << ",学生成绩: " << stu. score << endl;
}
void printStuInfoP ( const student* stu)
{
cout << "学生姓名: " << stu-> name << ",学生年龄:" << stu-> age << ",学生成绩: " << stu-> score << endl;
}
int main ( )
{
struct student stu = { "法外狂徒张三" , 23 , 94.3f } ;
printStudentInfo ( stu) ;
printStuInfoP ( & stu) ;
system ( "pause" ) ;
return 0 ;
}
补充知识点:
# include <Ctime>
srand ( ( unsigned int ) time ( NULL ) ) ;