Block Structured
Block Structured
Block Structured
© ©
CS 538 Spring 2008 44 CS 538 Spring 2008 45
© ©
CS 538 Spring 2008 46 CS 538 Spring 2008 47
Block Structure Concepts Variations in these rules of
name scoping are possible.
• Nested Visibility
For example, in Java, the
No access to identifiers outside lifetime of all class objects is
their scope. from the time of their creation
• Nearest Declaration Applies (via new) to the last visible
Static name scoping. reference to them.
• Automatic Allocation and Deallocation Thus
of Locals ... Object O;...
Lifetime of data objects is creates an object reference but
bound to the scope of the does not allocate any memory
Identifiers that denote them. space for O.
You need
... Object O = new Object(); ...
to actually create memory
space for O.
© ©
CS 538 Spring 2008 48 CS 538 Spring 2008 49
© ©
CS 538 Spring 2008 50 CS 538 Spring 2008 51
Dynamic scoping makes type Virtual Functions
checking and variable access
harder and more costly than A function declared in a class,
static scoping. (Why?) C, may be redeclared in a class
derived from C. Moreover, for
However, dynamic scoping
uniformity of redeclaration, it is
does allow a notion of an
important that all calls,
“extended scope” in which
including those in methods
declarations extend to
within C, use the new
subprograms called within that
declaration.
scope.
Example:
Though dynamic scoping may class C {
seen a bit bizarre, it is closely void DoIt()(PrintIt();}
related to virtual functions void PrintIt()
{println(“C rules!”);}
used in C++ and Java. }
class D extends C {
void PrintIt()
{println(“D rules!”);}
void TestIt() {DoIt();}
}
D dvar = new D();
dvar.TestIt();
D rules! is printed.
© ©
CS 538 Spring 2008 52 CS 538 Spring 2008 53
© ©
CS 538 Spring 2008 54 CS 538 Spring 2008 55
Structs vs. Blocks Blocks and structs look similar,
but there are significant
Many programming languages, differences:
including C, C++, C#, Pascal
Structs are data,
and Ada, have a notion of
grouping data together into • As originally designed, structs
contain only data (no functions or
structs or records.
methods).
For example: • Structs can be dynamically created,
struct complex { float re, im; } in any number, and included in
other data structures (e.g., in an
There is also the notion of
array of structs).
grouping statements and
declarations into blocks: • All fields in a struct are visible
outside the struct.
{ float re, im;
re = 0.0; im = 1.0;
}
© ©
CS 538 Spring 2008 56 CS 538 Spring 2008 57
© ©
CS 538 Spring 2008 58 CS 538 Spring 2008 59
Type Equivalence in Classes Then we may not want
assignment to be allowed.
In C, C++ and Java, instances of
the same struct or class are class Point {
type-equivalent, and mutually int dimensions;
assignable. float coordinates[];
Point () {
For example: dimensions = 2;
class MyClass { ... } coordinates = new float[2];
}
MyClass v1, v2;
Point (int d) {
v1 = v2; // Assignment is OK dimensions = d;
coordinates = new float[d];
}
}
We expect to be able to assign Point plane = new Point();
values of the same type, Point solid = new Point(3);
including class objects. plane = solid; //OK in Java
© ©
CS 538 Spring 2008 60 CS 538 Spring 2008 61
© ©
CS 538 Spring 2008 62 CS 538 Spring 2008 63
class LinkedList { • We must use wrapper classes like
Object value; Integer rather than int (because
LinkedList next; primitive types like int aren’t
Object head() {return value;} objects, and aren’t subclass of
LinkedList tail(){return next;} Object).
LinkedList(Object O) {
value = O; next = null;} For example, to use LinkedList
LinkedList(Object O, to build a linked list of ints we
LinkedList L){ do the following:
value = O; next = L;}
} LinkedList l =
new LinkedList(new Integer(123));
Using this class, we can create int i =
a linked list of any subtype of ((Integer) l.head()).intValue();
Object. This is pretty clumsy code.
But, We’d prefer a mechanism that
• We can’t guarantee that linked lists
allows us to create a “custom
are type homogeneous (contain version” of LinkedList, based
only a single type). on the type we want the list to
• We must cast Object types back
contain.
into their “real” types when we
extract list values.
© ©
CS 538 Spring 2008 64 CS 538 Spring 2008 65
© ©
CS 538 Spring 2008 66 CS 538 Spring 2008 67