1. 简介
C++是C语言的继承。它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计。还可以进行以继承和多态为特点的面向对象的程序设计。
基本语法
#include <iostream> //导入头文件
using namespace std; //使用std命名空间
int main(){
cout << "Hello world"; //输出hello world
return 0 ;
}
2. 数据类型、变量
C++常用的数据类型:
- 字符型:关键词 char
- 整型:关键字 int
- 布尔型:关键字 bool
- 浮点型:关键字 float
- 无类型:关键字:void
变量声明:必须以字母或者下划线开头,大小写敏感。
#include <iostream>
#include <typeinfo>
using namespace std;
int main(){
int a;
int b, c, d;
char * e;
float f;
bool g;
cout << "整型变量 d: " << typeid(d).name() << endl; // typeid().name可以获得变量类型
cout << "字符型变量 e: " << typeid(e).name() << endl; // endl表示换行
cout << "浮点型变量 f: " << typeid(f).name() << endl;
cout << "布尔型变量 g: " << typeid(g).name() << endl;
}
赋值:
#include <iostream>
using namespace std;
int main(){
int a;
a = 10;
cout << a << endl;
return 0;
}
3. 常量&运算符
常量
两种方式:#define
和const
#include <iostream>
using namespace std;
#define PI 3.14
int main(){
cout << "宏定义常量: " << PI << endl;
const double pi = 3.14;
cout << "const定义常量: " << pi << endl;
return 0;
}
define
:宏定义,在程序编译前由预处理指令把代码里面的宏变量用指定的字符串替换。没有类型区别。
const
:含有类型的常量定义,编译时会有语法检查。
注:
- 常量不可以被修改,否则会被报错
- 定义常量不赋值会报错
运算符
运算符有:算数运算符、关系运算符、逻辑运算符、位运算符等。
4. 条件判断
if语句
if( ){
}else{
}
三目运算符
定义:有3个操作数的条件运算符,也叫三元运算符。
可以在进行条件判断时,减少代码量。
表达式A ? 表达式B :表达式C
表达式A相当于if后面的条件,如果真,则执行:前的表达式B,否则执行表达式C。
if(num == 10){
cout << "num is equal to 10" << endl;
}else {
cout << "num is not equal to 10" << endl;
}
num == 10 ? "num is equal to 10" : cout << "num is not equal to 10" << endl;
5. 循环
break:退出循环
continue:跳过本次循环
#include <iostream>
using namespace std;
int main(){
int result = 0;
for(int i = 0; i <= 100; i++){
result += i;
}
cout << result << endl;
int result1 = 0;
for(int i = 0; i <= 100; i++){
if(i == 50){
break;
}
result1 += i;
}
cout << result1 << endl;
int result2 = 0;
for(int i = 0; i <= 100; i++){
if(i == 50){
continue;
}
result2 += i;
}
cout << result2 << endl;
}
结果:
6. 数字
字节
1T = 1024G
1G = 1025M(兆)
1M = 1024K
1K = 1024byte(字节)
1byte = 8bit(比特)
bit是最小的单位,计算机内部都是通过二进制数字通信的。bit可以存储0或1。
short(短整型):占用2字节,16位,因此它能表示的数字范围是-2^15 ~ 2^15 - 1。
int(整型):占4字节,32位,能表示的数字范围是-2^31 ~ 2^31 - 1。
long(长整型):占8字节,64位。。。
float(单精度浮点型):占4字节,32位。保留小数点后7位。
double(双精度浮点型):占8字节,64位。保留小数点后16位。
sizeof()
是一个内存度量函数,可以以字节位单位返回一个变量的大小。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
int x = 1, y = 10;
int z;
srand(time(NULL)); //初始化随机数种子
z = rand() % (y - x + 1) + x; //生成一个[x, y]区间内的整数
int num;
while(num != z){
cout << "Please input a number: " << endl;
cin >> num;
if(num > z){
cout << "Big!!" << endl;
}else if (num < z) {
cout << "Small!" << endl;
}
}
cout << "Right!!" << endl;
return 0;
}
7. 字符串
C++中有两种方式来写字符串,char和string。char的方式和C语言相同。
通过char创建字符串有两种方法,需要导入 cstring ,如下:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char * ch1 = "hhhhh"; //ch1放在常量区,不可以被修改
char ch2[] = "233"; //ch2放在栈区,可以修改
cout << ch1 << endl;
cout << ch2 << endl;
strcat(ch2, ch1); //把ch1拼到ch2的尾部
cout << ch2 << endl;
int len = strlen(ch2);
cout << len << endl;
/*
*strcat(ch1, ch2)会报错
*/
return 0 ;
}
String:需要导入string头文件。
#include <iostream>
#include <string>
using namespace std;
int main(){
string str1 = "ddd";
string str2 = "agsd";
string str3;
str3 = str1 + str2;
cout << str3 << endl;
cout << str3.size() << endl;
return 0;
}
8. 数组和函数
#include <iostream>
using namespace std;
int main(){
int arr[] = {1, 2, 3, 4, 5};
int len = sizeof(arr) / sizeof(arr[0]);
cout << len << endl;
for(int i = 0; i < len ; i++){
cout << arr[i] << endl;
}
return 0;
}
sizeof(arr)获得数组大小,除以数组中第一项的大小sizeof(arr[0]),可得到数组的长度。
函数:完成某个特定功能的代码块。