My question is this: Why does the program return the value of "80" for count3 in the last cout statement? Why doesn't it return a value of "50", since the cout statement is located in the outer block and so should reference the value made in the initializiation made near the top of main().
What am I not understanding here? Thanks.
CODE
//ex2_06.cpp
//demonstrating variable scope
#include<iostream>
using namespace std;
int count1 = 100; //Global version fo count1
int main()
{ //function scope begins here
int count1 = 10;
int count3 = 50;
cout << endl
<< "Value of outer count1 = " << count1
<< endl;
cout << "Value of global count1 = " << ::count1 //From outer block
<< endl;
{ //new scope starte here...
int count1 = 20; //This hides the outer count1
int count2 = 30;
cout << "Value of inner count1 = " << count1
<< endl;
cout << "Value of global count1 = " << ::count1 //From outer block
<< endl;
count1 += 3; //This affects the inner count1
count3 += count2;
} //...and ends here
cout << "Value of outer count1 = " << count1
<< endl
<< "Value of outer count3 = " << count3
<< endl;
//cout << count2 << endl; //uncomment to get an error
return 0;
} //Function scope ends here
//demonstrating variable scope
#include<iostream>
using namespace std;
int count1 = 100; //Global version fo count1
int main()
{ //function scope begins here
int count1 = 10;
int count3 = 50;
cout << endl
<< "Value of outer count1 = " << count1
<< endl;
cout << "Value of global count1 = " << ::count1 //From outer block
<< endl;
{ //new scope starte here...
int count1 = 20; //This hides the outer count1
int count2 = 30;
cout << "Value of inner count1 = " << count1
<< endl;
cout << "Value of global count1 = " << ::count1 //From outer block
<< endl;
count1 += 3; //This affects the inner count1
count3 += count2;
} //...and ends here
cout << "Value of outer count1 = " << count1
<< endl
<< "Value of outer count3 = " << count3
<< endl;
//cout << count2 << endl; //uncomment to get an error
return 0;
} //Function scope ends here

