Reference Variable
C++ introduces new kind of variable known as the reference variable.A reference variable provide an alias (alternative name) for a previously defined variable.
A reference variable is created as follow:
data_type & reference-name = variable-name;
For example,
float total = 100;
float& sum = total;
cout << total;
cout << sum;
both will print the value 100.
if you make changes in any one variable either sum or total then both the variable will get changed.
A simple program which demonstrate reference variable :
int main()
{
int m = 10;
f(m);
cout << m;
}
void f(int &x)
{
x= 12;
}
Output :
12
Manipulator
Manipulators are operators are used to format the data display.
The most commonly used manipulator are endl and setw.
Example of endl manipulator is
cout << “m = 10” << endl;
cout << “n = 20″≺≺ endl;
The Output will be
m = 10
n = 20
The manipulator setw specifies the field width for printing the value of the variable
cout << setw(5) << endl; // right justified
cout << setw(-5) << endl; // left justified
A simple program which demonstarte manipulator:
int main()
{
int num1 = 950, num2 = 95;
cout << set(10) << “number1= ” << num1 << endl;
cout << set(10) << “number2 = ” << num2 << endl;
}
Output:
number1 = 950
number2 = 95
Scope Resolution Operator
In C, the global version of a variable cannot be accssed from within the inner block.C++ resolves this problem by introducing a new operator (::) called the scope resolution operator.This can be used to uncover a hidden variable.
It takes the following term:
:: variable-name;
This operator allows to the global version of a variable.
A simple program which demonstrate the used of Scope resolution operator:
#include<iostream>
using namespace std;
int m = 10;
int main()
{
int m = 20;
cout << m; // To print local variable
cout << ::m; // To print global variable
}
Output
20
10