C++ Variables

Variable is a value holder in the computer program.

A variable is named memory location, where the user can store different values of the specified data type. In simple words, a variable is a value holder. Every variable in the program has the following properties.

  • Every variable has a name (user-specified) and an address.
  • Every variable must be created with a data type.
  • Every variable is allocated with a memory based on the data type of it.
  • Every variable has a value.

Creating Variables

Creating a variable is nothing but declaring a variable. In C++, all the variables must be declared before they use in the program. The general syntax for declaring a variable is as follows.

Syntax for creating a variable
data_type variable_name;

The above syntax is used to declare a variable without initial value. But, C++ allows us to declare a variable with initial value too. And it can be done using the following syntax.

Syntax for creating a variable with initial value
data_type variable_name = value;

In C++, multiple variables of the same data type may be declared using a single statement. In this case, all the variables must be separated with a comma symbol. Let's look at the following declaration statements.

Example statements for variables creation in c++
int a, b, c;
float x = 10.5, y;
char p = 'A', q = 'B', r = 'C';

Every declaration statement with multiple variables may contain declaration without initial value or with an initial value or mix-up of both.


Types of Variables

Based on the location of variable declaration, variables are classified into five types. They are as follows.

  • Local Variables
  • Global Variables
  • Formal Variables
  • Member Variables
  • Instance Variables

Local Variables

The variables that are declared inside a function or a block are called local variables. The local variable is visible only inside the function or block in which it is declared. That means the local variables can be accessed by the statements that are inside the function or block in which the variable has declared. Outside the function or block, the local variable can't be accessible.

The local variables are created upon execution control enters into the function or block and destroyed upon exit.

Consider the following example of C++ program.

C++ Program to illustrate local variables concept
#include < iostream.h > 

using namespace std;

int sum(){
   int a = 10, b = 20;
   //cout<< "Result = " << result; // Error - result can't be accessed
   return a + b;
}

int main()
{
    int result;

    result = sum();
    cout << "Sum = " << result << endl;
    //cout << a << "+" << b << "=" << sum() << endl; // Error -  a & b are not accessible

    return 0;
}

Global Variables

The variables that are declared outside a function are called global variables. The global variable is visible inside all the functions that are defined after its declaration. That means the global variables can be accessed by all the functions that are created after the global variable declaration. The functions that have created before the global variable declaration can't access it.

The local variables are created upon execution starts and destroyed upon exit.

Consider the following example of C++ program.

C++ Program to illustrate global variables concept
#include <iostream>

using namespace std;

int a = 10; // Global variable

int main()
{
    int b = 20; // Local variable
    void show();
    cout << "Inside the main!!!" << endl;
    cout << "a = " << a << endl;
    show();

    return 0;
}

void show(){
   int a = 30;  // Local variable with same name as global
   cout << "Inside the show!!" << endl;
   cout << "a = " << a << endl;    // refer to local variable
   cout << "a = " << : :a << endl;   // refer to global variable
}
  • When both global and local variables has same name, only local variable is refered inside the function. To refer the global variable we need to use : : (scope resolution) operator.

Formal Variables

The variables that are created in the function definition as receivers to the parameter values are called formal variables. The formal variables are also known as formal parameters, and they act as local variables inside the function.

Consider the following example of C++ program.

C++ Program to illustrate formal variables concept
#include <iostream>

using namespace std;

void add(int x, int y){
    cout << "sum = " << x+y << endl;
}

int main()
{
    int a = 10, b = 20; // Local variable
    
    add(a, b);
    
    return 0;
}

In the above example program, the variables x and y are called formal variables.

  • The formal variables are allowed only when the function has parameters.

Member Variables

The variables that are created in a class are known as member variables. The member variables are accessible to all the methods of that class. The member variables are also accessible to the methods of other classes using inheritance mechanism.

Consider the following example of C++ program.

C++ Program to illustrate member variables concept
#include <iostream>

using namespace std;

class Sample{
    public:
        int a, b;
        void setData(){
            cout << "Enter a and b values: ";
            cin >> a >> b;
        }
        void getData(){
            cout << "a = " << a << endl << "b = " << b << endl;
        }
};

int main()
{
    Sample s;

    s.setData();
    s.getData();

    return 0;
}

In the above example program, the variables a and b are called member variables of class Sample.


Instance Variables

The instance variable is a special type of variable of a user-defined data type called class. That means an instance variable is a variable of class type. The instance variables are also known as objects. The instance variables are used to access the class members from outside the class.

Consider the following example of C++ program.

C++ Program to illustrate instance variables concept
#include <iostream>

using namespace std;

class Sample{
    public:
        int a, b;
        void setData(){
            cout << "Enter a and b values: ";
            cin >> a >> b;
        }
        void getData(){
            cout << "a = " << a << endl << "b = " << b << endl;
        }
};

int main()
{
    Sample s;

    s.setData();
    s.getData();

    return 0;
}

In the above example program, the variable s in the main method is called instance variable of class Sample, and it also is known as the object of Sample class.