| Previous | Table of Contents | Next |
You can create more than one variable of the same type in one statement by writing the type and then the variable names, separated by commas. For example:
unsigned int myAge, myWeight; // two unsigned int variables long area, width, length; // three longs
As you can see, myAge and myWeight are each declared as unsigned integer variables. The second line declares three individual long variables named area, width, and length. The type (long) is assigned to all the variables, so you cannot mix types in one definition statement.
You assign a value to a variable by using the assignment operator (=). Thus, you would assign 5 to Width by writing
unsigned short Width; Width = 5;
You can combine these steps and initialize Width when you define it by writing
unsigned short Width = 5;
Initialization looks very much like assignment, and with integer variables, the difference is minor. Later, when constants are covered, you will see that some values must be initialized because they cannot be assigned to.
Listing 3.2 shows a complete program, ready to compile, that computes the area of a rectangle and writes the answer to the screen.
Listing 3.2 Demonstrates the Use of Variables
1: // Demonstration of variables
2: #include <iostream.h>
3:
4: int main()
5: {
6: unsigned short int Width = 5, Length;
7: Length = 10;
8:
9: // create an unsigned short and initialize with result
⇒// of multiplying Width by Length
10: unsigned short int Area = Width * Length;
11:
12: cout << Width: << Width << \n;
13: cout << Length: << Length << endl;
14: cout << Area: << Area << endl;
15: return 0;
16: }
![]() | Width: 5 Length: 10 Area: 50 |
![]() | Line 2 includes the required include statement for the iostreams library so that cout will work. Line 4 begins the program. |
On line 6, Width is defined as an integer, and its value is initialized to 5. Another integer, Length, is also defined, but its not initialized. On line 7 the value 10 is assigned to Length.
On line 10 an integer, Area, is defined, and it is initialized with the value obtained by multiplying Width times Length. On lines 12 through 15 the values of the variables are printed to the screen. Note that the special word endl creates a new line.
![]() |
|
![]() | It can become tedious, repetitious, and, most important, error-prone to keep writing unsigned short int. C++ enables you to create an alias for this phrase by using the keyword typedef, which stands for type definition. |
In effect, you are creating a synonym, and it is important to distinguish this from creating a new type (which you will do on Day 6, Basic Classes). typedef is used by writing the keyword typedef followed by the existing type and then the new name. For example,
typedef unsigned short int USHORT
creates the new name USHORT that you can use anywhere you might have written unsigned short int. Listing 3.3 is a replay of Listing 3.2 using the type definition USHORT rather than unsigned short int.
Listing 3.3 Demonstrates typedef
1: // *****************
2: // Demonstrates typedef keyword
3: #include <iostream.h>
4:
5: typedef unsigned short int USHORT; //typedef defined
6:
7: int main()
8: {
9: USHORT Width = 5;
10: USHORT Length;
11: Length = 10;
12: USHORT Area = Width * Length;
13: cout << Width: << Width << \n;
14: cout << Length: << Length << endl;
15: cout << Area: << Area <<endl;
16: return 0;
17: }
![]() | Width: 5 Length: 10 Area: 50 |
![]() |
|
On line 5, USHORT is typedefd as a synonym for unsigned short int. The program is otherwise identical to Listing 3.2, and the output is the same.
One source of confusion for new C++ programmers is when to declare a variable to be type long and when to declare it to be type short. The rule, when understood, is fairly straightforward: If there is any chance that the value youll want to put into your variable will be too big for its type, use a larger type.
| Previous | Table of Contents | Next |