Variables | How to declare variables – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

variablesVariables | How to declare variables
Object Oriented Programming(OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

A variable is the most fundamental aspect of any computer language. It is a location in the computer memory which can store data and is given a symbolic name for easy reference. The variables can be used to hold different values at different times during the execution of a program. To understand more clearly, let us take following example:
Total = 20.00 (i)
Net = Total – 12.00 (ii)

In equation (i), a value 20.00 has been stored in memory location Total. The variable Total is used in statement (ii) for the calculation of another variable Net. The point worth nothing is that the variable Total is used in statement (ii) by its name not by its value. Before a variable is used in a program, it has to be defined. This activity enable the compiler to make available the appropriate type of location in the memory. The definition of a variable consists of the type name followed by the name of the variable.

Declaration of variables:
In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float, etc) followed by a valid variable identifier. For example:
int a;
float mynumber;

There are two valid declarations of variables. The first one declares a variable of type int with the identifier ‘a’. The second one declares a variable of type float with the identifier ‘mynumber’. Once declared, the variables ‘a’ and ‘mynumber’ can be used within the rest of their scope in the program.

If you are going to declare more than variable of the same type, you can declare all of them in a single statement by separating their identifier with commas. For example:
int a,b,c;

This declares three variables (a,b, and c) all of them of type int and has exactly the same meaning as;
int a;
int b;
int c;

Similarly, a variable Total of type float can be declared as shown below;
float Total;

Example of some valid variable declaration are:
int count;
int i,j,k;
char ch, first;
float total, Net;
long int sal;

(Visited 231 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag :

Post a Comment

Your email is never published nor shared. Required fields are marked *


Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary