Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


declares a variable of type int and is given the name inchesInFoot. You must supply a name for every variable in C++. Notice that the modifier const is applied to the type name. This modifier says that this variable is a constant; the variable’s value cannot be changed.

This source line also introduces the assignment operator =. The assignment operator is used to place the value on the right-hand side of the operator to the variable on the left-hand side. In this example, the value 12 is placed in the variable named inchesInFoot. The constant (variable) inchesInFoot will hold this value throughout the life of the program (its value cannot be changed). The next line of code is the following:

int numberOfFeet = 0 ;

This int, named numberOfFeet is declared and initialized to a value of 0. The purpose of this variable is to hold the number of feet that the user specifies at runtime. Note that the const modifier is not applied to this variable definition because numberOfFeet’s contents will need to be changed. The next two lines

cout << “Please enter the number of feet, “ ;
cout << “I will tell you the number of inches” << endl ;

are used to output a message (an instruction) to the user. Notice that although the two statements are independent and exist on two separate lines, the message is actually displayed to the user as a single line. The endl manipulator performs two operations. First, it flushes the output buffer and outputs a newline character. Because output is to the screen, the cursor will be placed on the next line, flush with the left margin. Because the first cout statement does not end with an endl, the second message is appended to the first. The next line of code

cin >> numberOfFeet ;

awaits input from the user. After the user enters a value and presses the Enter key, the cin object will take the value and place it in the variable named numberOfFeet. The cin statement actually performs a conversion behind the scenes. You press the keyboard character 2; cin takes this character and converts it to a C++ integer type and places the value in the variable. This brings you to the next line of code:

int numberOfInches = numberOfFeet * inchesInFoot ;

This statement introduces the multiplication operator *. This operator is one of many defined by the C++ language. This source line is referred to as a compound statement. First, the int variable numberOfInches is declared for use. Next, the value stored in numberOfFeet is multiplied by the value stored in inchesInFoot. The result of this expression is then assigned to the variable numberOfInches. The next two source lines

cout << numberOfInches << “ inches are in “ ;
cout << numberOfFeet << “ feet” << endl ;

display the result of the conversion to the standard output. Notice that you can stream different data types to the cout object. First, the value stored in numberOfInches is sent to cout, followed by a string. In the next statement, numberOfFeet is sent to cout, followed by another string, and finally the endl manipulator.

The last program statement, the return statement, returns the value 0 returns to the operating system.

Comments

The C++ language defines a number of data types: bool, char, int, long, float, and double. A bool is used to represent a Boolean value, either true or false. The char type is used to contain characters, such as 3, #, or m. The types int and long are integral types and can hold whole numbers only. The types float and double are used for storing floating-point numbers.

In C++, strings are implemented as an array of char. In fact, any data type can be declared as an array. You will look at arrays in a How-To later in this chapter.

The const keyword is used to qualify a declaration. You must remember to initialize the const variable at the point of declaration; if you don’t, you will not be able to assign to it. For example, the compiler will not allow the following:

const int inchesInFoot ;
inchesInFoot = 12 ; // error right here!

The const variable is used as a symbolic constant (or alias) for a value. Using a symbolic name provides a descriptive name for some value. An additional benefit of using const is that the value stored in the variable is protected from modification. It also helps with code maintenance. Why? Let’s look at a brief example. The following example defines a char that is constant. Assume that the char is used as a choice within a menu system.

const char REPLACE_OPTION = ‘S’ ; // replace
//...
if( option == REPLACE_OPTION )
//...

Using a constant allows you to change the value that the name symbolizes in just one place without having to change all occurrences of that value. In other words, you would have to search for all occurrences of the letter S and change them to the new value. Instead, by using a constant, you only have to change the initialization value of the constant declaration. The following line of code shows a decision to change the value of REPLACE_OPTION to R:

const char REPLACE_OPTION = ‘R’ ;

The change is only required at the declaration. The next step would be to recompile all the code associated with this change. You do not have to worry about searching for every occurrence of R because the compiler will do all the work required.

The cin object is used to accept data from the standard input; the default is the keyboard. The extraction operator >> directs the input from the cin object to the named variable(s). In the previous example, numberOfFeet is the recipient of data from cin.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.