cs240 computer science ii
DESCRIPTION
CS240 Computer Science II. “Scope”. Two principle forms of scope. Local: scope of a block. File scope: has names that are external or global. Storage (classes) types. Automatic: auto (default class) Resister: register ( a request to compiler) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/1.jpg)
CS240 Computer Science II
“Scope”
![Page 2: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/2.jpg)
Two principle forms of scope
• Local: scope of a block.
• File scope: has names that are external or global
![Page 3: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/3.jpg)
Storage (classes) types
• Automatic: auto (default class)
• Resister: register ( a request to compiler)
• External: extern (a mechanism to transmit information across modules)
• Static: static (retain its previous value when block is re-entered)
![Page 4: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/4.jpg)
External storage class
• When a variable is declared outside a function, storage is permanently (life of a program) assigned to it, and its storage class is external (extern) by default.
• If a variable is external (global), it is visible to all functions after it. It can not be auto or register class, but can be static.
• The word extern (or implied) tells the compiler to look for it elsewhere, either in this file or in some other file. Thus two modules can be compiled separately.
• Information can be passed into a function by using external variables (global) or through the parameter mechanism (preferred).
![Page 5: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/5.jpg)
Example of extern declarations
// file circle.cppconst double pi = 3.14159; // because of const, local to the file circle.cppDouble circle(double, radius){
return (pi * radius * radius);}
// file main.cpp#include <iostream.h>double circle(double); // function is automatically externint main( ){
double x;…cout << circle(x) << “is area of circle of radius “ << x;return 0;
}
![Page 6: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/6.jpg)
Class scope rules
Scope resolution operator :: is the highest precedence operator in C++; it has two forms::: i; // unary; refer to external scopeclassname :: i;// binary; refer to class scope
The unary operator is used to access a name that is at external scope and has been hidden by local or class scope, as shown in the next slide.
![Page 7: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/7.jpg)
External linkage
A program can be thought of as a collection of separate modules or compilation units linked together. When a function in one module is called from another module, the function must have external linkage. By default, names of functions and variables are external. The calling module must be given information about the function name and parameter types (function prototype).
![Page 8: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/8.jpg)
External linkage example
• In module 1:int sum(int a, int b);{
return a+b;}
• In module 2:…int sum(int, int);…Int n = sum(5, 2);…
![Page 9: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/9.jpg)
Internal linkage
• The word static assigns internal linkage to a name, thus making it hidden inside a module.
• A function with internal linkage cannot be called directly by name from another module, as seen in the following example.
![Page 10: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/10.jpg)
Internal linkage example
• Module 1
…
static int sum(int a, int b){ return a + b;}
…• Module 2:
…float sum = 0.0; // unrelated to sum function in module 1
…
![Page 11: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/11.jpg)
Example: unary scope resolution operator
int count = 0; // external or global variable
// if not initialized explicitly,
// system will initialize it to 0.
void f(double w[ ], double x, int &count)
{
for (int j = 0; j < N; ++j)
count += (w[j] == x); // local count
++ ::count; // global count
}
![Page 12: CS240 Computer Science II](https://reader036.vdocuments.net/reader036/viewer/2022082611/568132a5550346895d994683/html5/thumbnails/12.jpg)
Assignments
• Build test cases to verify all the discussions.