C++基礎1:從C過渡到C++

C plus plus -- C語言的超集
C++可以完全引用C

案例:輸出Hello World

  • 源代碼:HelloWorld.cpp
// 第一個C++程序
#include <iostream>
using namespace std;
int main(){
    cout<< "Hello world" <<endl; 
}
  • 編譯:g++ HelloWorld.cpp -o HelloWorld
  • 執行:./HelloWorld
  • 結果:Hello world

麻雀雖小,五臟俱全。

HelloWorld.cpp,看C++與C的基本區別:

  1. 單行注釋// (C99開始支持單行注釋)
  2. 文件后綴名.cpp
  3. 頭文件#include <iostream>
  4. 命名空間 using namespace std;
  5. 標準輸出流cout、輸出運算符<<、換行控制器endl
  6. 編譯工具g++

1. 源文件后綴

  • C/C++頭文件后綴名區別
C C++
*.h *.h *.hpp
  • C/C++源文件后綴名區別
C C++
*.c *.cpp *.cc *.cxx
  • 不同編譯器C++源文件后綴名區別
平臺 可用后綴名
Unix *.C, *.cc, *.cxx, *.c
GNU C++ *.C, *.cc, *.cxx, *.cpp, *.c++
Borland C++ *.cpp
Microsoft Visual C++ *.cpp, *.cxx, *.cc

2.引用頭文件

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>

有些C++編譯器同時支持以上兩種頭文件,但有些不。請使用C++標準方式


3. 函數重載

實驗: 以下C與C++的編譯執行結果

  • printf.c
#include <stdio.h>
void printf(){
    printf("Hello world");
}
int main(){
    printf();
}
  • printf.cpp
#include <cstdio>
void printf(){
    printf("Hello world");
}
int main(){
    printf();
}

函數重載:函數名相同只有參數(個數或者類型)不相同。

C C++
不支持重載 支持重載

4. 命名空間

實驗: 以下C的編譯結果

  • scope.c
#include <stdio.h>
void test(){
    printf("this is test\n");
}
void test(){
    printf("this is another test\n");
}
int main(){
    test();
}

命名空間

C C++
不支持命名空間 支持命名空間
  • 命名空間的作用:避免全局變量、函數、類的命名沖突(因為名字相同而編譯失?。?。

  • 定義命名空間

namespace 空間名 {
    // 定義類/函數
}
  • 引用命名空間
    • using指令(using directive)
      using namespace 空間名;
      
      例如使用標準命名空間std
      using namespace std;
      
    • using聲明(using declaration)
      using 空間名::標識符;
      
      例如使用標準命名空間stdcout
      using std::cout;
      

C++命名空間處理方式

#include <cstdio>
namespace scope1 {
    void test(){
        printf("this is test\n");
    }
}
namespace scope2 {
    void test(){
        printf("this is another test\n");
    }
}
int main(){
    scope1::test();
    scope2::test();
}

全局命名空間

  • 默認的命名空間,所有名字都在全局命名空間中。
  • 使用方式:直接忽略或者只寫::
    例如:定義全局函數void test();,默認就是在全局命名空間中,調用方式test()或者::test()。

在C++中,不帶.h后綴的頭文件所包含和定義的標識符在std空間中;
.h后綴的頭文件所包含和定義的標識符在全局命名空間中,不需要聲明使用std空間


5. 類型

  • 新增基本類型bool--true/false

在C99中stdbool.h中增加三個宏定義booltruefalse。在C++中是內置類型和常量。
如何驗證C的bool是宏定義,C++的bool不是宏定義?

  • password.c
#include <stdio.h>
int main(){
      printf("input user name:");
      char name[BUFSIZ];
      scanf("%s",name);
      printf("input 3 number password:");
      int password1;
      scanf("%d",&password1);
      printf("input 3 number password again:");
      int password2;
      scanf("%d",&password2);
      printf("password check:%d\n", password1 == password2);
}
  • password.cpp
#include <iostream>
#include <cstdio>
using std::cout;
using std::cin;
using std::endl;
int main(){
      cout << "input user name:";
      char name[BUFSIZ];
      cin >> name;
      cout << "input 3 number password:";
      int password1;
      cin >> password1;
      cout << "input 3 number password again:";
      int password2;
      cin >> password2;
      cout << "password check:" << (password1 == password2) << endl;
}
  • 新增自定義類型class
    詳細信息參見:類與對象章節

6. 思想

C C++
面向過程 面向對象/基于對象

何為面向過程?何為面向對象?

  • 面向過程:強調如何處理(如何解決)
  • 面向對象:強調執行處理的對象(找誰解決)

面向過程與面向對象:廚師與老板

思維區別

  • 將問題按照過程方式來解決?
  • 將問題抽象為一個對象來解決?

7. 動態內存

  • 基本類型的動態內存
  • dynamic_mem.c
#include <stdio.h>
#include <stdlib.h>
int main(){
      int* num = malloc(sizeof(int));
      *num = 100;
      printf("%d\n",*num);
      free(num); 
}
  • dynamic_mem.cpp
#include <iostream>
int main(){
      int* num = new int;
      *num = 100;
      std::cout << *num << std::endl;
      delete num; 
}

動態內存區別

C C++
malloc()/free() new/delete

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

問題:

  • malloc()申請內存,是否可以使用delete銷毀內存?
  • new申請內存,是否可以使用free()銷毀內存?

8. 初始化

C++特殊初始化方法

int n=10;
int m(10);

擴展閱讀

練習

  • 完成上面擴展閱讀中的習題和練習。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容