小言_互联网的博客

C++学习日记1--c++基本介绍、C++的输入输出、运算符、C++的内存分配模型、new的用法、C++的引用介绍

448人阅读  评论(0)

一、C++简介

1、c++语法特性?

c++是一种面向对象的语言,任何一个事情都是某一个对象上的行为和属性

2.C++语法

c++继承了c语言的语法之外,还拓展了数据类型,继承、多态等新语法

3.C++函数接口

C++提供了标准模板库(STL),它里面提供了一些通用的接口,全世界的人都使用一个接口

 

二、书写第一个C++程序

1.创建一个c++的工程的文件

C++  工程文件的格式 xxx.cpp         (c++  plus plus)

2.比那及代码

1)C++是兼容c的,也是从main开始,从main结束

2)如果要使用c++ 语法必须要包含标准头文件以及对应的命名空间

#include<iostream>  ----标准头文件

using namespace std;  ----标准的命名空间

(在c++中有很多的工具都是放在这个命名空间中,如果你想在C++的代码中直接使用这些工具,那么就必须使用我们的标准的命名空间)

(如果不使用命名空间,那么你使用工具时,就必须在工具前面加作用于符号)

3)c++中使用cout这个工具来输出

4)endl 相当于“\n”

 


  
  1. #include<iostream>
  2. using namespace std:
  3. int main(int argc ,char*argv[]){
  4. cout << "hellowworld" << endl;
  5. return 0
  6. }

5)编译

 g++ 1.cpp -o 1

 

三、变量

1.变量的作用

给一段指定的内存空间起一个名字,方便我们去操作他

2.如何在C++程序中输出一个变量的值


  
  1. #include<iostream>
  2. using namespace std;
  3. int main(int argc ,char*argv[]){
  4. cout << "hellowworld" << endl;
  5. int a = 10;
  6. cout<< "a="<<a<< endl;
  7. return 0;
  8. }

四、常量。

1、常量的作用? 用于记录程序中不可修改的数据。

2、C++定义常量方法有两种:

1)宏常量。


  
  1. #include <iostream>
  2. using namespace std;
  3. #define DAY 7
  4. int main(int argc, char *argv[])
  5. {
  6. cout << "一个星期有:" << DAY << "天" << endl;
  7. return 0;
  8. }

2)const修饰的变量。


  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. const int month = 12; //变量
  6. //month = 13;
  7. cout << "一年有:" << month << "月" << endl;
  8. return 0;
  9. }

五、字符串

1.在C++中有两种风格可以表示字符串

 

C风格的字符串:

语法:char 数组名[]=“字符串的值”

 

C++风格字符串:

语法: string字符串名=“字符串值”


  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char b[]= "aaaaaa";
  6. string a = "ghjad";
  7. cout << a << endl;
  8. cout << b << endl;
  9. return 0;
  10. }

六、转义符号

常用的转移符号:

“\n”  ---换行

“\t” ----水平制表,跳转到下一个tab键的位置


  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "helloe\n";
  6. cout << "aaaaa\thelloe\n";
  7. cout << "aaa\thelloe\n";
  8. cout << "a\thelloe\n";
  9. return 0;
  10. }

七、数据的输入

1.作用:从键盘获取一些数据

2.关键词:cin


  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. string a;
  6. int b;
  7. double c;
  8. cin >> a >> b >> c;
  9. cout << a << endl
  10. << b << endl
  11. << c << endl;
  12. return 0;
  13. }

另外c++ 的cin会自动清除缓存区,不需要担心缓存区 有留存东西

八、递增递减运算符

1.作用::变量自身 +1/-1

2.符号 : ++/--

在C++中不支持连续的后置递增:(a++)++

 

九、比较运算符:

==、!=、<、>、<=、>=


  
  1. #include "iostream"
  2. using namespace std;
  3. int main()
  4. {
  5. string a = "aaa";
  6. string b = "aaabbbb";
  7. cout<<(a==b)<< endl;
  8. cout<<(a!=b)<< endl;
  9. cout<<(a>=b)<< endl;
  10. cout<<(a<=b)<< endl;
  11. cout<<(a>b)<< endl;
  12. cout<<(a<b)<< endl;
  13. if (a == b)
  14. {
  15. cout << 1 << endl;
  16. }
  17. else
  18. {
  19. cout << 2 << endl;
  20. }
  21. return 0;
  22. }

结论:

字符串比较大小、按照ascll依次一一比较

 

十、逻辑运算符

1.根据表达式真假,返回真或者假

2.符号

!、&&、||、


  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a = 10;
  6. int b = 10;
  7. int c = 0;
  8. int d = 0;
  9. cout << !a << endl;
  10. cout << !!a << endl;
  11. cout << (a&&b) << endl;
  12. cout << (a||b) << endl;
  13. cout << (a&&c) << endl;
  14. cout << (a||c) << endl;
  15. cout << (c||d) << endl;
  16. cout << (c&&d) << endl;
  17. return 0;
  18. }

十一、内存分区模型

1.在C++程序执行时,将内存大致方向分为四个区域

1)代码区:存放着函数的二进制代码,由操作系统进行管理

2)全局区:存放着全局变量,静态变量、常量

3)堆区:由工程师手动分配与释放、如果不释放,程序结束由系统自动回收。(new、malloc、calloc)

4)栈区:由编译器自动分配和释放、存放的时函数形式参数、局部变量。

2.内存四区的意义?

不同的区域存放不同的数据,赋予它不同的生命周期,给我们更大的灵活编程

十二、程序运行前

在程序编译后,生成一个可执行文件,未执行这个程序之前分为两个区域

1、代码区:

存放CPU执行的机器指令 -> 其实就是指0101这种数据。

代码区是共享的,共享的目的是对于频繁被执行的代码,只需要在内存中有一份代码即可。 无论执行了多少次这段代码,都是使用同一个代码。

代码区是只读的,使其只读的原因是防止程序意外地修改它的指令。

2、全局区:

全局变量与静态变量存放在此。

全局区还包含了常量区、字符串常量、其他常量(const修改的变量)在存放在此。

该区域的数据在程序结束之后由操作系统来释放。


  
  1. #include <iostream>
  2. using namespace std;
  3. int g_a = 10;
  4. int g_b = 20;
  5. const int c_g_a = 10;
  6. int main()
  7. {
  8. int a;
  9. static int s_a = 10;
  10. const int c_l_a = 10;
  11. //栈区
  12. cout << &a << endl; //局部变量
  13. cout << &c_l_a << endl; //const修饰的局部变量-----局部常量
  14. //全局区
  15. cout << &g_a << endl; //全局变量
  16. cout << &g_b << endl; //全局变量
  17. cout << &s_a << endl; //静态变量
  18. cout << "string address:" << & "fhasj" << endl; //字符串常量
  19. cout << &c_g_a << endl; //const修饰的全局变量-----全局常量
  20. //
  21. return 0;
  22. }

结论:

栈区:  

                局部变量

                const修饰的局部变量(局部常量)

在全局区

              全局变量

              静态变量

              常量

              const修饰的全局变量(全局常量)

           

十三、程序运行后 

 栈区: 由编译器自动分配释放。存放的函数的形式参数、局部变量等

 注意: 不要返回局部变量的地址

 

堆区:由工程师手动分配与释放、如果不释放,程序结束由系统自动回收。

在C++中主要利用new堆区开辟空间


  
  1. #include <iostream>
  2. using namespace std;
  3. int *func()
  4. {
  5. //利用new关键词、将数据开辟到堆取
  6. int *p = new int( 10);
  7. return p;
  8. }
  9. int main()
  10. {
  11. int *p = func();
  12. cout << (*p) << endl;
  13. return 0;
  14. }

             结论:在函数返回的时候,堆区的空间不会释放

十四、new操作符。

C++中利用new操作符在堆区开辟数据。 堆区中开辟的数据,由工程师手动开辟,手动释放、释放利用操作符delete。

语法:new 数据类型 利用new创建的数据,会返回该数据对应类型的指针。

1、 堆区的基本语法示例。


  
  1. #include <iostream>
  2. using namespace std;
  3. int *func()
  4. {
  5. //在堆区中创建整型数据。
  6. int *p = new int( 10);
  7. return p;
  8. }
  9. int main()
  10. {
  11. int p = func();
  12. cout << "p = " << *p << endl; //10
  13. //堆区的数据由用户主动释放。
  14. delete p;
  15. p = NULL;
  16. cout << "*p = " << *p << endl; //段错误
  17. return 0;
  18. }

 

2.在堆区利用new开辟数组


  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int *arr = new int[ 10];
  6. int i;
  7. for (i = 0; i < 10; i++)
  8. {
  9. arr[i] = i + 100;
  10. }
  11. for (i = 0; i < 10; i++)
  12. {
  13. cout << arr[i] << endl; //10
  14. }
  15. delete[] arr;
  16. return 0;
  17. }

十五、引用的基本使用

1.引用作用:

给变量起别名

2.语法:

数据类型  &别名=原名


  
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a = 10;
  6. int &b = a;
  7. cout << b << endl;
  8. b= 100;
  9. cout << b << endl;
  10. cout << a << endl;
  11. return 0;
  12. //注意应用对象必须初始化
  13. }

注意   1).引用必须初始化

           2) 引用在初始化之后。不可以改变,即b永远都是a的别名了

十七、引用做函数的参数

作用:函数传参时,我们可以利用引用 的技术让形参修饰实参、

优点: 可以简化指针修饰实参的方法、


  
  1. #include <iostream>
  2. using namespace std;
  3. void fun(int &a, int &b)
  4. {
  5. //这个a虽然是一个形参,但是实际上代表的是一个实参
  6. int tmp = a;
  7. a = b;
  8. b = tmp;
  9. }
  10. int main()
  11. {
  12. int a = 10;
  13. int b = 20;
  14. fun(a, b);
  15. cout << a << endl;
  16. cout << b << endl;
  17. return 0;
  18. }

 

十八、引用做函数的返回值

作用:引用是可以作为函数的返回值存在的

注意事项: 

1)不要返回局部变量的引用

2)函数的调用可以作为左值的存在


  
  1. #include <iostream>
  2. using namespace std;
  3. //引用作为函数返回值
  4. //1、 不要返回局部变量的引用。
  5. //2、 函数的调用可以作为左值的存在。
  6. //int --> 返回的是10(本体的值)
  7. //int& --> 返回的是a (本体)
  8. int &test01()
  9. {
  10. //1、不要返回局部变量的引用。
  11. //int a = 10; //局部变量
  12. static int a = 10;
  13. return a; //其实就是返回a的本体。
  14. }
  15. int main()
  16. {
  17. int &ret = test01();
  18. cout << "ret = " << ret << endl; //10
  19. //2. 函数的调用可以作为左值的存在。
  20. test01() = 1000; //a = 1000
  21. cout << "ret = " << ret << endl; //1000
  22. return 0;
  23. }

 

引用的详细图解

 

 

 

 

 

 

 

 

 

 

 

 


转载:https://blog.csdn.net/weixin_41558261/article/details/116975490
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场