軟體工程師手冊/語言詞典/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;
標準模板庫包含各種容器,包括 vector、bit-set、list、map 和 multi-set。
標準模板庫包含各種演算法,包括 sort、remove_if 和 find_if。
垃圾回收是手動的。
通常,介面定義在標頭檔案中,通常為 *.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 0321334876,More Effective C++ ISBN 020163371X,Effective STL ISBN 0201749629。Meyers 的系列書籍包含了最佳的 C++ 實踐,從類設計到最小化編譯依賴性。
- Herbert Schildt,C++: The Complete Reference ISBN 0072226803。它包含程式碼語法和用法的字典式列表。這種結構不利於從頭開始學習這門語言。然而,它在從類似語言轉換時非常有用。
- Bjarne Stroustrup,The C++ Programming Language ISBN 0201889544。由 C++ 的創造者編寫,涵蓋了從核心語言到標準庫的一切。這本書本身超過 1300 頁。