C++中的变量可以在多个地方多次声明,但只能定义一次。
在一个文件中的变量的声明、定义和初始化操作:
#include【2】C++ 变量作用域using namespace std; extern int a; //声明 int main() { int a; //定义 a = 5; //初始化 cout << "a = " << a << endl; return 0; }
作用域是程序的一个区域,一般来说有三个地方可以定义变量:
①在函数或一个代码块内部声明的变量,称为局部变量。
②在函数参数的定义中声明的变量,称为形式参数。
③在所有函数外部声明的变量,称为全局变量。
需要注意:当局部变量被定义时,系统不会对其初始化,您必须自行对其初始化。定义全局变量时,系统会自动初始化为下列值:
在 C++ 中,有两种简单的定义常量的方式:
- 使用 #define 预处理器。
- 使用 const 关键字。
#includeusing namespace std; #define LENGTH 10 #define WIDTH 5 #define newline 'n' int main() { int area; area = LENGTH * WIDTH; cout << area; cout << newline; return 0; }
#include【4】数据类型using namespace std; int main() { const int LENGTH = 10; const int WIDTH = 5; const char newline = 'n'; int area; area = LENGTH * WIDTH; cout << area; cout << newline; return 0; }
可以通过运行以下程序来查看你电脑上各种数据类型的大小。
#include【5】typedef 声明#include using namespace std; int main() { cout << "type: tt" << "************size**************"<< endl; cout << "bool: tt" << "所占字节数:" << sizeof(bool); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "char: tt" << "所占字节数:" << sizeof(char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "signed char: t" << "所占字节数:" << sizeof(signed char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned char: t" << "所占字节数:" << sizeof(unsigned char); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "short: tt" << "所占字节数:" << sizeof(short); cout << "t最大值:" << (numeric_limits ::max)(); cout << "tt最小值:" << (numeric_limits ::min)() << endl; cout << "int: tt" << "所占字节数:" << sizeof(int); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "long: tt" << "所占字节数:" << sizeof(long); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "unsigned long: t" << "所占字节数:" << sizeof(unsigned long); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "double: t" << "所占字节数:" << sizeof(double); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "long double: t" << "所占字节数:" << sizeof(long double); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "float: tt" << "所占字节数:" << sizeof(float); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "size_t: t" << "所占字节数:" << sizeof(size_t); cout << "t最大值:" << (numeric_limits ::max)(); cout << "t最小值:" << (numeric_limits ::min)() << endl; cout << "string: t" << "所占字节数:" << sizeof(string) << endl; // << "t最大值:" << (numeric_limits ::max)() << "t最小值:" << (numeric_limits ::min)() << endl; cout << "type: tt" << "************size**************"<< endl; return 0; }
#includeusing namespace std; typedef int zheng; //将int重命名为zheng int main() { zheng a = 50; //定义变量a,a的值为50 cout << "a = " << a << endl; return 0; }
运行结果如下:
a = 50
当您有多个文件且定义了一个可以在其他文件中使用的全局变量或函数时,可以在其他文件中使用 extern 来得到已定义的变量或函数的引用。可以这么理解,extern 是用来在另一个文件中声明一个全局变量或函数。
extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候,如下所示:
第一个文件:main.cpp
#includeint count ; extern void write_extern(); int main() { count = 5; write_extern(); }
第二个文件:support.cpp
#includeextern int count; void write_extern(void) { std::cout << "Count is " << count << std::endl; }
可以看到,在support.cpp文件中需要用到main.cpp文件中定义的count全局变量,因此在support.cpp文件中需要用extern来声明main.cpp文件中的count变量,这样就能实现在support.cpp文件中访问main.cpp文件中的count变量的目的。同理,在main.cpp文件中需要访问support.cpp文件中的void write_extern(void)函数。也需要用extern来声明。
【7】C++运算符参考菜鸟教程,很详细。链接:https://www.runoob.com/cplusplus/cpp-operators.html
【8】C++函数①值传递
②引用传递
③指针传递
参考菜鸟教程,链接:https://www.runoob.com/cplusplus/cpp-functions.html
最后总结值传递与引用传递的区别:
1. 在函数定义格式上有不同:
值传递在定义处是:swap(int x, int y);
引用传递在这义处是:swap(int &x, int &y);
2. 调用时有相同的格式:
值传递:swap(a,b);
引用传递:swap(a,b);
3. 功能上是不同的:
值传递的函数里操作的不是a,b变量本身,只是将a,b值赋给了x,y函数里操作的只是x,y变量而不是a,b,显示a,b的值不会被swap函数所修改。
引用传递swap(a,b)函数里是用a,b分别代替了x,y。函数里操作的是a,b。
C++ 内置了丰富的数学函数,可对各种数字进行运算。下表列出了 C++ 中一些有用的内置的数学函数。
为了利用这些函数,您需要引用数学头文件 。
示例代码如下:
#include【10】C++数组 ①一维数组#include using namespace std; int main () { // 数字定义 short s = 10; int i = -1000; long l = 100000; float f = 230.47; double d = 200.374; // 数学运算 cout << "sin(d) :" << sin(d) << endl; cout << "abs(i) :" << abs(i) << endl; cout << "floor(d) :" << floor(d) << endl; cout << "sqrt(f) :" << sqrt(f) << endl; cout << "pow( d, 2) :" << pow(d, 2) << endl; return 0; }
int a[5] = {1,2,3,4,5};
或者
int a[] = {1,2,3,4,5};
或者
int a[5];
a[0] = 1; a[1] = 2;……
也可以采用for循环赋值;
一个二维数组可以被认为是一个带有 x 行和 y 列的表格。下面是一个二维数组,包含 3 行和 4 列:
int a[3][4] = {
{0, 1, 2, 3} ,
{4, 5, 6, 7} ,
{8, 9, 10, 11}
};
内部嵌套的括号是可选的,下面的初始化与上面是等同的:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
还有指针数组等等,详见菜鸟教程:https://www.runoob.com/cplusplus/cpp-arrays.html
动态二维数组详见:https://blog.csdn.net/qq_26822029/article/details/85037209
指针是用来存放变量地址的。如下:
int *p; int a = 20; p = &a; //将a的地址赋值给p指针
另外,指向指针的指针用来存放指针的地址。如下:
int a = 50; int *p; int **pa; p = &a; //存放变量a的地址 pa = &p; //存放指针p的地址
详见:https://www.runoob.com/cplusplus/cpp-pointers.html
【12】C++结构体话不多说,直接看例子:
#include【13】面向对象——继承#include using namespace std; // 声明一个结构体类型 Books,使用typedef重命名是个好习惯 typedef struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }books; //指针传递 void point_print(books *book); //值传递 void value_print(books book); int main() { books Book1; // 定义结构体类型 Books 的变量 Book1 books Book2; // 定义结构体类型 Books 的变量 Book2 // Book1 详述 strcpy_s(Book1.title, "C++ 教程"); strcpy_s(Book1.author, "Runoob"); strcpy_s(Book1.subject, "编程语言"); Book1.book_id = 12345; // Book2 详述 strcpy_s(Book2.title, "CSS 教程"); strcpy_s(Book2.author, "Runoob"); strcpy_s(Book2.subject, "前端技术"); Book2.book_id = 12346; //调用指针传递函数输出信息 point_print(&Book1); point_print(&Book2); //调用值传递函数输出信息 value_print(Book1); value_print(Book2); return 0; } //指针传递函数,需使用->运算符访问结构体成员 void point_print(books *book) { cout << book->author << endl; cout << book->book_id << endl; cout << book->subject << endl; cout << book->title << endl; return; } //值传递函数,需使用.运算符访问结构体成员 void value_print(books book) { cout << book.author << endl; cout << book.book_id << endl; cout << book.subject << endl; cout << book.title << endl; return; }
三类继承的特点:



