• Aucun résultat trouvé

To Order, Call Toll Free: 1-800-622-6435

Dans le document How To Write And Use (Page 31-35)

Sot'Solutions 440 Quentin Drive San Antonio, TX 78201

Trademarks: Norton GuideslPeter Norton Computing. dBASE '" Plus IV/Ashton·Tate. ClipperlNantucket, dBXL OUicksilverlWordtech Systems. FoxBASE+iFox Software. On LinelSofSolutions

Reader Service Number IDS·

MICRO CORNUCOPIA, #47, May-June 1989 29

struct with a few extras. A function in-side a class definition gets treated just like any other structure member; you dereference a member with a dot (for a structure variable) or an arrow (for a structure pointer).

Members can be public (available to everyone) or private (only member func-tions can change or use them). A struc-30 MICRO CORNUCOPIA, #47, May-June 1989

ture in C++ is a class with all members public (so a structure can have member functions).

All classes and structures have special functions called constructors (a member function with the class name) I called when a variable (object) is created, and a destructor (a tilde followed by the class name), called by the compiler when an

object goes out of scope.

Inheritance & Member Objects The controller uses two object-oriented concepts: "inheritance" and

"member objects." Let me explain this further.

There are two ways you can use an old class to make a new class in C++.

-•

•••••••••••••••••••••••••••••••••••

'}'/"

il I,,,,

L """

"",., .,.,. ,"i .\ ;7 ",

•••••••••••••••••••••••••••••••••••••••••••••

\~2

'\'"

ii'''~11k'

...•..•..••.•

:

•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

~ ~

.••••••.

~

~

•.•••••••••••••••••

~

••••••••••••••••.

.•.••••••...••... ' .•••••••.••••..

'YY?~j

. ~ ••••••••• ~ •••.•••• ,~££~;~~~! •••••••. ~ •••••••••

.~

••••.•.••••••••••••••••••.•.•.•.•••••.•.•••••••.••••••••••••••..

,/I~f:!!lliil

", •• flL.ilfJILAtlJ

One (probably the one you've heard about) is "inheritance." When you inherit a new class from an old one, you make a new version of the old class. You can also use a member object in a class. A mem-ber object is simply another class ele-ment.

When you use an old class to make a new one, you must initialize the old class by calling the constructor. You call the constructor for the base class (if you're inheriting) or the member object before the code for the new class constructor ex-ecutes. To show this, c++ syntax has the constructor calls after the closing parenthesis of the argument list, but before the opening brace of the construc-tor function body. For

example-class old int i;

public:

old(int j = 0) { i = j;

} ;

class meIn_ob int i;

public:

meIn_obj(int j

=

0) { i

=

j;

} ;

class derived : public old mem_obj member_object;

public:

derived(int p

=

0, int q

=

0)

(p) , member_object(q) {}

} ;

Class derived is inherited from class old and contains an object of the class mem_obj. There's no object name as-sociated with the base class, so no name is used to call the base class constructor.

Object-Oriented Event Control CONTROLR is "truly object-oriented." It manipulates generic objects . Each object belongs to the general base class, "event" (see the file EVENT.HXX, Figure 3), and to a specific derived class that has special properties in two virtual functions, descriptionO and actionO. A virtual function is declared in the base class and defined in the derived class, so two objects can have the same interface but different implementations.

We keep objects of the base class in a list and readyO constantly checks them to see if they're ready to run. Here, readyO only ties to the clock. But you can tie it to hardware .

For hardware events, make readyO a virtual function which defaults to check-ing the clock. When derivcheck-ing a new event, change the definition of readyO so that it checks the state of some hardware

MICRO CORNUCOPIA, #47, May-June 1989 31

instead of the clock - very simple.

When an event is ready, its actionO is performed and the event gets removed from the list.

A Class To Manage Time

Class event contains a member object called evenCtlme which belongs to class time_point, defined just prior to class event. A time_point object holds a single point in time.

Points can be assigned, added, com-pared, and randomized (Le., increased by a random amount of time or bounded by a random factor). Once you've defined class time_point, you can ignore the details of time calculations.

Each object in class event relates to a single time_point. When the clock time equals or exceeds the event_time, the member function, readyO, returns true.

So you can ask an event if it's ready to run.

To create a new type of event, inherit a new class (from event). Then if you want, redefine the virtual functions, de-scriptionO and actionO. If you don't they default to their definitions in the base class.

The process of inheriting a new class changes only in name from event to event. So the code to inherit a new sub-type is packaged in the macro EVENT_TYPE, defined in EVENT.HXX, and used in CONTROLRCXX. Notice that you can continue a macro as long as you keep putting backslashes at the end of each line.

Design Guidelines

When designing an object-oriented system, it's important that your concept of the object encompass all the object's possible uses. In this system, for example, an actionO can be anything, including re-starting the system (see EVENT_ TYPE (system_restart) in CON-TROLRCXX). Notice also the flexibility of the system; the action of one event can add other events to the list, as shown in EVENT _TYPE(bell).

The system restart commands are handy

because-(1) you may want to create a process which repeats itself;

(2) and there may be a power failure where you'll want the controller to resyn-chronize itself.

A C++ Oriented Linked List

Completely object-oriented programs manage an arbitrary number of generic objects (from classes which use polymor-phism, aka virtual functions). A linked list seems' to be the best way to handle 32 MICRO CORNUCOPIA, #47, May-June 1989

these objects, so I've been trying to create a better linked list for C++.

My best try so far, the list in EV-LIST HXX (see Figure 4), uses the unique features of C++ to advantage, in particu-lar constructors, destructors and static class variables.

Many linked lists define a link ele-ment, and then a "container" to manage the links. The class event_el in EV-LIST.HXX contains itself by using a static class variable for the head pointer (the pointer to the beginning of the list). A

static class variable allows all objects in a class to share common information economically. Space for only one variable of that name is defined, and all objects share the same data space for the varia-ble.

As in C, you use a global static class variable with a hidden name known only to members of the class (assuming a pri-vate element).

Unlike C, you shouldn't initialize static class variables when they're de-clared. Initialization tells the C++

transla-

---CP/M, NorthStar, Macintosh, Apple II, MS-DOS, and PS/2

Dans le document How To Write And Use (Page 31-35)

Documents relatifs