// ScopeOperator1.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates how the scope operator can be used to refer to a hidden // global namespace member. Looking at the code, there are two definitions // of the variable "max". // Both declarations can still be accessed by the fibonacci function. All // unqualified references will refer to the local declaration. To access the // global declaration, the scope operator must be used. // // Key points: 1) the Scope operator :: #include // Global variables const int max = 65000; const int lineLength = 12; void fibonacci(int max) { if (max < 2) return; cout << "0 1 "; int v1 = 0, v2 = 1, cur; for (int i = 3; i <= max; ++i) { cur = v1 + v2; if (cur > ::max) break; cout << cur << " "; v1 = v2; v2 = cur; if (i % lineLength == 0) cout << endl; } } int main() { cout << "Fibonacci Series: 16\n"; fibonacci(16); cout << endl << endl; fibonacci(16000); cout << endl; return 0; } /* The output Fibonacci Series: 16 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 */