#ifndef _CONTAINER_H_
#define _CONTAINER_H_
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
class container : public entity {
private:
protected:
int length; // record the length of the linked-list
element * head; // first element of list in container
void add_first(element * el); // add element at the first position
of the list
void add_at_head(element * el); // insert element at head of list
void add_element(element * el); // add element to list
void set_head(element * el); // let el be the head element of list
public:
container();
container(char * NAME);
~container();
void add(entity * ENT); // add object to the container
void add(char * NAME); // add object to the container
void print(); // print all names of entities in the container (recursively)
container * names(); // create new container with names of objects
element * get_head(); // get the head element
int get_length(); // get the length of list
int size(); // same as get_length()
Bool empty(); // is the container empty?
Bool is_in_name(char * NAME); // is object with the given name in the
container?
Bool is_in(entity * ENT); // is this entity in the container?
void append(container * c); //add contents to this container
};
#endif
Here is an example of using the container class:
int main() {
entity * e1 = new entity;
entity * e2 = new entity;
entity * e3 = new entity;
container * con = new container;
// print out whether container, con is an empty container.
// If the value is 0, then con is not empty.
// If the value is 1, then con is empty.
cout << "Is con an empty container? " << con->is_empty() << endl;
con->add(e1); // add object e1 into con
con->add(e2); // add object e2 into con
con->add(e3); // add object e3 into con
// Is con still empty?
cout << "Is con an empty container after adding 3 objects into it? " << con->is_empty() << endl;
// print the size of con
cout << "Size of con: " << con->size() << endl;
return 0;
};
The output results are:
Is con an empty container? 1 //1" means "True" and "0" means "False".
Is con an empty container after adding 3 objects into it? 0
Size of con: 3