// GLOBAL DEFINITION
enum { FALSE,TRUE }; // FALSE = 0, TRUE = 1
typedef unsigned int Bool;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- */
// CLASS ENTITY
class entity {
private:
static char * classname; // class of object
protected:
char * name; // name of object
virtual void out_of_memory_exit(void * new_object);
char * make_name(char * NAME);
public:
entity();
entity(char * NAME);
virtual ~entity();
virtual char * get_classname(); // class of the object
virtual char * get_name(); // name of the object
virtual entity * get_ent(); // returns pointer of this object
virtual void print(); // print out the name of entity
virtual Bool equal(entity *ENT); // pointers comparison
virtual entity * equal_self(entity *ENT); // return *ENT if = *ent
virtual entity * equal_self(void *ENT); //
virtual entity * equal_name(entity *ENT); // same as above, use name
virtual Bool eq(char * name); // name fields comparison
virtual Bool eq(entity * ent); // name fields comparison
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- */
#endif
Note that the entity name is protected (so that it is inaccessible and inherited that way too). There is a classname that is useful for testing equality. It is declared to be static, meaning that is shared by all instances of the class. There are two constructors for entity, one of which takes the new entity's name as its argument. The destructor, ~entity(), frees up the memory space occupied by the instance for reuse. Print() and equal() are virtual since they are expected to be supplied by derived classes. All containers in HCCL use pointers to entity as the means to store objects. Therefore, to store an object it must directly or indirectly come from a derived class of class entity. Thus the key point to handling heterogeneous objects is to unify them under the umbrella of the entity class. In particular, since all of the classes in HCCL are derived from class entity, their instances may be stored in any HCCL containers. This leads to hierarchical construction, where containers may be placed into containers up to any level of recursion.
To illustrate:
class vehicle:public entity{
public:
virtual float fuel_efficiency(){..}
};
class motor_boat:public vehicle {
public:
float fuel_efficiency(){..}
};
class airplane:public vehicle {
public:
float fuel_efficiency(){..}
};
class truck:public vehicle {
public:
float fuel_efficiency(){..}
};
main(){
container * c = new container();
c->add(new motor_boat());
c->add(new airplance());
c->add (new truck());
for (element * p = c->get_head();p !=NULL; p = p->get_right()){
entity * e = p->get_ent();
vehicle * v = (vehicle *)e;
total = total + v->fuel_efficiency();
}
cout <<"Avg. Efficiency is: " << total/ c->size();
}
Note how the entities coming off a container must be cast into their
known classes in order to apply any methods specific to the class.