在 C++ 中,程式中的任何變數在使用前一定要先經過宣告,變數的宣告方式如下:
資料型態 變數名稱
例如,宣告一個整數型態的變數,名稱為 mySalary,它的敘述句如下:
int mySalary;
經過宣告後,我們在程式中就可以隨時取用 mySalary 這個變數來做運算了。有些時候,我們也可能需要在宣告變數時就設定一個初值給它,這時候就可以使用 "=" 這個指派運算子,將變數初值賦予變數,例如:
int mySalary = 10000;
變數除了使用前必須先經宣告以外,還有變數可見的範圍,也就等於變數宣告的位置:依照可見度的範圍大小區分有以下四種等級:
- 在整個程式內可見
- 在一個檔案內可見
- 在一個函數中可見
- 在一個區塊中可見
常數的宣告,則必需使用 const 這個關鍵字,例如,宣告一個資料型態為字串的常數,其值為 "Hello World",如下:
const char *MY_GREETING = "Hello World";習慣上,常數的命名大多使用大寫字母加底線組合而成,並非一定得要這麼做,但既然是大家的命名習慣,我們也只得將就遵守比較好。
※命名原則
雖然說變數名稱由我們自取,但還是有一些規定需要遵守,以下是強迫性的規定:
- 第一個字母必須是英文字或是"_"底線符號
- 其它字母必須是英文字、數字或"_"底線符號
- 不可以使用 C++ 關鍵字 ( 保留字 )
- 大小寫是有區別的
※關鍵字
下表為 C++ 關鍵字,這些關鍵字都不可以被用來當做常數或變數
| Keyword | Description |
|---|---|
| asm | insert an assembly instruction |
| auto | declare a local variable |
| bool | declare a boolean variable |
| break | break out of a loop |
| case | a block of code in a switch statement |
| catch | handles exceptions from throw |
| char | declare a character variable |
| class | declare a class |
| const | declare immutable data or functions that do not change data |
| const_cast | cast from const variables |
| continue | bypass iterations of a loop |
| default | default handler in a case statement |
| delete | make dynamic memory available |
| do | looping construct |
| double | declare a double precision floating-point variable |
| dynamic_cast | perform runtime casts |
| else | alternate case for an if statement |
| enum | create enumeration types |
| explicit | only use constructors when they exactly match |
| export | allows template definitions to be separated from their declarations |
| extern | tell the compiler about variables defined elsewhere |
| false | a constant representing the boolean false value |
| float | declare a floating-point variable |
| for | looping construct |
| friend | grant non-member function access to private data |
| goto | jump to a different part of the program |
| if | execute code based on the result of a test |
| inline | optimize calls to short functions |
| int | declare an integer variable |
| long | declare a long integer variable |
| mutable | override a const variable |
| namespace | partition the global namespace by defining a scope |
| new | allocate dynamic memory for a new variable |
| operator | create overloaded operator functions |
| private | declare private members of a class |
| protected | declare protected members of a class |
| public | declare public members of a class |
| register | request that a variable be optimized for speed |
| reinterpret_cast | change the type of a variable |
| return | return from a function |
| short | declare a short integer variable |
| signed | modify variable type declarations |
| sizeof | return the size of a variable or type |
| static | create permanent storage for a variable |
| static_cast | perform a nonpolymorphic cast |
| struct | define a new structure |
| switch | execute code based on different possible values for a variable |
| template | create generic functions |
| this | a pointer to the current object |
| throw | throws an exception |
| true | a constant representing the boolean true value |
| try | execute code that can throw an exception |
| typedef | create a new type name from an existing type |
| typeid | describes an object |
| typename | declare a class or undefined type |
| union | a structure that assigns multiple variables to the same memory location |
| unsigned | declare an unsigned integer variable |
| using | import complete or partial namespaces into the current scope |
| virtual | create a function that can be overridden by a derived class |
| void | declare functions or data with no associated data type |
| volatile | warn the compiler about variables that can be modified unexpectedly |
| wchar_t | declare a wide-character variable |
| while | looping construct |
沒有留言:
張貼留言