目录
1、C++程序创建步骤(环境Visual Studio 2019)
前言:学习资料参考自《C++ Primer Plus(第6版)中文版》和B站视频《C++教程从0到1入门编程》黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili
1、C++程序创建步骤(环境Visual Studio 2019)
- 打开软件
- 创建项目:选择Visual C++空项目
- 创文件:选择.cpp文件
- 编写和运行代码,以下是简单的Hello world程序例子:
#include<iostram>
using namespace std;
int main()
{
cout<<"Hello world!"<<endl;
return 0;
}
2、C++的注释
- 单行注释:使用//(双斜线),双斜线之后的整行内容为注释
- 多行注释:使用/**/组合,星号*中间的内容为注释
- 快捷键:VS2019中,注释Ctrl+K+C,取消注释Ctrl+K+U
int main() { cout<<"你好"<<endl;//双斜线后面的整行为注释 /* 星号*中间的是 多行注释 */ return 0; }
3、变量的作用
- 存在意义:方便管理内存空间
- C++定义变量的语法:数据类型 变量名=初始值;
int a=10;//创建一个int整型变量,并赋初始值 short b=2,c=3;//同时创建多个short整型变量 int d;//创建时可省略初始值 d=10;//赋值
4、常量的作用
- 作用:记录程序中不可更改的数据。当常量被后续代码修改,编译器会报错。
- 定义常量的两种格式
- (1)#define 宏常量 初始值(通常在文件开头定义)
#define day 7//后面不需要分号
- (2)用const修饰变量(通常在写在函数体内部)
const int month=12;
- (1)#define 宏常量 初始值(通常在文件开头定义)
5、C++的关键字
- 概念:关键字是C++预留的单词(标识符),具有特定的功能
- 意义:在定义变量时,不能将它们用作变量名
- 常见的关键字如:if,return,double,int,struct,class,void,case,catch,new,virtual,whlile等
C++的关键字
asm continue extern int volatile nVint; register switch union auto default false long reinterpret_cast template unsigned bool delete float mutable return this using break do for namespace short throw virtual case double friend new signed true void catch else goto operator sizeof try volatile char enum if private static typedef wchar_t class explicit inline protected static_cast typeid while const export int public struct typename
6、C++的标识符命名规则(变量、常量、函数、类取名规则)
- 标识符不能有关键字
- 只能由字母、数字和下划线组成
- 第1个字符只能是字母或下划线,不能用数字开头
- 标识符中字母区分大小写
int a_1;//只能由字母、数字或下划线组成 int a1,A1;//字母区分大小写 int _a1;//开头只能字母或下划线开头
7、常见的数据类型
数据类型的作用:给变量分配合适的内存空间。