第一个C++程序
#include <iostream> // 使用C++提供的流库
using namespace std; // 使用命名空间,为什么必须是std?
int main() {
// 根据 iostream 提供的函数 打印
std::cout << "hello c++" << std::endl; // 使用命名空间引用函数?
// 也可以不使用命名空间 cout << "hello c++" << endl;
return 0;
}
变量、常量
给指定的一段内存空间起了一个别名,方便操作这个内存空间
内存空间的指定由操作系统完成
#include <iostream>
int main() {
int varA = 100;
cout << varA << endl;
return 0;
}
常量可以使用宏,或者是const
#include <iostream>
using namespace std;
#define DAY 1
int main() {
const double PI = 3.14;
cout << DAY << endl;
cout << PI << endl;
// printf("PI need format %.2f
", PI); // 原始的C写法
return 0;
}
似乎printf函数已经包含在iostream里面了
C++整形
#include <iostream>
#include <cmath>
using namespace std;
int main() {
short n1 = pow(2, 15) - 1; // 2^15-1
int n2 = pow(2, 31) - 1; // 2^31-1
long n3 = pow(2, 31) - 1; // 2^31-1
long long n4 = pow(2, 61) - 1; // 2^61-1
cout << "short maxValue -> " << n1 << endl;
cout << "int maxValue -> " << n2 << endl;
cout << "long maxValue -> " << n3 << endl;
cout << "long long maxValue -> " << n4 << endl;
return 0;
}
SIZEOF 运算符
获取该变量或者是数据类型的字节占用大小
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int temp = 100;
cout << sizeof(temp) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(long long) << endl;
return 0;
}
实型【浮点型】
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// 使用float 需要后缀f
float f = 10.002f;
double d = 3.1415926;
cout << sizeof(float) << sizeof(f) << endl;
cout << sizeof(double) << sizeof(d) << endl;
return 0;
}
字符串型
字节占用1,存储的是对应的ASCII编码
C++支持原生的C字符数组形式创建
也可以使用自己的string
#include <iostream>
using namespace std;
int main() {
char str1[] = "hello c style string";
string str2 = "hello c++ string";
cout << str1 << endl;
cout << str2 << endl;
return 0;
}
布尔类型
最常用的TRUE & FALSE 类型在C++终于有正式的数据类型了
C中的定义是 0 false 非0的正数即 true,字节占用1,可以使用字面量整数写入流程控制中
#include <iostream>
using namespace std;
int main() {
bool flag = true;
if (flag) {
cout << sizeof(bool) << endl;
}
return 0;
}
输入函数
#include <iostream>
using namespace std;
int main() {
string aaa;
cin >> aaa;
cout << aaa << endl;
return 0;
}
GOTO语句
#include <iostream>
using namespace std;
int main() {
cout << "executing program in line1 ..." << endl;
cout << "executing program in line2 ..." << endl;
cout << "executing program in line3 ..." << endl;
cout << "executing program in line4 ..." << endl;
goto FLAG;
FLAG2:
cout << "executing program in line5 ..." << endl;
cout << "executing program in line6 ..." << endl;
FLAG:
cout << "executing program in line7 ..." << endl;
cout << "executing program in line8 ..." << endl;
return 0;
}
直接跳转到指定的标记行执行,不建议使用,可读性差,逻辑混乱
C++ 指针
32位操作系统的任意数据类型的指针类型都是占用4字节大小
64位操作系统则占用8个字节
#include <iostream>
using namespace std;
int main() {
int a = 100;
double b = 3.14;
int * integerPointer = &a;
double * doublePointer = &b;
cout << sizeof(integerPointer) << endl;
cout << sizeof(doublePointer) << endl;
return 0;
}
C++ NEW操作符
之前的C中我们需要使用malloc函数在堆区中申请内存空间进行使用,之后还需要释放该空间
在C++ 中我们可以使用new操作符实现
#include <iostream>
using namespace std;
// 基本语法
void newUsage() {
// new 返回的是一个该数据类型的指针
int * pointer = new int(10);
// 获取数据就需要取引 *p // 堆区的数据由开发者自己管理,创建和释放
cout << *pointer << endl;
// 我们需要释放该内存空间,则使用 delete关键字 (C语言中使用free函数,另外还需要自己做一些安全处理)
delete pointer;
cout << *pointer << endl;
}
// 创建堆区数组
void newArray() {
int * arrPointer = new int[10];
for (int i = 0; i < 10; ++i) {
arrPointer[i] = i + 100;
cout << *(arrPointer + i)/* arrPointer[i] */ << endl;
}
// 释放数组还需要 这样声明标识 ,否则只释放首个元素的内存空间
delete [] arrPointer;
}
int main() {
newUsage();
return 0;
}
C++ 引用
引用和指针的作用相似,对指针操作习惯反而对引用难理解
作用是对变量起别名进行使用
#include <iostream>
using namespace std;
void referenceUsage() {
int a = 100;
cout << a << endl;
// int &refA; 不可以只声明不初始化
int &refA = a;
refA = 200; // *&refA = 200;
cout << refA << endl;
refA = 300; // 引用允许重新赋值
// 但是不允许改变引用的变量
}
int main() {
referenceUsage();
return 0;
}
显而易见使用引用的好出就在于函数形参的问题上
我们希望传递普通变量,进行交换,在C语言中保证改变的变量是实参而非形参
就必须使用指针作为形参传递,使用引用就可以很好的解决这个问题,函数调用时可以很好的表达出我们希望做出的逻辑
#include <iostream>
using namespace std;
void referenceSwap(int &n1, int &n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
void show() {
int a = 10;
int b = 20;
cout << a << " - " << b << endl;
referenceSwap(a, b);
cout << a << " - " << b << endl;
}
int main() {
show();
return 0;
}
用来做函数的返回值和被赋值?
#include <iostream>
using namespace std;
int& returnValue() { // 不要返回局部变量,该变量在函数调用结束后出栈销毁
int a = 100;
return a;
}
int& returnValue2() { // 静态变量在程序结束后释放
static int a = 100;
return a;
}
void essenceOfReference() {
// 引用的本质实际上就是 【指针常量】
int a = 100;
int * const reference = &a;
*reference = a;
int b = 200;
// reference = &b; 不可更改指向
}
int main() {
// int &a = returnValue();
// cout << a << endl; // 不要这样使用
returnValue2() = 300; // 作为左值进行赋值的意义?
return 0;
}
引用的本质
void essenceOfReference() {
// 引用的本质实际上就是 【指针常量】
int a = 100;
int * const reference = &a;
*reference = a;
int b = 200;
// reference = &b; 不可更改指向
}
常量引用,应用的场景,固定形参
void constantFormalParameters(const int &n1, const int &n2) {
// 上述的形参不可以在函数体中发生写入,只允许读取
}