C++基础入门
文章目录
1 C++初识
1.1 第一个C++程序
编写代码
#include <iostream>
using namespace std;
int main()
{
cout << "hello,world!" << endl;
system("pause");
return 0;
}
1.2 注释
// 单行注释
/* */ 多行注释
1.3 变量
同c语言
1.4 常量
C++定义常量两种方式
- #define 宏常量
- const 数据类型 常量名 = 常量值
示例1 define
#include <iostream>
using namespace std;
#define Day 7
int main()
{
cout << "一周总共有:" << Day << "天" << endl;
system("pause");
return 0;
}
示例2 const
#include <iostream>
using namespace std;
int main()
{
const int Day = 7;
cout << "一周总共有:" << Day << "天" << endl;
system("pause");
return 0;
}
1.5 关键字
同c语言
1.6 标识符命名规则
同c语言
2 数据类型
2.1 整型
同c语言
2.2 sizeof关键字
作用:利用sizeof可以统计数据类型所占内存大小
语法:
sizeof(数据类型/变量)
示例:
#include <iostream>
using namespace std;
int main()
{
cout << "short类型所占内存空间为:" << sizeof(short) << endl;
cout << "int类型所占内存空间为:" << sizeof(int) << endl;
cout << "long类型所占内存空间为:" << sizeof(long) << endl;
cout << "long long类型所占内存空间为:" << sizeof(long long) << endl;
system("pause");
return 0;
}
2.3 实型
- 单精度float
- 双精度double
示例:
#include <iostream>
using namespace std;
int main()
{
float f1 = 3.14f;
cout << "f1 = " << f1 << endl;
double d1 = 3.14;
cout << "d1 = " << d1 << endl;
cout << "float占用内存空间为:" << sizeof(float) << endl; //4字节
cout << "double占用内存空间为:" << sizeof(double) << endl; //8字节
//科学计数法
float f2 = 3e2; //3*10^2
cout << "f2 = " << f2 << endl;
system("pause");
return 0;
}
2.4 字符型
示例:
#include <iostream>
using namespace std;
int main()
{
char ch = 'A';
cout << ch << endl; //创建字符型变量
cout << "内存空间:" << sizeof(char) << endl; //1字节
cout << (int)ch << endl; //字符型变量对应ASCII码
system("pause");
return 0;
}
2.5 转移字符
同c语言
2.6 字符串型
两种风格
- c风格:
char 变量名[] = ""
- c++风格
string 变量名 = ""
示例:
#include <iostream>
using namespace std;
#include <string>
int main()
{
char str[] = "hello world";
cout << str << endl;
string str2 = "hello world";
cout << str2 << endl;
system("pause");
return 0;
}
2.7 布尔类型 bool
bool类型只有true(1)和false(0)两个值,占1个字节大小
示例:
#include <iostream>
using namespace std;
int main()
{
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
cout << "占用内存:" << sizeof(bool) << endl;
system("pause");
return 0;
}
2.8 数据的输入
关键字:cin
语法:
cin >> 变量
示例:
#include <iostream>
using namespace std;
#include <string>
int main()
{
//整型
int a = 0;
cout << "给a赋值:" << endl;
cin >> a;
cout << "a:" << a << endl;
//浮点型
float f = 3.14f;
cout << "给f赋值:" << endl;
cin >> f;
cout << "f:" << f << endl;
//字符型
char ch = 'a';
cout << "给ch赋值:" << endl;
cin >> ch;
cout << "ch:" << ch << endl;
//字符串型
string str = "hello";
cout << "给str赋值:" << endl;
cin >> str;
cout << "str:" << str << endl;
//布尔型
bool flag = false;
cout << "给flag赋值:" << endl;
cin >> flag;
cout << "flag:" << flag << endl;
system("pause");
return 0;
}
3 运算符
同c语言
4 程序流程结构
同c语言
5 数组
同c语言
6 函数
函数的分文件编写
- 创建后缀名为.h的头文件
- 创建后缀名为.cpp的源文件
- 在头文件中写函数的声明
- 在源文件中写函数的定义
示例:
//swap.h文件
#include <iostream>
using namespace std;
void swap(int a, int b);
//swap.cpp文件
#include "swap.h"
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
#include <iostream>
using namespace std;
#include "swap.h"
int main()
{
int a = 10;
int b = 20;
swap(a, b);
system("pause");
return 0;
}
7 指针
7.1 指针的定义和调用
示例:
#include <iostream>
using namespace std;
int main()
{
// 定义指针
int a = 10;
int* p;
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "指针p为:" << p << endl;
//使用指针
*p = 1000;
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;
system("pause");
return 0;
}
7.2 指针所占内存空间
在32位:指针占4个字节空间大小
在64位:指针占8个字节空间大小
示例:
#include <iostream>
using namespace std;
int main()
{
// 指针所占内存空间
int a = 10;
int* p = &a;
cout << "sizeof (int *) = " << sizeof(p) << endl; //4字节
system("pause");
return 0;
}
7.3 空指针
空指针:指针变量指向内存中编号为0的空间
用途:初始化指针变量
注意:空指针指向的内存是不可以访问的
示例:
#include <iostream>
using namespace std;
int main()
{
//空指针用于给指针变量进行初始化
int* p = NULL;
//空指针是不可以访问的
//内存编号0~255为系统占用内存,不允许用户访问
cout << *p << endl; //报错
system("pause");
return 0;
}
7.4 野指针
野指针:指针变量指向非法的内存空间
示例:
#include <iostream>
using namespace std;
int main()
{
int* p = (int*)0x1100;
cout << *p << endl; //报错
system("pause");
return 0;
}
总结:空指针和野指针都不是我们申请的空间,因此不要访问。
7.5 const修饰指针
- const修饰指针 ——常量指针
- const修饰常量 ——指针常量
- const即修饰指针,又修饰常量
示例:
#include <iostream>
using namespace std;
int main()
{
//1. const修饰指针 常量指针
int a = 10;
int b = 10;
const int* p = &a;
//指针指向的值不可以改,指针的指向可以改
//*p = 20; 错误
p = &b; //正确
//2. const修饰常量 指针常量
int* const p2 = &a;
//指针指向的值可以改,指针的指向不可以改
*p2 = 100; //正确
// p2 = &b; 错误
//3. const修饰指针和常量
const int* const p3 = &a;
//指针指向的值和指针的指向都不可以改
system("pause");
return 0;
}
技巧:看const右侧跟着的是指针还是常量,是指针就是常量指针,是常量就是指针常量
7.6 指针和数组
作用:利用指针访问数组中元素
示例:
#include <iostream>
using namespace std;
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
cout << "访问第一个元素:" << *p << endl;
p++;
cout << "访问第二个元素:" << *p << endl;
int* p2 = arr;
for (int i = 0;i < 10;i++)
{
cout << *p2 << endl;
p2++;
}//利用指针遍历数组
system("pause");
return 0;
}
7.7 指针和函数
作用:利用指针做函数参数,可以修改实参的值
示例:
#include <iostream>
using namespace std;
void swap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void swap02(int* p1, int* p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main()
{
//值传递,不可以修改实参
int a = 10;
int b = 20;
//swap01(a, b);
//地址传递,可以修改实参
swap02(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
7.8 指针、数组、函数
冒泡排序
#include <iostream>
using namespace std;
void bubble(int *arr, int len)
{
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;
}
}
}
}
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(arr[0]);
bubble(arr, len);
printArray(arr, len);
system("pause");
return 0;
}
8 结构体
结构体中const使用场景
#include <iostream>
using namespace std;
#include <string>
struct student
{
string name;
int age;
int score;
};
//const使用场景
void printStudents(const student *s) //加const防止函数体中的误操作
{
cout << s->name << s->age << s->score << endl;
}
int main()
{
struct student s = { "张三",15,70 };
printStudents(&s);
system("pause");
return 0;
}