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

文章目录

一、C与C++的区别

1.1 C是面向过程的

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

1.2 C++是面向对象的

C++是面向对象的:把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的属性,行为。

【C++】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;
}

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

#include

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

#include

<<是一个运算符重载函数 operatoer<<(形参列表)< code><br><code>cout <<</code>&#x7B49;&#x4EF7;&#x4E8E;<code>cout.operator<<(实参)< code>&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;<strong>C&#x5E93;&#x51FD;&#x6570;printf</strong><br><strong>endl</strong>&#x76F8;&#x5F53;&#x4E8E;C&#x8BED;&#x8A00;&#x7684;<strong>&#x6362;&#x884C;&#x7B26;</strong>&#xFF1B;<!--(实参)<--></code><!--(形参列表)<-->

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;
}

结果展示:

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

三、命名空间

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

namespace yemaoxu
{

    int a=10;

}

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

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;
}

代码结果展示:

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

3.3 using关键字

  1. 导入具体的标识符:
    使用using + 名字空间名 + 具体的哪一个变量或函数或类 的名字标识符。
  2. 导入名字空间中的所有标符识 :
    using + namespece + 哪个名字空间
#include

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;
}

代码结果展示:

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

四、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.pfunc();
    return 0;
}

代码结果展示:

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

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();

    return 0;
}

代码结果展示:

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

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;
}

代码结果展示:

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

五、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;
}

代码结果展示:

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

Original: https://blog.csdn.net/m0_65835264/article/details/126472469
Author: 夜猫徐
Title: 【C++】C向C++的知识过度(上)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/542961/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球