Tutorial#08 - The Basics of C++ Program (Part - 2)

In this section we cover these topics:
1: Declaration and Definition.
2: Assignment statement.
3: Output variation.
4: endl Manipulator.

Declarations and Definitions:
Let’s digress for a moment to note a subtle distinction between the terms definition and declaration as applied to variables.
A declaration introduces a variable’s name (such as var1) into a program and specifies its type (such as int). However, if a declaration also sets aside memory for the variable, it is also called a definition. The statements
int var1;
int var2;
in the INTVARS program are definitions, as well as declarations, because they set aside memory for var1 and var2. We’ll be concerned mostly with declarations that are also definitions, but later on we’ll see various kinds of declarations that are not definitions.
Assignment Statements:
The statements
var1 = 20;
var2 = var1 + 10;
assign values to the two variables. The equal sign (=), as you might guess, causes the value on the right to be assigned to the variable on the left. The = in C++ is equivalent to the := in Pascal or the = in BASIC. In the first line shown here, var1, which previously had no value, is given the value 20.
Output Variations:
The statement
cout << “var1+10 is “;
displays a string constant, as we’ve seen before. The next statement
cout << var2 << endl;
displays the value of the variable var2. As you can see in your console output window, the output of the program is
var1+10 is 30
Note that cout and the << operator know how to treat an integer and a string differently. If we send them a string, they print it as text. If we send them an integer, they print it as a number.
The endl Manipulator:
The last cout statement in the INTVARS program ends with an unfamiliar word: endl. This causes a linefeed to be inserted into the stream, so that subsequent text is displayed on the next line. It has the same effect as sending the ‘\n’ character, but is somewhat clearer.

Source file and text file download link is given bellow:
Click here to download....

Comments