C++基础学习
1.新建项目
使用Clion软件,官网下载即可。
新建一个cpp代码文件
Hello World!, main函数只能有一个
#include "iostream"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
代码基础结构
#include "iostream"
using namespace std; // 前两行,预处理指令,代码编译前的准备工作
int main() // 主函数,程序入口,程序从这里开始执行
{
// cout 对外输出 中间是输出信息 endl 输出一个换行
cout << "Hello world!" << endl; // 具体逻辑功能代码
return 0;
}
2.单工程,多main函数设置
方法1:修改main函数名称即可
方法2:新建文件时修改设置,去勾选该选项
3.手动编译代码并运行
工具:MinGW
地址:MinGW - Minimalist GNU for Windows download | SourceForge.net
环境变量设置
验证环境变量
g++ -v
新建一个文本文件,编写代码,然后修改后缀为.cpp
打开命令框,进入文件所在位置,输入编译命令
g++ hello.cpp -o hello.exe
运行程序
hello.exe
4.VSCode配置C++环境
下载两个插件
新建.vscode文件夹
新增c_cpp_properties.json配置文件
{
"configurations": [
{
"name": "Win32",
"includePath": ["${workspaceFolder}/**"],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "F:\\software\\mingw\\bin\\g++.exe",
"cStandard": "c++17",
"intelliSenseMode": "${default}"
},
"version":4
]
}
新增launch.json配置文件
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Windows 上的 Bash 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "F:\\software\\mingw\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
新建tasks.json配置文件
{
"version": "2.0.0",
"tasks": [
{
"label": "task g++",
"type": "shell",
"command": "F:\\software\\mingw\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\C++Learning\\VSday1",
"-std=c++17"
],
"options": {
"cwd": "F:\\software\\mingw\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "F:\\software\\mingw\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}
按F5启动,有问题就把lauch.json文件输出启动。
4.基础语句
1.cout输出语句
非数字需要用""包围,数字可以包围,也可以不包围
#include "iostream"
using namespace std;
int main()
{
cout << "I love Java" << endl;
cout << 10 << endl;
cout << 1 << "小于" << 2 << endl;
return 0;
}
2.字面常量
#include "iostream"
using namespace std;
int main()
{
// 整形
20;
// 实型(小数)
180.5;
// 字符
'a';
// 字符串
"abc";
return 0;
}
3.符号常量
#include "iostream"
#include "windows.h"
using namespace std;
#define MAX_LENGTH 100
// 符号常量: #define 名称(标识符) 常量值 符号常量定义在代码的头部
// 符号常量的定义,不需要分号结尾
int main()
{
SetConsoleOutputCP(CP_UTF8);
cout << MAX_LENGTH << endl;
cout << "14行设置编码,需要引入window.h头文件,解决中文乱码问题" << endl;
// 方式二
system("chcp 65001");
return 0;
}
4.变量声明和赋值
#include "iostream"
using namespace std;
int main()
{
system("chcp 65001");
int age;
float height;
char gender;
string name;
age = 21;
height = 180.5;
gender = 'm';
name = "小明";
cout << age << endl;
cout << height << endl;
cout << gender << endl;
cout << name << endl;
return 0;
}
5.整形
#include "iostream"
#include "windows.h"
using namespace std;
int main()
{
SetConsoleOutputCP(CP_UTF8);
short age = 21;
// unsigned 无符号整形,不能表示负数
unsigned short age1 = 21;
// 快捷写法
u_short age2 = 20;
int num1 = 10;
long num2 = 10;
long long num3 = 10;
// sizeof() 函数, 获取数据所占用的字节
cout << "short占的字节" << sizeof(age) << endl;
}
6.实型
#include "iostream"
using namespace std;
int main()
{
// 不区分是否有符号,默认有符号
float num1 = 12.23;
double num2 = 12.333;
long double num3 = 12.333333222;
// 转换为字符串的方法
string str = to_string(num1);
cout << fixed; // 设置为小数显示,负责输出为科学计数法
cout.width(10); // 设置显示的最大宽度(最大位数)
}
7.cin 输入数据
#include "iostream"
using namespace std;
int main()
{
// cin >> 变量
int num;
cin >> num;
}
中文乱码解决方法
ctrl + shift + alt + / 快捷键
8.生成随机数
#include "iostream"
#include "random"
using namespace std;
int get_random_num(int min, int max)
{
// 创建一个随机数生成器
random_device rd;
mt19937 gen(rd());
// 定义一个均匀分布的整数范围
uniform_int_distribution<> dis(min, max);
// 生成一个随机数并输出
int random_number = dis(gen);
return random_number;
}
int main()
{
int num = get_random_num(1, 10);
cout << num << endl;
return 0;
}
9.枚举
#include "iostream"
using namespace std;
int main()
{
enum Color {
RED = 2, // 从数字几开始
YELLOW,
BLUE
};
int num;
cin >> num;
switch (num) {
case RED:
cout << RED << endl;
break;
case YELLOW:
cout << YELLOW << endl;
break;
case BLUE:
cout << BLUE << endl;
break;
default:
return 0;
}
}
10. goto语句,配合标签食用
#include "iostream"
using namespace std;
int main()
{
a: // 标签
{
cout << "a标签的代码" << endl;
goto b;
}
b:
{
cout << "b标签的代码" << endl;
}
}
11.数组
#include "iostream"
using namespace std;
int main()
{
int v[5]; // 定义数组
v[0] = 1;
v[1] = 2;
v[2] = 3;
v[3] = 4;
v[4] = 5;
int i = 0;
while (i < sizeof(v) / sizeof(v[0])) {
cout << v[i] << endl;
}
// 第二种定义
int v2[] = {1, 2, 3, 4, 5};
for (int item : v2) {
cout << item << endl;
}
for (auto &item: v){
cout << item << endl;
}
int v3[3][3];
v3[0][0] = 00;
v3[1][1] = 11;
int v4[3][3] = {
{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
}
12.指针
#include "iostream"
#include "windows.h"
using namespace std;
int main()
{
SetConsoleOutputCP(CP_UTF8);
int num = 10;
int * p;
p = #
int num1 = 20;
int * p1 = &num1;
*p1 = 10;
cout << "num的变量地址" << p << endl;
cout << "指针p指向的内存地址的值" << *p << endl;
// 野指针
int * p2;
* p2 = 10;
// 空指针
int *p3 = NULL;
}
5.构造函数和析构函数
#include "iostream"
#include "windows.h"
using namespace std;
class Person
{
public:
Person()
{
cout << "Person 构造函数" << endl;
}
~Person()
{
cout << "Person 析构函数" << endl;
}
};
int main()
{
SetConsoleOutputCP(CP_UTF8);
Person person;
}