C++基础:从C过渡到C++,需要注意这几点


C++ 又名 C plus plus ,是C语言的超集,C++可以完全引用C

C : 面向过程——解决问题
C++ :面向对象——找谁解决

用C++编译下显示helloworld

编写文件 helloworld.cpp

#include <iostream>

// using namespace std; 表示后面用到的都是来自标准库

int main(){
        //std 表示来自标准库
        std::cout << "Hello World" << std::endl;    // printf("Hello World\n");
}

编译使用 g++ helloworld.cpp
./a.out 运行

结果为:

Hello World

头文件 #include <iostream> 如同C语言中的 #include <stdio.h>
namespace 是命名空间,std 表示后面的命令使用的是标准库中的命令

1. 引用头文件

C++头文件使用C标准库,在C标准库文件名前加上字母c,并且省略后缀名.h

C头文件C++头文件
#include <stdio.h>#include <iosteam> / #include <cstdio>
#include <stdlib.h>#include <cstdlib>
#include <string.h>#include <cstring>
#include <math.h>#include <cmath>

2. 函数重载

C语言中,函数不能命名为标准库中的命令,但C++编译是可以的

函数重载: 函数名相同但其他参数(个数或者类型)不相同,c++判断这两个函数不同

#include <cstdio>
void printf(){
    printf("Hello world");
}
int main(){
    printf();
}

此程序在C++中是支持的!

3. 命名空间

命名空间: 让相同函数名的函数也能编译,避免全局变量、函数类的命名冲突
类似于文件夹,函数名字相同也不会冲突

#include <cstdio>

using namespace std;

//定义命名空间和命名空间下的函数
namespace English{
        void printf(){
                std::printf("Hello World");
        }
}

namespace Chinese{
        void printf(){
                std::printf("你好世界");
        }
}

using namespace Chinese;
int main(){
        printf();
        English::printf();
}

结果为:

你好世界Hello World

using namespace Chinese; 下面将广泛使用命名空间
English::printf(); 指定使用命名空间

在 C++ 中,不带 .h 后缀的头文件所包含和定义的标识符在 std 空间中;
带 .h 后缀的头文件所包含和定义的标识符在全局命名空间中,不需要声明使用 std 空间

4. 输入输出的区别

cin 从终端里读
cout 写入终端中

#include <iostream>
#include <cstdio>

using namespace std;

int main(){
        int n;
        float f;
        char s[20];
        /*
        scanf("%d%f%s",&n,&f,&s);
        printf("%d\n%f\n%s\n",n,f,s);
        */
        cin >> n >> f >> s;
        cout << n << endl << f << endl << s << endl;
        //c++输入输出不用写类型,它自己知道类型
}

结果为:

10 1.1 abc
10
1.1
abc

c++输入输出不用写类型,它自己知道类型

5. 动态内存

在 C++ 中申请内存使用的是 new
销毁内存使用的是 delete

我们用C和C++的方式分别申请和释放单个内存和整个数组

#include <cstdio>    // printf() 用到
#include <cstdlib>   // malloc() free() 用到
#include <iostream>  // cout 用到

using namespace std;

int main(){

        //申请一个内存
        int* p = (int*)malloc(sizeof(int));
        *p = 100;
        printf("%p %d\n",p,*p);
        free(p);
        p = NULL;

        //c++ 申请内存方式
        int* q = new int;
        *q = 200;
        cout << q << " " << *q << endl;
        delete q;
        q = NULL;

        //申请多个内存
        int* parr = (int*)malloc(sizeof(int)*5);
        for(int i=0;i<5;++i){
                parr[i] = i;
        }
        for(int i=0;i<5;++i){
                printf("%p %d\n",parr+i,parr[i]);
        }
        free(parr);
        parr = NULL;

        //c++ 申请内存方式
        int* qarr = new int[5];
        for(int i=0;i<5;++i){
                qarr[i] = i;
        }
        for(int i=0;i<5;++i){
                cout << qarr+i << " " << qarr[i] << endl;
        }
        delete [] qarr;    // []表示释放的是数组
        qarr = NULL;
}

在 C++ 中用 malloc 是不能判别类型的,需要强制 int* p = (int*)malloc(sizeof(int));

结果为:

0x69feb0 100
0x69feb0 200
0x69feb0 0
0x69feb4 1
0x69feb8 2
0x69febc 3
0x69fec0 4
0x69feb0 0
0x69feb4 1
0x69feb8 2
0x69febc 3
0x69fec0 4

如结果所示,刚释放掉的内存可能被下一个定义的指针申请

C++ 仍然可以使用 malloc() / free() ,但是不建议这么做

6. 初始化

在C++中,括号就能进行变量初始化

#include <iostream>

using namespace std;

int main(){
        int n = 10;     // c初始化内置类型
        int m(20);      // c++初始化变量
        int t{30};      // c++11统一初始化方式
        cout << n << " " << m << " " << t << endl;
}

结果为:

10 20 30
  • 13
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值