504 pdfsam Cplusplus Primer Plus 4th Edition

This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks int ...

0 downloads 4 Views 2KB Size
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks

int main() // uses global variable { cout << "Global warming is " << warming << " degrees.\n"; update(0.1); // call function to change warming cout << "Global warming is " << warming << " degrees.\n"; local(); // call function with local warming cout << "Global warming is " << warming << " degrees.\n"; return 0; } void update(double dt) // modifies global variable { extern double warming; // optional redeclaration warming += dt; cout << "Updating global warming to " << warming; cout << " degrees.\n"; } void local() // uses local variable { double warming = 0.8; // new variable hides external one cout << "Local warming = " << warming << " degrees.\n"; // Access global variable with the // scope resolution operator cout << "But global warming = " << ::warming; cout << " degrees.\n"; } Here is the output:

Global warming is 0.3 degrees. Updating global warming to 0.4 degrees. Global warming is 0.4 degrees. Local warming = 0.8 degrees. But global warming = 0.4 degrees.