|
|
|
|
![]() ![]() |
Jun 27 2005, 08:42 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 407 Joined: 13-December 04 Member No.: 2,696 |
Below is my code, which pretty much follows the Ivor Horton book on learning C++.
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 This post has been edited by snlildude87: Jun 27 2005, 09:38 PM |
|
|
|
Jun 27 2005, 09:44 PM
Post
#2
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 936 Joined: 14-April 05 From: West Chester, PA Member No.: 5,636 |
The reason i think it displays 80 instead of 50 is because of one reason. You cannot have brackets unless it is for a class or structure or function definition or unless the statement is a while, for, if, etc. statement. THerefore, where you say
QUOTE { //new scope starte here... you should get an error or it just ignores them and stays with the same scope. Try it with an if statement to understand what i mean.
|
|
|
|
Jun 28 2005, 05:29 AM
Post
#3
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 142 Joined: 24-December 04 From: Queensland, Australia Member No.: 2,902 |
ffanatics, nope. You can have parentheses wherever you like, as long as you close any parentheses you open.
The reason you get 80 is because you added 30 to it... it's no trick. Whenever you declare a new scope within a previous... e.g. CODE int count1 = 10 int count3 = 50; { // New scope // Declare a variable called count1 (this means that when referring // to count1 the count1 referred to is the one in this scope, and the // one in the outer scope is ignored int count1 = 20; // Because no new variable, count3 has been declared here, count3 // from the outer scope is referred to count3 += count1; } // Close scope You'll notice in the example you gave, count3 is not initialized in the new scope either, so when that value is used, it refers to the first declaration of count3. |
|
|
|
Jun 28 2005, 01:36 PM
Post
#4
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
hi,
yeah. that is the answer. You are adding count2 to count3 in the inner block. But since count3 is not declared in the inner block, it affects the count3 variable in the outer scope and hence 30 is added to the existing value of 50. This gives the answer as 80. you would understand it better if you just output count3 in the inner block. Cheers. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 13th October 2008 - 04:42 PM |