内容来自小破站《黑马程序员C++》复习自用
【学习笔记】C++ 基础入门(一)Hello World 实现 + 数据类型 + 运算符
1 Hello World 实现
1.1 项目创建
① 创建项目 ② 创建文件 ③ 编写代码 ④ 运行程序
下面开始编写代码啦
#include <iostream>
using namespace std;
#include<string>
int main() {
string str = "Hello C++ World";
cout << str << endl;
system("pause");
return 0;
}
1.2 随手加注释,留下正能量
① 单行注释
// 描述信息通常放在一行代码的上方,
// 或者一条语句的末尾,对代码进行说明
② 多行注释
/*描述信息*/
1.3 变量
作用:给一段指定的内存空间起名,方便操作这段内存。
变量创建的语法:数据类型 变量名 = 变量初始值
#include <iostream>
using namespace std;
#include<string>
int main() {
int a = 10;
cout << "a = " << a << endl;
}
1.4 常量
作用:用于记录程序中不可更改的数据,C++中定义常量的两种方式,一个是define,一个是const。
语法:
#define 常量名 常量值
const 数据类型 常量名 = 常量值
首先, #define表示宏常量,通常在文件上方定义,表示一个常量。而const修饰的变量,通常在变量定义之前加关键字const,修饰该变量为常量,不可修改。
#include <iostream>
using namespace std;
#include<string>
#define Day 7
int main() {
cout << "A week has " << Day << "days." << endl;
const int mouth = 12;
cout << "There are" << mouth << " months in a year." << endl;
system("pause");
return 0;
}
1.5 关键字
是C++预先保留的单词(标识符)
2 C++ 数据类型
C++规定在创建一个变量或者是常量时,必须要指出相应的数据类型,否则无法给变量分配内存。
2.1 整形 short、int、long
作用:整型变量表示的时整数类型的数据。
- short(短整型),2字节,-2^15 - 2^15-1
- int (整型),4字节,-2^31 - 2^31-1
- long(长整型)Win4字节,Linux4字节(32位)、8字节(64位),-2^31 - 2^31-1
- long long(长长整型),8字节,-2^63 - 2^63-1
2.2 sizeof 关键字
作用:可以利用 sizeof 求出数据类型占用内存大小。
#include <iostream>
using namespace std;
#include<string>
int main() {
short num1 = 10;
cout << sizeof(short) << endl;
cout << sizeof(num1) << endl;
}
2.3 实型(浮点型)用于表示小数
作用:用于表示小数,浮点型变量分为两种,单精度float,双精度double。
float 4字节 7位有效数字
double 8字节,15-16位有效数字。
float f1 = 3.14f;
// 编译器默认会把单精度识别为双精度,尾部加个f即可解决。
// 默认情况下,输出一个小数,会显示六位有效数字。
2.4 字符型,用于显示单个字符
#include <iostream>
using namespace std;
#include<string>
int main() {
char ch = 'a';
cout << ch << endl;
// 在现实字符型变量的时候,用单引号不要用双引号。
// 单引号内只能有一个字符,不可以是字符串。
// C和C++中,字符变量只占用一个字节。
// 字符型变量并不是吧字符本身放到内存中储存,二十将对应的ASCII编码放入到存储单元。
}
2.5 转义字符,用于表示无法显示的ASCII字符
2.6 字符串型,用于表示一串字符
C风格字符串
#include <iostream>
using namespace std;
int main() {
// char 变量名[] = “字符串值”
char str1[] = "Hello World";
cout << str1 << endl;
}
C++风格字符串
#include <iostream>
using namespace std;
#include<string>
int main() {
// string 变量名 = “字符串值”
string str2 = "Hello World";
cout << str2 << endl;
}
// 使用C++风格字符串的时候,需要包含#include<string>头文件
2.7 布尔数据类型 bool
作用:布尔数据类型代表真或假的指
true –真(本质是1)
false —假(本质是0)
boll类型占用1个字节
#include <iostream>
using namespace std;
#include<string>
int main() {
// 创建一个bool数据类型
bool flag = true; // 代表真
cout << flag << endl;
cout << sizeof(bool) << endl;
}
2.8 数据的输入 cin >> 变量
作用:用于从键盘获取的数据
关键字:cin
语法:cin >> 变量
#include <iostream>
using namespace std;
#include<string>
int main() {
int a = 0;
cout << "请给整型变量a赋值" << endl;
cin >> a;
cout << "整型变量a = " << a << endl;
}
3 C++ 运算符
作用:用于执行代码的运算,算数运算符,赋值运算符,比较运算符,逻辑运算符。
3.1 算数运算符
- 加减乘除 ±*/
- 取模运算(除余)%,即10%3输出1。
- 前置递增 ++a
#include <iostream>
using namespace std;
#include<string>
int main() {
int a = 10;
++a;//让变量+1,将输出a=11
cout << "a = " << a << endl;
//前置递增,先让变量a+1,再进行表达式的运算。
}
#include <iostream>
using namespace std;
#include<string>
int main() {
int a2 = 10;
int b2 = ++a2 * 10;
cout << "a2 = " << a2 << endl;
cout << "b2 = " << b2 << endl; //将会输出a2 = 11; b2 = 110;
}
- 后置递增 a++
#include <iostream>
using namespace std;
#include<string>
int main() {
int b = 10;
b++;//让变量+1,将输出b=11
cout << "b = " << b << endl;
//前置递增,先进行表达式的运算,再让变量b+1。
}
#include <iostream>
using namespace std;
#include<string>
int main() {
int a3 = 10;
int b3 = a3++ * 10;
cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl; //将会输出a3 = 11; b3 = 100;
}
- 前置递减 --a
- 后置递减 a–
3.2 赋值运算符 =、+=、-=、*=、/=、%=
作用:用于将表达式的值赋给变量。
3.3 比较运算符
作用:用于表达式的比较判断,并返回一个真值或者假值。
- 等于==
- 不等于!=
- <、>、<=、>=
#include <iostream>
using namespace std;
#include<string>
int main() {
int a = 10;
int b = 20;
cout << (a == b) << endl;
}
3.4 逻辑运算符 !a、a&&b、a||b
作用:用于表达式的值返回真值或假值
- !a 非 如果a为假,则!a为真;
- a&&b 与 如果a和b都为真,则结果为真;
- a||b 或 如果a和b中有一个为真,则结果为真。
#include <iostream>
using namespace std;
#include<string>
int main() {
int a = 10;
cout << !a << endl; //输出0,在C++中,除了0都为真
int c = 10;
int b = 20;
cout << (c && b) << endl;
}