[ Pobierz całość w formacie PDF ]
.// Fig// Fig.3.12: fig03_12.cpp// A scoping example#include <iostream.h>void a( void ); // function prototypevoid b( void ); // function prototypevoid c( void ); // function prototypeint x = 1; // global variableint main() {int x = 5; // local variable to maincout << "local x in outer scope of main is " << x << endl;{ // start new scopeint x = 7;cout << "local x in inner scope of main is " << x << endl;} // end new scopecout << "local x in outer scope of main is " << x << endl;a(); // a has automatic local xb(); // b has static local xc(); // c uses global xa(); // a reinitializes automatic local xb(); // static local x retains its previous valuec(); // global x also retains its valuecout << "local x in main is " << x << endl;return 0;}void a( void ){int x = 25; // initialized each time a is calledcout << endl << "local x in a is " << x<< " after entering a" << endl;++x;cout << "local x in a is " << x<< " before exiting a" << endl;}void b( void ){static int x = 50; // Static initialization only// first time b is called.cout << endl << "local static x is " << x<< " on entering b" << endl;++x;cout << "local static x is " << x<< " on exiting b" << endl;}void c( void ){cout << endl << "global x is " << x<< " on entering c" << endl;x *= 10;cout << "global x is " << x << " on exiting c" << endl;}
[ Pobierz całość w formacie PDF ]