• 【C++】C向C++的知识过度(上)


    一、C与C++的区别

    1.1 C是面向过程的

    C语言是面向过程的:就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了。但是随着程序大型化之后,C也暴露了不足之处。
    C语言的缺点:程序大型化所带来的分解的难度的提高,当程序过大时,就会出现难以分解,以及分解所带来的代码的严重冗余,及后期维护成本的提高。

    1.2 C++是面向对象的

    C++是面向对象的:把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的属性,行为。
    在这里插入图片描述
    面向对象有三大特性:封装性,继承性和多态性

    总之,面向对象就是高度实物抽象化、面向过程就是自顶向下的编程。

    1.3 编译器的区别

    编译器不同:Linux中 C++程序使用g++编译器,C程序使用gcc编译器。使用g++也可以编译c程序。但是gcc是无法编译C++程序的。

    二、C与C++默认代码的不同

    以hello world!为例

    #include 
    
    using namespace std;
    
    int main()
    {
        cout << "Hello World!" << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    与C语言相比,C++标准库的头文件是都不带.h的

    #include 
    
    • 1

    C++中在使用C表准库时,头文件前要加c

    #include 
    
    • 1

    <<是一个运算符重载函数 operatoer<<(形参列表)
    cout <<等价于cout.operator<<(实参),相当于C库函数printf
    endl相当于C语言的换行符

    C库的输入函数:scanf
    在C++中的输入函数 cin >> 相当于scanf

    在C++中表示字符串有了专门类型:string类型
    在C中字符串是没有类型,只有表现形式:字符指针,字符数组但是会有容量和尾部\0的问题。

    #include 
    
    using namespace std;
    
    int main()
    {
        cout << "Hello World!" << endl;
        string buff;
        cin >> buff;
        cout << buff <<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果展示:
    在这里插入图片描述

    三、命名空间

    3.1 关键字namespace去定义自己的名字空间。

    namespace yemaoxu//名字空间标识符
    {
        //在自定义的命名空间中定义自己的变量或函数或类。
        int a=10;
        //...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    所有的名字空间就是在全局空间中定义的,相当于是在全局空间中又自定义一个不同名字的全局作用域。

    3.2 访问不同命名空间中的变量或函数

    使用::域名访问符来访问指定空间中的变量或函数名或类。

    访问命名空间中的变量的方式: 名字空间名 + :: 域名访问符 指定访问某个名字空间中的变量或函数。

    对于嵌套的名字空间,使用::逐级访问。

    #include 
    
    using namespace std;
    
    namespace yemaoxu//名字空间标识符
    {
        //在自定义的命名空间中定义自己的变量或函数或类。
        int a=10;
        void my_func()
        {
            cout << "我是夜猫徐" << endl;
        }
    }
    
    namespace study
    {
        int a=100;
        void my_func()
        {
            cout << "学习C++" << endl;
        }
    }
    //名字空间嵌套
    namespace A
    {
        namespace B
        {
            int a=1000;
            void my_func()
            {
                cout << "请关注我吧!" << endl;
            }
        }
    }
    int main()
    {
        cout << yemaoxu::a << endl;
        cout << study::a << endl;
        yemaoxu::my_func();
        study::my_func();
    
        cout << A::B::a << endl;
        A::B::my_func();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    代码结果展示:
    在这里插入图片描述

    3.3 using关键字

    1. 导入具体的标识符:
      使用using + 名字空间名 + 具体的哪一个变量或函数或类 的名字标识符。
    2. 导入名字空间中的所有标符识 :
      using + namespece + 哪个名字空间
    #include 
    
    //using namespace std;
    using std :: cout;
    using std :: endl;
    
    namespace yemaoxu//名字空间标识符
    {
        //在自定义的命名空间中定义自己的变量或函数或类。
        int a=10;
        void my_func()
        {
            cout << "我是夜猫徐" << endl;
        }
    }
    
    namespace study
    {
        int a=100;
        void my_func()
        {
            cout << "学习C++" << endl;
        }
    }
    //名字空间嵌套
    namespace A
    {
        namespace B
        {
            int a=1000;
            void my_func()
            {
                cout << "请关注我吧!" << endl;
            }
        }
    }
    
    using namespace A::B;
    
    int main()
    {
        using yemaoxu::a;//就近原则
        cout << a << endl;
        my_func();
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    代码结果展示:
    在这里插入图片描述

    四、C++中的struct结构体与字符串

    4.1 C++中的结构体与C中结构体的区别

    C中使用struct结构体:

    是不可以直接在结构体内部定义函数的。只能通函数指针的方式间接调用一个全局函数的形式来表示。

    #include 
    
    typedef struct
    {
        char name[32];
        int age;
        void (*pfunc)();
    }stu_t;
    void my_func()
    {
        printf("学习C++\n");
    }
    int main()
    {
        stu_t stu={"yemaoxu",20,my_func};
        printf("%s \n",stu.name);
        printf("%d \n", stu.age);
        //通过函数指针的回调调用一个全局函数来表示stu这个结构体变量的行为。
        stu.pfunc();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    代码结果展示:
    在这里插入图片描述

    C++中结构体:

    多了封装性,只不过默认采用的公有的访问权限public
    C++中的结构体的大小与非静态属性有关,同时遵从C中的结构体对齐原则,与结构体中成员函数无关。因为C++中的类就是从C中的结构体衍生而来。

    把这种定义在结构内部的函数称之为成员函数。

    #include 
    
    using namespace std;
    struct stu
    {
    public://共有访问权限
        char name[32];
        int age;
        void my_func()
        {
            printf("学习C++\n");
        }
    private://私有访问权限    
        void you_func()
        {
            printf("学习C++\n");
        }
    };
    
    int main()
    {
        stu a={"yemaoxu",20};
        cout << a.name << endl;
        cout << a.age << endl;
        a.my_func();
        //a.you_func();私有访问权限会报错
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    代码结果展示:
    在这里插入图片描述

    4.2 认识C++中string类:

    C中是没有字符串的类型,只有字符串的表示形式:1.字符指针 2.字符数组。
    在C使用字符串要注意:字符长度及尾\0的问题。

    在C++的面向对象这个世界中,对于底层数据元素操作,我们把这个底层操作封装了起来,成为了一个专属性字符串类型。String类型。

    C++string 字符串类的API的使用:

    1. at()接口
      查看第几个元素的字符;
    2. size()接口
      查看有多少个字符;
    3. []中括号与at()一样
      在使用string定义的字符串,当访问指定的字符时,我们更推荐使用at,因为他有边界检查;
    4. +号运算符重载
      连接两个字符串;
    5. append()接口
      连接一个字符串
    #include 
    
    using namespace std;
    
    int main()
    {
        string str1;
        str1="yemaoxu";
        string str2;
        str2=" study C++";
        cout << str1 << endl;
        cout << str1.size() <<endl;
        cout << str1.at(2) << endl;
        cout << str1[2] << endl;
        cout << "---------------------------" << endl;
        cout << str1+str2 << endl;
        string str=str1+=str2;
        cout << str << "," << str1 <<endl;
        string str3=str1.append(" very good");
        cout << str3 << "," <<  str1  << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    代码结果展示:
    在这里插入图片描述

    五、C++中bool变量

    在C中,0为假,非0为真,即使是个负数也是为真。

    在C++中我们表示布尔有了专门一个布尔类型,内置类型,使bool来表示。
    真关键字:true
    假关键字:false
    都是unsigned char 类型1字节,true为1,false为0。

    #include 
    using namespace std;
    int main()
    {
        bool ok;
        ok = true;
        cout << ok <<endl;
        ok = false;
        cout << ok << endl;
        cout << sizeof (bool) << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    代码结果展示:
    在这里插入图片描述

  • 相关阅读:
    Android 阿里云OSS 上传文件,使外网可以直接访问
    Ubuntu18 Opencv3.4.12 viz 3D显示安装、编译、移植
    java详解队列
    【计算机网络】计算机网络复习总结 ----- 计算机网络概述
    关于electron打包卡在winCodeSign下载问题
    NoSQL之 Redis配置与优化
    ios原生分享
    RK3568开发笔记(一):瑞芯微RK3568芯片介绍,入手开发板的核心板介绍
    PHP中流的理解
    使用Fiddler进行移动端抓包和模拟弱网络测试
  • 原文地址:https://blog.csdn.net/m0_65835264/article/details/126472469