CMSC202 Lecture Notes for week 2
Files used in the lecture
During the week of Feb. 8, we discussed the following:
- The "vector" class shows how a class is used in a program.
- "bulletprofing" a program, with the "lowecase" program
as an example, shows how to design for user input
- Scope and visibility tell us where an identifier is known
- The scope resolution operator is used to "unhide" a
global variable, and to specify that a function is in the extended
scope of a class.
- Static declarations modify an identifier's lifetime or visibility.
- Declaring a variable as "external" means that no memory is allocated for
it; the variable must be (declared and) defined elsewhere.
- we started the discussion of recursion by examining a recursive
definition of the factorial function
Using Classes
The Vector class and its implementation
are a simple class used to illustrate how we can solve a more interesting
problem (is this a rectangle?). Our knowledge of linear algebra tells us
that two vecrors are perpendicular when their inner product is zero. Our
understanding of the vector class also tells us that we must have nonzero
vectors to compute a meaningful inner product. We use these problem
oriented observations to add a non-member function, perpendicular
which takes two vectors as its arguments. From this, we see that a class
instance may be used as a parameter or even a return type by a non-member
function (in this regard, classes behave just like any other data type!).
(The sample driver for the vector class is on your text's floppy disk.)
Error checking
The idea that this example makes is that we, as implementors of programs,
should always take action to protect our code from defective data. We also
introduce the way to read command-line arguments using
argc and argv (see pages 58-59). The lowercase program(also on class floppy)
uses the command line args to check for 0 or 2 files provided on the command
line, and it behaves appropriate to what the user's input is.
As a review, invoking function main this way
main(int argc, char * argv[]) { ...
causes argc to hold a count of command line args, and for the array of
strings, argv, to hold them. argv[0] is always the name of the
program that was invoked. This allows us to give the same executable code
different names (e.g. cp, ln, mv)
Scope and Visibility
The Scope of an identifier is the part of the program where it (the
identifier) is defined. There are 4 separate scopes in C++:
- local scope: an identifier with local scope can be seen only within the
block where it was defined. Variables with local scope come into existence
when the statement where they are defined is executed. A (non-static)
local variable goes out of scope (and is destroyed) when its defining block
is finished. See example for this point.
- Function Scope: is the visibility of a label (the target of a goto).
Since we do not use goto's (ever) in this class, you will
just have to forego the pleasure of experiencing identifiers with
function scope.
- Class Scope is the "extent" of a class. An identifier declared inside
a class has class scope, and is usually visible within the extended scope
of the class. This means that, should you use the scope resolution operator,
:: , to qualify a function, that function is in the extended scope of the
class.
- File Scope refers to what we think of as "global" identifiers. Briefly,
any identifier declared outside of a function has file scope. It is visible from
its point of definition to the end of the file in which it is declared.
Declaring an identifier as static changes its visibility (or,
in some cases, its lifetime). A global variable is normally visible everywhere
(i.e. globally). In particular, it is visible in any other file that is
linked with the one where it's declared. By scoping the global identifier as static,
e.g.
static int x;
it ceases to be visible outside the file where it was declared. This is especially
useful when writing library files with global identifiers, since it helps to assure
the library is portable.
Previous Lecture
Next Lecture