cs240 computer science ii

12
CS240 Computer Science II “Scope”

Upload: lewis-swanson

Post on 01-Jan-2016

25 views

Category:

Documents


3 download

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 Presentation

TRANSCRIPT

Page 1: CS240 Computer Science II

CS240 Computer Science II

“Scope”

Page 2: CS240 Computer Science II

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

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

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

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

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

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

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

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

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

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

Assignments

• Build test cases to verify all the discussions.