C++学习笔记(5) C语言与C++的区别

两者关系:

  1. C++是C的一些扩充

    1. 头文件

      C语言 C++
      stido.h iostream
      math.h cmath
      string.h cstring
      sdilib.h cstdlib
      。。。
    2. 命名空间

      1. 重复的标识符

        using namespace std;	//使用std命名空间
        
        	std :: cout << ....	注明cout来自于哪个命名空间
        

        cout cin endl 都是在 std 的命名空间

    3. 输入输出iostream

      1. printf() scanf()
      2. cout << //输出
      3. cin << //输入
      4. endl << //换行
      5. ends <<
    4. 基本数据类型

      1. 几乎一样

        1.  int short char  float  double  usigned  long
          
      2. C++新增布尔变量

        1.  true    false
          
        2. 实际运行是 true当做1来使用,false 当做 0

        3. 让代码看上去更直观,while(true)

    5. 结构体

      在 C 中定义一个结构体,类型的struct不能省略,C++中可以省略

      struct Stu{
      	int m;
      	int age;
      };
      Stu s1; //c++风格
      struct Stu s1; //c风格
      
    6. 强制类型转换

      double a = 3.8;
      double b = 6.6;
      int c = 30;
      int d;
      
      d = c  + (int)(a + b); //c风格
      d = c  + int(a + b); //c++风格
      
    7. 条件运算符 ? :

      (a<b)? a:b;    //C风格,只能去使用返回值	本质:变量
      ((a<b)? a:b )= 20; //c++风格,可以进行赋值  本质:内存
      
    8. for循环

      int i;
      for(int i = 0;i<10;i++)//c中必须先定义
      for(int i = 0;i<10;i++)//C++中在第一个表达式中定义,节约内存,程序结束释放
      
    9. 作用域运算符 ::

      1. 区分全局和局部(变量与函数)

        int a = 20;
        int main(){
            int a = 10;
            cout << a << endl; 
            cout <<::a << endl;
            while(1);
            return 0;
        }
        

        在这里插入图片描述

      2. 类的作用域

        男人 吃饭 eat()

        女人 吃饭 eat()

        cat 吃饭 eat()

      3. 命名空间

        using namespace std;

    10. 动态内存分配

      编程语言 开辟内存 释放内存 方式
      C malloc free 函数
      C++ new new[] delete delete[] 运算符
      int main(){
          int *p1,*p2,*p3;
          p1 = new int;//int  4B内存空间
          p2 = new int[5];//指向 5个 int 数组首地址,20B
          p3 = new int(5);//指向 int 4B空间,初始化为5
          delete p1;
          delete[] p2; //释放首地址为p2的数组内存段
          delete p3;
          while(1);
          return 0;
      }
      
    11. 变量的引用&(important)

      void swap(int a,int b){//直接赋值
          int t;
          t = a;
          a = b;
          b = t;
      }
      void swap(int* a,int* b){//传入指针变量
          int t;
          t = *a;
          *a = *b;
          *b = t;
      }
      void swap(int& a,int& b){//变量的引用,给内存段取别名
          int t;
          t = a;
          a = b;
          b = t;
      }
      int main(){
          int x = 10;
          int y = 20;
          swap(x,y);//直接赋值,只是把实际参数传递给形式参数,交换的是形式参数
          cout << x << endl << y<<endl;
          swap(&x,&y);//访问内存,交换地址绑定的内存段
          cout << x << endl << y<<endl;
          swap(x,y);//引用,不会新开辟内存空间,共享一个内存
          int& c=x;//c和x共享一个内存段,等价
          cout << x << endl << y<<endl;
          while(1);
          return 0;
      }
      
    12. 内置函数内联

      在调用函数的时候,需要一定时间或者空间开销。(跳转)

      在这里插入图片描述

      C语言:宏函数

      #define Fun(a,b) (a*b + a/b) 
      //预处理i--汇编s--编译obj--连接exe
      int main(){
      int x = 10;
      int y = 20;
      Fun(x,y);//简单代码替换 
      }
      

      C++:内置函数 (内联)

      关键字 inline:编译的过程中,将所调用的函数代码直接嵌入到主调函数中

      注意:

      1. 内置函数可以节省运行时间,增加目标代码的长度

      2. 是使用内置函数时,5行以内,经常出现的函数

      3. 不能包含负责的语句,循环,switch

      4. inline 比较灵活,并不是一个指令性的关键字

        ​ inline 只是一个建议,编译器采纳与否与程序员无关

      inline int Max(int a,int b,int c){
      	if (a < b) a =b;
      	if ( a< c) a = c;
      	return a;
      }
      int main(){
          int x = 10;
          int y = 20;
          int z = 18;
          max = Max(x,y); //本质也是进行替换
          return 0;
      }
      
    13. 带默认参数的函数

      1. 声明时给形式参数赋值,调用时可以不用赋值
      2. 调用函数是,实际参数个数 = 形式参数个数 - 默认参数个数
      3. 形式参数有一些初始化,一些不初始化,需要初始化的 放在参数列表 右边
      int Max(int a = 10,int b = 15,int c=18);//声明时赋值
      int main(){
          cout << Max();
          int x= 50;int y = 40;int z =80;
          cout << Max(x);
          cout << Max(x,y);
          cout << Max(x,y,z);
          while(1);
          return 0;
      }
      int Max(int a,int b,int c){//定义不用赋值
      	if (a < b) a =b;
      	if (a < c) a = c;
      	return a;
      }
      
    14. 函数的重载

      1. 函数名一样,参数列表个数、类型、顺序不一样

      2. 在调用的时候,编译器会自动选择合适的函数

      3. 返回值类型不能决定是不是函数重载

      4. 不能有歧义,函数重载不能与带默认参数的函数起冲突

      5. 运算符重载

        & 取地址运算符、按位与、引用

        class complex{ 
        
        };
        
        complex operator +(complex& c1 ,complex &c2)
        
      int Max(int a,int b,int c);
      float Max(float a,float b,float c);
      double Max(double a,double b,double c);
      int main(){
          float a = 50;float b = 40;float c =80;
          cout << Max(a,b,c);
          while(1);
          return 0;
      }
      int Max(int a,int b,int c){//定义不用赋值
      	if (a < b) a =b;
      	if ( a< c) a = c;
      	return a;
      }
      int Max(int a,int b,int c){}
      int Max(int a,int b,int c){}
      
    15. 函数模板(参数化思想)

      1. 函数模板,模板函数

        1. 建立一个通用的函数,函数的类型和参数的类型可以不指定
        2. 凡是函数体相同的多个函数,都可以使用函数模板
      2. 类模板,模板类

        参数化

        1. ​ 软件开发过程中必须要掌握的一种思想,他提出将各种相同的概念用参数来代替使用,将具体的东西带入这个参数即可
      #include <iostream>
      using namespace std;
      //函数模板
      tempalte <class T1>;//tempalte <typename T>
      tempalte <typename T2>;//tempalte <class T>
      T1 Max(T1 a,T2 b,T1 c);//流入三个接口
      
      int main(){//存在重定义
          float a = 50.f;double b = 40.2;float c =80.f;
          cout << Max(a,b,c);
          int a = 50;float b = 40.2f;int c =80;
          cout << Max(a,b,c);
          double a = 50.0;int b = 40;double c =80.0;
          cout << Max(a,b,c);
          while(1);
          return 0;
      }
      
      tempalte <class T>
      T1 Max(T1 a,T2 b,T1 c){//定义不用赋值
      	if (a < b) a =b;
      	if ( a< c) a = c;
      	return a;
      }
      

猜你喜欢

转载自blog.csdn.net/weixin_43357695/article/details/105455746