Top Banner
Week 11 Multi-file Programs and Scope of Variables
25

Week 11

Feb 24, 2016

Download

Documents

Deon

Week 11. Multi-file Programs and Scope of Variables . Multi-file programs . Header files. Creation of a header file math1.h // defining a header file: math1.h # ifndef math1 #define math1 #define M_PI 3.14 # endif. How to compile header files. Implementation files. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 11

Week 11

Multi-file Programs and Scope of Variables

Page 2: Week 11

Multi-file programs

Page 3: Week 11

Header files

Creation of a header file math1.h

// defining a header file: math1.h #ifndef math1 #define math1 #define M_PI 3.14 #endif

Page 4: Week 11

How to compile header files

Page 5: Week 11

Implementation files

• Don’t forget that the program allows to have one main() function only but many sub-functions.

• The main() function and other sub-functions can be put in either one file or different files. – Note that the main() function file does not need to

#include those sub-function files; it incorporates those files by the function call mechanism.

Page 6: Week 11

Regions in a program

• There are three kinds of regions in a program according to their sizes, as shown in the diagram next slide.– File region – Function region – Block region.

Page 7: Week 11

Regions in a program

Page 8: Week 11

Scope of a variable

• Category 1: A variable declared once only in a region.

• Category 2: The variables with the same name declared in more than two regions separately

Page 9: Week 11

Category 1: the variable declared once only in a region

Page 10: Week 11

Category 1: the variable declared once only in a region

• Case 1a : A variable declared in a block level region.

• Case 1b : A variable declared in a function level region.

• Case 1c : A variable declared in a file level region.

• Case 2a : A variable declared in a block level region

• Case 2b : A variable declared in a function level region.

• Case 2c : A variable declared in a file level region.

Page 11: Week 11

Case 1a – A variable declared in a block region ( file 1)

#include <iostream.h> void func1(); main() {

// cout << "main(i)= " << i << endl; // it is error if this line is not masked func1();

} void func1() {

// cout << "fun1(i)= " << i << endl; // it is error if this line is not masked { int i=7;

cout << "block(i)= " << i << endl; // only this cout<< works }

}

Page 12: Week 11

Case 1b - A variable declared in a function region (file 1)

Page 13: Week 11

Case 1c- A variable declared in a file region (file 1)

Page 14: Week 11

A variable declared after the function in a file

Page 15: Week 11

Case 2a- A variable declared in a block region (file 2)

Page 16: Week 11

Case 2b – A variable declared in a function region (file 2)

Page 17: Week 11

Case 2c - A variable declared in a file region (file 2)

Page 18: Week 11

Category 2: the variables with the same name declared in two or more regions separately

• We have discussed the way to find the scope of a variable declared once only. From now on we are going to learn how to find the scope of the variables with the same name when they are declared in different regions at the same time.

{ int i; float i;

}

{ int i;

{ float i; }

}

Page 19: Week 11

Scope of the variables with same name in different regions

Page 20: Week 11

Scope of the variables with same name in different regions

• The output values are: main(i) =1 (not 0) func2(i) =2 (not 0) block(i) =3 (not 0, or 2).

• In general: a variable in the inner region has a priority for use over those in the outer regions when they have same name.

Page 21: Week 11

Local variables and global variables

• We normally use a term global variables for those declared in either program level regions or in file level regions and another term local variables for those declared either in function level regions or in block level regions.

Page 22: Week 11

How to refer to a file scope variable from within a local block scope

• If you wish to access a file scope variable i from within a block in a different file, then you may use “extern i;” to get it, on the condition that no other local variable with the same has been defied within the block. However if this condition is not met or if there is the variable that has been already declared, can you still access that variable? The answer is yes but you need to use a special sign called “scope resolution operator ::” to do this job.

Page 23: Week 11

Scopes of auto variables (local) and local static variables

• All the variable declared within the functions and blocks are automatic variables by default. Prefacing the definition with an “auto” defines an automatic variable. When a block is entered the system allocates memory for the auto variables. Within that block, these variables are declared and are considered “local” to the block. When the block is left, the system releases the memory. Thus the values of these variables disappear. If the block is re-entered, the system once again allocates memory, and so previous values are unknown.

• However in most situations, we wish the system to allow a local variable to retain its previous value but not to initialise the variable (in spite of the initialisation statement ) when the block is re-entered. To do so, we need to define a new local variable called a static variable, which is made by prefacing “static” before the variable.

Page 24: Week 11

Scopes of auto variables (local) and local static variables

Page 25: Week 11

Scope of a global static variable

extern i in file 2 is aimed to use static int i in file 1. This operation is illegal because “static” doesn’t allow the variable i to be used outside file 1 according to the sense of the static.