Yurttas/PL/OOL/Cplusplus/F/02/04/02/00/01/m 11.cpp

From ZCubes Wiki
Jump to navigation Jump to search
 1/*
 2   Copyright(C) 1998
 3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4
 5   Permission to use, copy, modify, and distribute this
 6   software and its documentation for EDUCATIONAL purposes
 7   and without fee is hereby granted provided that this
 8   copyright notice appears in all copies.
 9 
10   date   : January 1, 1998.
11   author : Salih Yurttas.
12
13   m_11.cpp
14*/
15
16
17// the rule: minimize or eliminate the globals
18//           use locals
19
20
21#include <iostream>
22
23using namespace std;
24
25int a=1; // a exists and visible in the file scope
26
27void f() {
28  static int j=0; // j is local and hidden from other functions,
29                  // except its own function scope
30                  // j is visible in f but exists beyond f
31
32  cout << "f / ";
33  cout << ++j;
34  cout << endl;
35  cout << "a = ";
36  cout << ++a;
37  cout << endl;
38}
39
40int main(int argc, char* argv[]) {
41
42  int i=0;        // i is local and hidden from other functions,
43                  // except its own function scope
44
45  cout << "m / ";
46  cout << ++i;
47  cout << endl;
48  cout << "a = ";
49  cout << ++a;
50  cout << endl;
51
52  cout << endl;
53
54  f();
55
56  cout << endl;
57
58  cout << "m / ";
59  cout << ++i;
60  cout << endl;
61  cout << "a = ";
62  cout << ++a;
63  cout << endl;
64
65  cout << endl;
66
67  f();
68
69  cout << endl;
70
71  cout << "m / ";
72  cout << ++i;
73  cout << endl;
74  cout << "a = ";
75  cout << ++a;
76  cout << endl;
77
78}