Keywords
C++ reserves a set of 63 words for it’s own use. These words are called keywords, and each of these keywords has a special meaning with in the C++ language. The 15 keywords that are starred (*) were added to the language after it’s initial release, consequently, some older reference books or material may omit these.
Here is a list of all the C++ keywords:
You have already run across some of these keywords, including int, void, return, using, and namespace. Along with a set of operators, these keywords define the entire language of C++ (preprocessor commands excluded). Because these keywords have special meaning, your IDEs will change the text color of these words (usually to blue) to make them more visible.
By the time you are done with this tutorial, you will understand what almost all of these words do!
Identifiers, and naming them
The name of a variable, function, class, or other entity in C++ is called an identifier. C++ gives you a lot of flexibility to name identifiers as you wish. However, there are a few rules that must be followed when naming identifiers:
Now that you know how you can name a variable, let’s talk about how you should name a variable.
First, it is a convention that variable (and function) names begin with a lower case letter. If the variable or function name is one word, the whole thing should be written in lower case letters.
If the identifier is a multiword name, there are two common conventions: separated by underscores, or intercapped.
In this tutorial, we will use the intercapped approach because it’s easier to read (it’s easy to mistake an underscore for a space in dense blocks of code).
Second, and this is perhaps the most important rule of all, give your identifiers names that actually describe what they are. It is typical for inexperienced programmers to make variable names as short as possible, either to save on typing or because they figure the meaning is obvious. This is almost always a mistake. Ideally, variables should be named in a way that would help someone who has no idea what your code does be able to figure it out as quickly as possible. In 3 months, when you look at your program again, you’ll have forgotten how it works, and you’ll thank yourself for picking variable names that make sense. The more complex the code the variable is being used in, the better name it needs to have.
* Note: it is okay to use trivial variable names for variables that have a trivial use, such as loop variables. We will address this topic in the section on flow control.
Third, a clarifying comment can go a long way. For example, say we’ve declared a variable named numberOfChars that is supposed to store the number of characters in a piece of text. Does the text “Hello World!” have 10, 11, or 12 characters? It depends on whether we’re including whitespace or punctuation. Rather than naming the variable numberOfCharsIncludingWhitespaceAndPunctuation, which is rather lengthy, a well placed comment on the declaration line should help the user figure it out:
Hungarian notation
One method that is sometimes used in naming variables is called Hungarian notation. In Hungarian notation, variables always begin with a prefix that indicates the variable’s type.
We will talk more about Hungarian notation later in this section once we’ve covered the different numerical data types.
Quiz
Pick which variables are improperly declared and named according to standard conventions (ie. how you should name a variable).
1) int sum;
2) int _apples;
3) int cats=5, dogs=5;
4) int VALUE;
5) int clouds=5, int trees;
6) int my variable name;
7) int length=3; int width=4;
8) int TotalCustomers;
9) int void = 5;
10) int nAngle;
11) int 3some;
12) int meters_of_pipe;
13) int length, width=5;
C++ reserves a set of 63 words for it’s own use. These words are called keywords, and each of these keywords has a special meaning with in the C++ language. The 15 keywords that are starred (*) were added to the language after it’s initial release, consequently, some older reference books or material may omit these.
Here is a list of all the C++ keywords:
asm auto bool * break case catch char class | const const_cast * continue default delete do double dynamic_cast * | else enum explicit * export * extern false * float for | friend goto if inline int long mutable * namespace * | new operator private protected public register reinterpret_cast * return | short signed sizeof static static_cast * struct switch template | this throw true * try typedef typeid * typename * union | unsigned using * virtual void volatile wchar_t * while |
By the time you are done with this tutorial, you will understand what almost all of these words do!
Identifiers, and naming them
The name of a variable, function, class, or other entity in C++ is called an identifier. C++ gives you a lot of flexibility to name identifiers as you wish. However, there are a few rules that must be followed when naming identifiers:
- The identifier can not be a keyword. Keywords are reserved.
- The identifier can only be composed of letters, numbers, and the underscore character. That means the name can not contains symbols (except the underscore) nor whitespace.
- The identifier must begin with a letter or an underscore. It can not start with a number.
- C++ distinguishes between lower and upper case letters.
nvalue
is different thannValue
is different thanNVALUE
.
Now that you know how you can name a variable, let’s talk about how you should name a variable.
First, it is a convention that variable (and function) names begin with a lower case letter. If the variable or function name is one word, the whole thing should be written in lower case letters.
1 | int value; // correct |
2 |
3 | int Value; // incorrect (should start with lower case letter) |
4 | int VALUE; // incorrect (should start with lower case letter) |
5 | int VaLuE; // incorrect (see your psychiatrist) ;) |
1 | int my_variable_name; // correct (separated by underscores) |
2 | int myVariableName; // correct (intercapped) |
3 |
4 | int my variable name; // incorrect (spaces not allowed) |
5 | int MyVariableName; // incorrect (should start with lower case letter) |
Second, and this is perhaps the most important rule of all, give your identifiers names that actually describe what they are. It is typical for inexperienced programmers to make variable names as short as possible, either to save on typing or because they figure the meaning is obvious. This is almost always a mistake. Ideally, variables should be named in a way that would help someone who has no idea what your code does be able to figure it out as quickly as possible. In 3 months, when you look at your program again, you’ll have forgotten how it works, and you’ll thank yourself for picking variable names that make sense. The more complex the code the variable is being used in, the better name it needs to have.
int ccount | Bad | Nobody knows what a ccount is |
int customerCount | Good | Clear what we’re counting |
int i | Bad | What does I stand for?* |
int index | Good | This variable is indexing something |
int _count | Bad | Do not start variable names with underscore |
int count | Either | Okay only if obvious what we’re counting |
int data | Bad | What kind of data? |
int value1, value2 | Either | Can be hard to differentiate between the two |
int numberOfApples | Good | Descriptive |
int totalScore | Good | Descriptive |
int monstersKilled | Good | Descriptive |
int x, y | Either | Okay only in trivial mathematical functions |
Third, a clarifying comment can go a long way. For example, say we’ve declared a variable named numberOfChars that is supposed to store the number of characters in a piece of text. Does the text “Hello World!” have 10, 11, or 12 characters? It depends on whether we’re including whitespace or punctuation. Rather than naming the variable numberOfCharsIncludingWhitespaceAndPunctuation, which is rather lengthy, a well placed comment on the declaration line should help the user figure it out:
1 | // holds number of chars in a piece of text -- including whitespace and punctuation! |
2 | int numberOfChars; |
One method that is sometimes used in naming variables is called Hungarian notation. In Hungarian notation, variables always begin with a prefix that indicates the variable’s type.
1 | int nValue; // the n before Value represents that this is an integer |
2 | bool bValue; // b means boolean |
3 | char chValue; // ch means char |
4 | double dValue; // d means double |
5 | float fValue; // f means float |
Quiz
Pick which variables are improperly declared and named according to standard conventions (ie. how you should name a variable).
1) int sum;
2) int _apples;
3) int cats=5, dogs=5;
4) int VALUE;
5) int clouds=5, int trees;
6) int my variable name;
7) int length=3; int width=4;
8) int TotalCustomers;
9) int void = 5;
10) int nAngle;
11) int 3some;
12) int meters_of_pipe;
13) int length, width=5;
No comments:
Post a Comment