1 / 12

CS240 Computer Science II

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)

Download Presentation

CS240 Computer Science II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS240 Computer Science II “Scope”

  2. Two principle forms of scope • Local: scope of a block. • File scope: has names that are external or global

  3. 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)

  4. 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).

  5. Example of extern declarations // file circle.cpp const double pi = 3.14159; // because of const, local to the file circle.cpp Double circle(double, radius) { return (pi * radius * radius); } // file main.cpp #include <iostream.h> double circle(double); // function is automatically extern int main( ) { double x; … cout << circle(x) << “is area of circle of radius “ << x; return 0; }

  6. Class scope rules Scope resolution operator :: is the highest precedence operator in C++; it has two forms: :: i; // unary; refer to external scope classname :: 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.

  7. 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).

  8. 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); …

  9. 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.

  10. 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 …

  11. 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 }

  12. Assignments • Build test cases to verify all the discussions.

More Related