Introduction to Variables,Objects,And Their Declarations in C++ :: Part-A-6



A variable is a symbol that represents a storage location in the computer’s memory. The information that is stored in that location is called the value of the variable. The most common way that a variable obtains a value is by means of an assignment. His has the syntax

                                Variable = expression ;

The expression is first evaluated, and then its resulting value is assigned to the variable. The equals sing “=” is the assignment operator in C++.


Here is a simple C++ program with an integer variable named n:

          #include  <iostream . h>

           //   a simple example to illustrate assignment :

              main ()


                       int n ;

                       n = 66;

                       cout <<  n  <<  endl ;

                       return  0 ;



The first line between the braces {  } declares  n to be a variable of type int. The statement in the second line assigns the value 66 to  n. The statement on the third line prints the value of  n.

Note the use of the symbolic constant endl. This is a predefined iostream manipulator. Sending this to cout is equivalent to the endline character  ‘ \n ‘ and then “flushing” the output buffer.

In the previews examples, the variable n has the value 66. That value is actually stored in the computer’s memory as a sequence of bits (0s and 1s). The computer interprets that sequence of the bits as an integer because the variable was declared to be an integer.

A declaration of a variable is a statement that gives information about the variable to the C++ compiler. Its syntax is

                type variable;

Where type is the name of some C++ typs. For example, the declaration

                 int  n;

tells the compiler two things: (1) the name of the variable is n, and (2) the variable has type int. Every variable must have a type. Its type tells the compiler how the variable’s values are to be stored and used. We can characterize a type by the set of all possible values that could be assigned to a variable of that type. On some computers, the int type set consists of all the integers in the range form-32,768 to 32,767.

C++ is an object-oriented programming language. Among other things, this means that the language is good at simulating system that consist of interacting objects such as an airport control system. In such a simulation, the objects in the system (airplanes, people, luggage, etc.)are represented by variables in the computer program. So variables are often referred to as objects themselves and are visualized as self-contained entities endowed with certain capabilities. In this context we say that the declaration creates the object. The variable being declared then is the name of the object

The declaration creates the object shown here. Its name is n and its type is int. the shaded box represents that area of memory that has been allocated to the object to store its value. The question marks  indicate that no value has been given to the object yet.

          An assignment is one way that an object’s value can be changed. For example,

                   n= 66;

changes the value of n  to 66.

In C++, a declaration may appear anywhere within the program, as the next example shows.


This example shows that a variable may be declared anywhere in a C++ program:

#include <iostream.h>

//This program illustrates variable declarations:



      Int  x, y1;     //declares the variables x and y1

     x = 77;

    y1 = 88;

   int   y2  =  55;   //declares the variables y2, initializing it to 55

   cout  <<  x  <<  “,  “  <<  “,  “  <<  y2  <<  end1;

   return 0;


OUTPUT: 77,  88,  55

                                                                                            ——————— Thanks


About Author


Leave A Reply

Powered by