C++ Global Variables

A memory location needs to be identified, and it is identified by a name, called a variable. The length of a program, from top to bottom, has different portions that do different things. Some portions are related but discontinuous by length. The intervals by the length of the discontinuous portions have other portions, which are related to other portions.

A scope is a set of related portions where a variable can be used or seen. Scopes have names. A global variable is a variable that can be seen in every scope of a program. A global variable can be of different types. This article explains how a global variable can be seen in common scopes.

Article Content

Related Portions

Consider the following program:

    #include <iostream>
    using namespace std;

    int it = 5;
    float ft = 2.7;

    int fn1 (int integ = it)
    {
        /* statements */
        return integ;
    }

    float fn2 (float flt = ft)
    {
        /* statements */
        return flt;
    }
   
    int main()
    {
        cout << fn1() << endl;
        cout << fn2() << endl;

        return 0;
    }

The output is:

    5
    2.7

The variables “it” and ft are of different types. Considering the variable “it” first: ‘it’ is seen in its line of declaration. Some lines are skipped. It is then seen in the signature of fn1(). It is seen as well in the whole body of fn1(). Some lines and a portion are skipped. It can then be seen in the whole body of the main() function. Actually, it is seen indirectly, in the first statement of the main() function, through the fn1() function call. The portions for the variable “it” have just been illustrated.

Considering the variable ft now: ft is seen in its line of declaration. Some lines and a portion (fn1 definition) are skipped. A blank line is skipped. It is then seen in the signature of fn2(). It is seen in the whole body of fn2(). It can then be seen in the whole body of the main() function. Actually, it is seen indirectly, in the second statement of the main() function, through the fn2() function call. The portions for the variable ‘ft’ have just been illustrated.

Global Scope and Global Variable

When a programmer just starts typing a file, that is the global scope. Consider the following program:

    #include <iostream>
    using namespace std;

    char var = ‘E’;

    int main()
    {
        cout <<var<<'\n';
        cout <<::var<<'\n';

        return 0;
    }

The output is:

    E
    E

In this program, the portion or scope for var starts from the point of declaration of var, continues downward until the end of the translation unit (file).

The main() function's body is a different scope in its own right; the global scope nests the main() function scope.

The variable var is a global scope variable because it can be seen everywhere in the file, beginning from where it has been declared. It can be seen in the main() function scope. In the previous program, “it” and ft are global scope variables. Each could be seen in its line of declaration, in a function scope, and in the main() function scope.

To access a global variable (variable of the global scope) from a different scope, the variable name is used directly or preceded by the scope resolution operator,:: as shown in the above program.

Explaining the code: all programs in C++ should begin with at least the first two lines that this program begins with. The lines are:

    #include <iostream>
    using namespace std;

The first line here is not a statement; it is a directive, an inclusion directive. It includes the input/output stream library, which is responsible for sending output to the terminal and receiving input from the keyboard. The terminal and the keyboard together are called the console. This library is called the iostream library (i.e., the input/output stream library).

The second line in the program is not a directive; it is a statement, and it ends with a semicolon. It says any name used below it is from the standard namespace unless indicated otherwise. It might interest the reader, to know that the namespace has a scope; however, that will not be addressed in this article.

The next statement, below the first two lines, is a char variable complete declaration. In the main() function, the first two lines print the value of the same variable to the terminal in slightly different ways.

Block Scope and Global Variable

Examples of compound statements are the if, while, do, for, or switch statements. Each of these compound statements can have a block. A block scope variable is a variable defined in a block. Its scope starts at its point of declaration and finishes at the end of its block. A global variable is normally defined outside and above this block. A global variable can be seen outside this block, in the header line of this block, and inside the block. The following program illustrates this with an if-block:

    #include <iostream>
    using namespace std;

    int i = 5;
   
    int main()
    {
        cout << "First i is: " << i << endl;

        if (i == 5) {
            cout << "Second i is same: " << i  << endl;
        }

        return 0;
    }

The output is:

    First i is: 5
    Second i is same: 5

The if-block is a nested block to the main() function scope, which is a nested block to the global scope. This program shows that global variables can be seen in nested blocks.

Overriding in Nested Block

A global variable can be overridden in a nested block. That is, a global variable can be replaced by a variable of the same name in a nested block. In the following program, i is replaced by another entity, a float, in a nested block. And this is the rule: A variable in a nested scope with the same name as one outside the scope overrides that outer variable. So, for the nested scope, the variable declared outside the nested scope with the same name cannot be seen inside the nested scope; because the one inside the nested scope has taken its place inside the nested scope. The one inside the nested scope is seen from its point of declaration to the end of its block. The one inside the nested scope does not need to have any relationship with the one outside the nested scope. The program is:

    #include <iostream>
    using namespace std;

    int i = 5;
   
    int main()
    {
        cout << "First i is: " << i << endl;

        if (i == 5) {
            float i = 7.0;
            cout << "Second i is different: " << i  << endl;
        }

        return 0;
    }

The output is:

    First i is: 5
    Second i is different: 7

The overridden variable is of the same name and can still be of the same type.

Normal Function Scope and Global Variable

Consider the following program:

    #include <iostream>
    using namespace std;

    int fn (int integ);

    int i = 5;

    int fn (int integ = i)
    {
        /* statements */
        return integ;
    }
   
    int main()
    {
        cout << fn() << endl;

        return 0;
    }

The output is 5. The declarative region of the function fn(), consists of the line “int fn (int integ);” of its prototype, the portion of its definition in the global scope, and the calling statement in the main function body. All these lines and portions form the scope of the function fn(). The function signature of fn() is part of the function scope (as well as the body). Now, the global variable, i has been seen in all the lines or portions of the fn function.

Consider the following program, with classes and instantiated objects:

    #include <iostream>
    using namespace std;

    int i = 5;

    class Calc {
        private:
        int pM = i;
        public:
        void mfn(int inte = i)
            {
                cout << inte << endl;
            }
    };

    class DCla: public Calc
        {
            public:
            int dM = i;
        };
   
    int main()
    {
        Calc obj;
        obj.mfn();
        DCla dObj;
        cout << dObj.dM << endl;

        return 0;
    }

The output is:

    5
    5

The program has a base class, Calc, and its derived class, Dcla. The base class is public to the derived class: this means the derived class can see the protected members of the base class. So the portions for the class, Calc, are its block and the block of the derived class, Dcla. That is, there are two portions for the scope of the class, Calc. Now, the global variable, i is seen in these two portions.

Conclusion

A memory location needs to be identified, and it is identified by a name, called a variable. The length of a program, from top to bottom, has different portions that do different things. Some portions are related but discontinuous by length. The intervals by the length of the discontinuous portions have other portions, which are related to other portions.

A scope is a set of related portions where a variable can be used or seen. Scopes have names. A global variable is a variable that can be seen in every scope of the program. A global variable can be of any type.



from https://ift.tt/3bPSqmR

Post a Comment

0 Comments