跳轉到內容

軟體工程師手冊/語言詞典/C++

來自華夏公益教科書,開放的世界,開放的書籍

C++ 是一種源自 C多正規化 程式語言。

C++ 是一種完整的 多正規化 程式語言,它實現了以下正規化:泛型 (模板 超程式設計)、命令式面向物件 (基於類)。

執行入口點

[編輯 | 編輯原始碼]

可執行檔案從 main() 方法開始。

一般語法

[編輯 | 編輯原始碼]

典型的語句以分號結尾。要將 b 賦值給 a,請使用

 a = b;
 // this is an inline comment.  Everything after the // is a comment.

塊註釋以 /* 開始,以 */ 結束。它們可以跨越多行。

 /*
  * this is a block comment 
  */

變數宣告

[編輯 | 編輯原始碼]

宣告可以出現在實現中的任何位置。

將 i 宣告為整數

 int i;

以下是兩種將 i 宣告為整數併為其賦予初始值 0 的方法

 int i = 0;
 int i(0);

方法宣告/實現

[編輯 | 編輯原始碼]

方法透過指定方法的介面來宣告。它通常在類的範圍內宣告。

 return_type function_name(argument_1_type arg_1_name, 
                           argument_2_type arg_2_name, 
                           default_argument_type default_arg_name = default_arg_value);

方法實現重複了大部分相同的資訊

 return_type class_name::function_name(argument_1_type arg_1_name, 
                           argument_2_type arg_2_name, 
                           default_argument_type default_arg_name)
 {
     // work with arg_1_name, arg_2_name, and default_arg_name
     // depending on the argument types the variables are passed by 
     //   value, reference, or are constant
     // don't forget to return something of the return type
     return 36;
 }

重要提示 C/C++ 初學者可能會對函式原型中如何宣告指標與如何定義函式之間的區別感到困惑。例如,如果 my_function 不返回任何內容,但接受一個浮點輸入,則其函式原型如下所示

 void my_function(float *some_variable);

但是,如果變數 my_var 被定義為指向浮點數的指標,例如

 float *my_var;

然後呼叫 my_function 的方式如下

 my_function(my_var);

初學者有時會對在原型語句中使用 "*" 來告訴編譯器一個變數是指標,但在接受指標作為輸入的函式呼叫過程中語法上不使用 "*"感到困惑。

作用域

[編輯 | 編輯原始碼]

作用域由大括號定義。

 { // this the beginning of a scope
     // the scope is about to end
 }

條件語句

[編輯 | 編輯原始碼]

當且僅當 A 等於 B 時,將 C 賦值給 D,否則將 E 賦值給 F。

 if( A == B )
 {
     D = C;
     // more code can be added here.  It is used if and only if A is equal to B
 }
 else
 {
     F = E;
     // more code can be added here.  It is used if and only if A is not equal to B
 }

或者

 if( A == B ) 
     D = C; //more lines of code are not permitted after this statement
 else
     F = E;

或者,可以使用 switch 語句進行多項選擇操作。此示例將數字輸入轉換為文字。

 switch( number_value )
 {
     case 37:
         text = "thirty-seven";
         break; // this line prevents the program from writing over this value with the
                //   following code
     case 23:
         text = "twenty-three";
         break;
     default: // this is used if none of the previous cases contain the value
         text = "unknown number";
 }

迴圈語句

[編輯 | 編輯原始碼]

此程式碼從 0 到 9 計數,將陣列的內容加起來。

 int i = 0;
 for( int index = 0; index < 10; index = index + 1 )
 {
     i = array[index];
 }

此程式碼重複執行,直到找到數字 4 為止。如果它執行到陣列的末尾,可能會出現問題。

 int index = 0;
 while( 4 != array[index] )
 {
     index = index + 1;
 }

此程式碼在檢查之前遞增計數器,因此它從元素 1 開始。

 int index = 0;
 do
 {
     index = index + 1;
 }
 while( 4 != array[index] );

輸出語句

[編輯 | 編輯原始碼]
 printf( "%s","Hello world!\n" );

或者

 std::cout << "Hello world!" << std::endl;

標準模板庫包含各種容器,包括向量、位集、列表、對映和多重集。

演算法

[編輯 | 編輯原始碼]

標準模板庫包含各種演算法,包括排序、刪除和查詢。

垃圾回收

[編輯 | 編輯原始碼]

垃圾回收是手動的。

物理結構

[編輯 | 編輯原始碼]

通常,介面定義在標頭檔案中,通常為 *.h。實現檔案通常命名為 *.cpp。有用的類集合可以編譯成庫,通常為 *.dll、*.a 或 *.so,它們可以編譯成可執行檔案(靜態連結)或在執行時使用(動態連結)。

不要混淆這兩者

 =  // assignment
 == // comparison, is equal to

通常,使用你不想使用的那個會編譯,並會產生你沒有預期的結果。

一個好的實踐是編寫; if(CONSTANT == variable) 而不是 if(variable == CONSTANT),因為編譯器會捕捉; if(CONSTANT = variable) 但不會捕捉 if(variable = CONSTANT)。

陣列從索引 0 開始。

網路參考資料

[編輯 | 編輯原始碼]

書籍和文章

[編輯 | 編輯原始碼]
  • Bruce Eckel,Thinking in C++,卷 1:標準 C++ 簡介 ISBN 0139798099。它幫助許多人從 C 語言過渡到 C++ 語言。它可以在 Bruce Eckel 的網站 上免費獲取。
  • Nicolai M. Josuttis,The C++ Standard Library : A Tutorial and Reference ISBN 0201379260。它包含關於標準模板庫容器和演算法的冗長而深入的討論。
  • Stanley B. Lippman、Josée Lajoie & Barbara E. Moo,C++ Primer ISBN 0321714113。這是一本非常全面的 C++ 入門書,它用了近 1000 頁來介紹 C++ 程式設計。
  • Andrew Koenig & Barbara E. Moo,Accelerated C++ ISBN 020170353X。這是另一本 C++ 入門書,但它強調使用 STL。它是一本相當緊湊的入門書,學習曲線較陡,更適合那些已經熟悉其他程式語言的人。
  • Scott Meyers,Effective C++ ISBN 0321334876More Effective C++ ISBN 020163371XEffective STL ISBN 0201749629。Meyers 的系列書籍包含 C++ 最佳實踐,從類設計到最小化編譯依賴關係。
  • Herbert Schildt,C++: The Complete Reference ISBN 0072226803。它包含程式碼語法和用法的字典式列表。它的結構不適合從頭開始學習這門語言。然而,它在從類似語言切換時非常有用。
  • Bjarne Stroustrup,The C++ Programming Language ISBN 0201889544。由 C++ 的創造者編寫,它涵蓋了從核心語言到標準庫的一切內容。本書本身超過 1300 頁。
華夏公益教科書