01.05.2013 Views

[P] Programming

[P] Programming

[P] Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4.6 Scope<br />

class — Class programming 35<br />

In the examples we have seen so far, the member variables are unique to the instance. For example,<br />

if we have<br />

.coord1 = .coordinate.new<br />

.coord2 = .coordinate.new<br />

then the member variables of .coord1 have nothing to do with the member variables of .coord2.<br />

If we were to change .coord1.x, then .coord2.x would remain unchanged.<br />

Classes can also have variables that are shared across all instances of the class. Consider<br />

begin coordinate2.class<br />

version 11<br />

class coordinate2 {<br />

classwide:<br />

double x_origin = 0<br />

double y_origin = 0<br />

instancespecific:<br />

double x = 0<br />

double y = 0<br />

}<br />

end coordinate2.class<br />

In this class definition, .x and .y are as they were in coordinate.class—they are unique to<br />

the instance. .x origin and .y origin, however, are shared across all instances of the class. That<br />

is, if we were to type<br />

.ac = .coordinate2.new<br />

.bc = .coordinate2.new<br />

there would be only one copy of .x origin and of .y origin. If we changed .x origin in .ac,<br />

.ac.x_origin = 2<br />

we would find that .bc.x origin had similarly been changed. That is because .ac.x origin and<br />

.bc.x origin are, in fact, the same variable.<br />

The effects of initialization are a little different for classwide variables. In coordinate2.class,<br />

we specified that .origin x and .origin y both be initialized as 0, and so they were when we typed<br />

“.ac = .coordinate2.new”, creating the first instance of the class. After that, however, .origin x<br />

and .origin y will never be reinitialized because they need not be recreated, being shared. (That<br />

is not exactly accurate because, once the last instance of a coordinate2 has been destroyed, the<br />

variables will need to be reinitialized the next time a new first instance of coordinate2 is created.)<br />

Classwide variables, just as with instance-specific variables, can be of any type. We can define<br />

begin supercoordinate.class<br />

version 11<br />

class supercoordinate {<br />

classwide:<br />

coordinate origin<br />

instancespecific:<br />

coordinate pt<br />

}<br />

end supercoordinate.class<br />

The qualifiers classwide: and instancespecific: are used to designate the scope of the<br />

member variables that follow. When neither is specified, instancespecific: is assumed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!