Variables

 < Day Day Up > 



A variable is an object whose value will likely change during the execution of a program. Variables have a storage class, and a visibility or scope

Declaring

You have seen several variables declared and used in this chapter. You precede the identifier with the variable’s type as in the following example:

char* f_name;

In this case the type of the variable named f_name is pointer to char. Here’s another example:

float account_balance;

Variables can be defined or initialized at the point of declaration or later if necessary. As a rule, though, it is a good idea to initialize the variable to some known value so you don’t try to use a variable that contains a garbage value. Here’s an example:

long defunct_dot_coms = 0;  ... defunct_dot_coms = 32345;

Here the variable named defunct_dot_coms is declared and initialized to 0. You can also initialize a variable using “constructor” notation. Check this out:

int cool_variable(40);

Scope

Variables have a scope or authorized area of usage within a program. Sorting out all the scoping rules can be overwhelming and they’re best mastered on-the-fly so I will only discuss two basic scoping rules here and leave the others to the chapter to which they more aptly belong.

Scoping is also related to the topic of linkage, so, to understand each completely, you must understand both as they relate to each other. The best way to learn them both is by studying examples so I will go light on the verse and heavy on the code.

Local Scope

A variable is available for use after its point of declaration. Let us look at an example:

int a; a = 3; cout<<a<<endl;

The variable a is available for use just after the its declaration on the first line, just before the semicolon indicating the end of the first statement. From that point forward a is in scope and can be used as shown. The braces { } can be used to introduce local blocks of scope into a sequence of statements as shown in the following example:

 1   int a = 1, b = 2; 2    {  3      cout<<a<<endl; 4      int a = 3; 5      cout<<a<<endl; 6    } 7   cout<<a<<" "<<b<<endl;

In this example, two integer variables, a, and b, are declared and initialized on line 1. On the line 2 a new scope is introduced with the left brace and proceeds up to the closing right brace on line 6. Within this scope a new integer variable named a is declared and initialized on line 4. Its scope is up to the closing right brace. Figure 5-15 gives a graphical view of what’s happening:

click to expand
Figure 5-15: Creating Local Scope Blocks with Braces

When a local scope is created within another scope, the inner scope is said to be the enclosed scope, while the outer scope is referred to as the enclosing scope. If you declare a variable in an enclosed scope using the name of a variable already in scope in the outer scope it causes the outer scope variable to be hidden or masked from the point of declaration in the enclosed scope.

Looking at figure 5-15, the variable a declared in the outer scope is visible up to where the second variable a is declared in the inner scope. The statement on line 3 will print the outer scope a value to the screen. The statement on line 5 will print the inner scope’s a value to the screen, and finally, the last statement will print the values of both outer scope variables a and b.

Function Scope

Functions will be discussed in detail in chapter 9 so I will keep the examples here simple. Variables declared within a function have scope within that function. Examine the following code:

 1  int a = 1, b = 2; 2 3  void someFunction(){ 4     cout<<"In someFunction(): outer scope a == "<<a<<endl; 5     int a = 3; 6     cout<<"In someFunction(): inner scope a == "<<a<<endl; 7  }

In this example, the integer variable a declared on line 1 exists in the outer scope and is visible within someFunction() up to the point of declaration of someFunction()’s local variable a. Example 5.5 illustrates the scoping issues discussed thus far:

Listing 5.5: Source Code Showing Local,Function, and File Scoping

start example
 1  #include <iostream>  2  3  using namespace std;  //introduces namespace std  4  5  void someFunction();  6  7  int a = 1, b = 2;  8  9  void someFunction(){ 10     cout<<"In someFunction(): outer scope a == "<<a<<endl; 11     int a = 3; 12     cout<<"In someFunction(): inner scope a == "<<a<<endl; 13  } 14 15  int main(){ 16     someFunction(); 17     {  18     cout<<a<<endl; 19     int a = 3; 20     cout<<a<<endl; 21     } 22     cout<<a<<" "<<b<<endl; 23  return 0; 24  }
end example

Referring to example 5.5, line 5 gives the function declaration of someFunction(). On line 7 appears the declaration and definition of integer variables a and b. Since these two variables are declared outside of any function they have file scope. Variables with file scope are often referred to as global variables because they are visible to all functions defined within the file in which they appear and, as you will see below, to functions appearing in other files as well.

File Scope

As mentioned above, variables declared outside of any function within a file have file scope. These are referred to as global variables because they can been seen globally throughout the program.

Keep the use of global variables to the absolute minimum required to reduce coupling in your program. Remember from chapter 1 that intermodule coupling is a bad thing! Minimize coupling — maximize cohesion.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net