A scoping example : Variables « Language Basics « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Language Basics » Variables 
1.3.3.A scoping example
#include <iostream>
using std::cout;
using std::endl;

void useLocalvoid )
void useStaticLocalvoid )
void useGlobalvoid )

int x = 1;

int main()
{
   int x = 5;

   cout << x << endl;
   {
      int x = 7;
      cout << "local x in main's inner scope is " << x << endl;
   }

   cout << x << endl;

   useLocal()
   useStaticLocal()
   useGlobal()
   useLocal()
   useStaticLocal()
   useGlobal()

   cout << x << endl;
   return 0;


void useLocalvoid )
{
   int x = 25;

   cout << "local x is " << x << " on entering useLocal" << endl;
   x = x + 20;
   cout << "local x is " << x << " on exiting useLocal" << endl;


void useStaticLocalvoid )
{
   static int x = 50;

   cout << "local static x is " << x << " on entering useStaticLocal" 
      << endl;
   x = x + 20;
   cout << "local static x is " << x << " on exiting useStaticLocal" 
      << endl;


void useGlobalvoid )
{
   cout << "global x is " << x << " on entering useGlobal" << endl;
   x = x + 20;
   cout << "global x is " << x << " on exiting useGlobal" << endl;
}
5
local x in main's inner scope is 7
5
local x is 25 on entering useLocal
local x is 45 on exiting useLocal
local static x is 50 on entering useStaticLocal
local static x is 70 on exiting useStaticLocal
global x is 1 on entering useGlobal
global x is 21 on exiting useGlobal
local x is 25 on entering useLocal
local x is 45 on exiting useLocal
local static x is 70 on entering useStaticLocal
local static x is 90 on exiting useStaticLocal
global x is 21 on entering useGlobal
global x is 41 on exiting useGlobal
5
1.3.Variables
1.3.1.Assign value to a variable
1.3.2.Dynamic initialization.
1.3.3.A scoping example
1.3.4.Using the unary scope resolution operator
1.3.5.Finding maximum and minimum values for data types
1.3.6.Comparing data values
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.