C++ Variable Initialization


A variable can be initialised by a calculation involving other variables:


#include <iostream.h>

void main ()
{
double a = 12 * 3.25;
double b = a + 1.112;

cout << "a contains&nbsp;: " << a << endl;
cout << "b contains: " << b << endl;

a = a * 2 + b;

double c = a + b * a;

cout << "c contains: " << c << endl;
}


Like in C, variables can be encapsulated between hooks:


#include <iostream.h>

void main()
{
double a;

cout << "Type a number: ";
cin >> a;

{
int a = 1;
a = a * 10 + 4;
cout << "Local number: " << a << endl;
}

cout << "You typed: " << a << endl;
}



Added on June 13, 2007 Comment
}