2009年8月12日 星期三

變數常數和關鍵字

※變數
在 C++ 中,程式中的任何變數在使用前一定要先經過宣告,變數的宣告方式如下:

資料型態 變數名稱

例如,宣告一個整數型態的變數,名稱為 mySalary,它的敘述句如下:
int mySalary;
經過宣告後,我們在程式中就可以隨時取用 mySalary 這個變數來做運算了。有些時候,我們也可能需要在宣告變數時就設定一個初值給它,這時候就可以使用 "=" 這個指派運算子,將變數初值賦予變數,例如:
int mySalary = 10000;
變數除了使用前必須先經宣告以外,還有變數可見的範圍,也就等於變數宣告的位置:

依照可見度的範圍大小區分有以下四種等級:
  1. 在整個程式內可見
  2. 在一個檔案內可見
  3. 在一個函數中可見
  4. 在一個區塊中可見
※常數
常數的宣告,則必需使用 const 這個關鍵字,例如,宣告一個資料型態為字串的常數,其值為 "Hello World",如下:
const char *MY_GREETING = "Hello World";
習慣上,常數的命名大多使用大寫字母加底線組合而成,並非一定得要這麼做,但既然是大家的命名習慣,我們也只得將就遵守比較好。

※命名原則
雖然說變數名稱由我們自取,但還是有一些規定需要遵守,以下是強迫性的規定:
  1. 第一個字母必須是英文字或是"_"底線符號
  2. 其它字母必須是英文字、數字或"_"底線符號
  3. 不可以使用 C++ 關鍵字 ( 保留字 )
  4. 大小寫是有區別的
另外在便於程式可讀性方面,則有所謂的匈牙利命名法,這部份並非強制規定使用,大家也可以參考看看維基百科關於匈牙利命名法的解釋。

※關鍵字
下表為 C++ 關鍵字,這些關鍵字都不可以被用來當做常數或變數
KeywordDescription
asminsert an assembly instruction
autodeclare a local variable
booldeclare a boolean variable
breakbreak out of a loop
casea block of code in a switch statement
catchhandles exceptions from throw
chardeclare a character variable
classdeclare a class
constdeclare immutable data or functions that do not change data
const_castcast from const variables
continuebypass iterations of a loop
defaultdefault handler in a case statement
deletemake dynamic memory available
dolooping construct
doubledeclare a double precision floating-point variable
dynamic_castperform runtime casts
elsealternate case for an if statement
enumcreate enumeration types
explicitonly use constructors when they exactly match
exportallows template definitions to be separated from their declarations
externtell the compiler about variables defined elsewhere
falsea constant representing the boolean false value
floatdeclare a floating-point variable
forlooping construct
friendgrant non-member function access to private data
gotojump to a different part of the program
ifexecute code based on the result of a test
inlineoptimize calls to short functions
intdeclare an integer variable
longdeclare a long integer variable
mutableoverride a const variable
namespacepartition the global namespace by defining a scope
newallocate dynamic memory for a new variable
operatorcreate overloaded operator functions
privatedeclare private members of a class
protecteddeclare protected members of a class
publicdeclare public members of a class
registerrequest that a variable be optimized for speed
reinterpret_castchange the type of a variable
returnreturn from a function
shortdeclare a short integer variable
signedmodify variable type declarations
sizeofreturn the size of a variable or type
staticcreate permanent storage for a variable
static_castperform a nonpolymorphic cast
structdefine a new structure
switchexecute code based on different possible values for a variable
templatecreate generic functions
thisa pointer to the current object
throwthrows an exception
truea constant representing the boolean true value
tryexecute code that can throw an exception
typedefcreate a new type name from an existing type
typeiddescribes an object
typenamedeclare a class or undefined type
uniona structure that assigns multiple variables to the same memory location
unsigneddeclare an unsigned integer variable
usingimport complete or partial namespaces into the current scope
virtualcreate a function that can be overridden by a derived class
voiddeclare functions or data with no associated data type
volatilewarn the compiler about variables that can be modified unexpectedly
wchar_tdeclare a wide-character variable
whilelooping construct

沒有留言:

張貼留言