Solved

Container Classes and Linked Lists

Posted on 1998-08-07
15
277 Views
Last Modified: 2010-04-10
My question regards containers (container classes), when used as a linked list.

Given the following:

A Zoo is made up of animals and buildings.
The ability to add objects, animals or buildings, is needed.
To diplay a "walkthrough" of the Zoo, a printing member function is needed.
An entrance and an office are in the front of the Zoo and a first-aid station in the
rear of the Zoo.

Lion Leo, open, eats at 7:00 and 3:00, fresh meat
Panda - Fred, open eats at 12:00, bamboo
Brown Bear - Yogi, closed, eats 9:00AM, picnic baskets

Zoo animals are individual classes derived from a base class called ZooAnimal
which is derived from ZooStuff. Each derived class should print out what
it is, its name, if its open or closed, where it is located and when it eats.

A Zoo is a container(linked list) of ZooStuff items, arranged in a specific
order such that when you traverse the list, you are walking through the Zoo.
The container maintains a linked list of ZooStuff things, each of which can be
some type of derived ZooStuff object.
When walking through the Zoo, invoking print() on each ZooStuff item
should cause it to print its repective description, etc.

Here is the code "skeleton" I have thus far...........................................

*************************************

class ZooStuff  {

public:

      virtual void talk() = 0;  // this function explains the location
      void walk(); // print out a walk
      // void add_element( ZooStuff an_element );
      ZooStuff();
      ~ZooStuff();

protected:

      int location;
      char *IsOpen;
      ZooStuff *next, *prev;

};


// ZooAnimal - derived from base class ZooStuff
class ZooAnimal : public ZooStuff {

public:
      void talk();
      void timeOpen();
      void timeEats();
      void what_it_eats();
   // void add_element( ZooStuff an_element );

private:

      char type_of_animal;
      char food_of_preference;

};

class Lion : public ZooAnimal {

// Lion class derived from ZooAnimal, in turn derived from base class
// ZooStuff


};

class Panda : public ZooAnimal {

// Panda class derived from ZooAnimal, in turn derived from base class
// ZooStuff

};

class BrownBear : public ZooAnimal {
// BrownBear class derived from ZooAnimal, in turn derived from base class
// ZooStuff

};

class BengalTiger : public ZooAnimal {
// BengalTiger class derived from ZooAnimal, in turn derived from base class
// ZooStuff

};

class Fox : public ZooAnimal {
// Fox class derived from ZooAnimal, in turn derived from base class
// ZooStuff

};


And, the current implementation "skeleton"...............
************************************

int main()
{

 ZooStuff *zoo;
 // Bear b(...);
 // zoo->add_element(b); // make b the 1st location in the zoo
 // Building c(...);   // add a building to the end of the zoo
 // zoo->add_element(c);  // make c the 2nd location in the zoo
 // zoo->add-element(new fox("clyde","9 to 5",open));
 zoo->walk();  // ???



return (0);
}

void ZooStuff::walk()
{
// code here is to do a complete showing of a "walk" through the Zoo. ???
cout << "walk; from: ZooStuff::walk()" << endl; // for testing ONLY
}

ZooStuff::ZooStuff()    // CTOR
{
}

ZooStuff::~ZooStuff()  // DTOR
{
}

void ZooAnimal::talk()
{
// Add code here to display talk of particular zoo animal ???

}
void ZooAnimal::timeOpen()
{

// Add code here to show the time that a particular zoo animal opens
// for viewing ???
}

void ZooAnimal::timeEats()
{
// Add code here to display the time at which a particular zoo animal eats ??

}
void ZooAnimal::what_it_eats()
{
// Add code here to show the preferred food of a particular zoo animal ???
}


*********************************************************

OK. I understand the bare basics of linked lists and I think I understand
class derivation enough thus far. But, this program focuses on the use
of containers and/or container classes as well as operator overloading and data
hiding. I don't understand containers very much at all and I understand
operator overloading only a little better.

QUESTION:

Could someone please point out/explain the use of containers and container
classes as they need to be used in the above program? I need some direction.

S
0
Comment
Question by:swright243
  • 9
  • 6
15 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 600 total points
Comment Utility
answers coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
The heart of the problem is that you are trying to use one base class ZooStuff and derive everything from that.  The zoo itself should not be derived from ZooStuff.  Think of ZooStuff as something in the zoo, like an animal or building.  but the Zoo is not in the zoo, it is a container of the things that are in the Zoo.  So you will need a class for the Zoo that is not derived from the ZooStuff class.  Makes sense?

This class, I'll call it ZooClass, will contain the linked list of ZooStuff items.  Thus it will need a pointer to the head of the list.  it will also need functions for adding (and deleting?) ZooStuff items to the Zoo.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
the class might look like this.  

class ZooClass
{
   ZooStuff *LstPtr; // -> start of linked list of things in the zoo.
public:
   void add_element(ZooStuff  *NewItem );
   ZooClass() : LstPtr(NULL) {};
   ~ZooClass() {} ;  // You may need to do some things in the destructor.  
                                // We'll worry aobut that later.
}

Note it has a constructor that initalizes the zoo to have an empty list.  It has a function to add items to the list.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Now for the ZooStuff class.  Remember this class represents the things found in the zoo, that is, the things that get stored in the ZooClass container.

A few little points.  Make the ZooClass a friend.  That way it can manipulate the next and previous pointers when a ZooStuff object is added to the ZooClass linked list.
also make the destructor virtual.  That is just a good idea for any class hierarchy.
I removed walk().  I'm guessing that is for walking through the Zoo and therefore belongs in the ZooClass, not the ZooStuff.
I removed the add_element because that is part of the Zoo class, (you had it commented out, so you may have been thinking along those lines)
What is IsOpen() for?  does that apply to buildings only?  or to all ZooStuffs?

Here is the class as I see it, I'm sure there will be changes still.

class ZooStuff  
{
public:
   virtual void talk() = 0;  // this function explains the location
   ZooStuff();
   virtual ~ZooStuff();

protected:
   int location;
   char *IsOpen;
   ZooStuff *next, *prev;
   friend ZooClass;
};
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Your main() is a mess.  But lets ignore that for the time being.  Concentrate on getting the classes together, then worry about the code that uses them.

Hopefully, that is enough to get you moving a little more.  I'm not 100% sure I understand everything you are trying to do and everything you need to do, so I haven't tried to get every detail at once.  But I focused on the core things.  The next thing you should do is work on the procedure in the ZooClass that adds items to the zoo.  Also the procedure that goes through the items and prints them.  (I forgot that in the class definition I wrote).  If you can do those two things (or try at least) that will be a great start.

Something that would help me is an idea of what some of these functions do.  Like the functions that deal with food and eating times and things.  

If you want to e-mail me instead of posting code here.  My e-mail address is nietod@theshop.net.  I'm done for the day, but will start again at about 7:00 AM tomorrow, that is USA central time.
0
 

Author Comment

by:swright243
Comment Utility
Thanks nietod for your help on these 2 projects. I'm not
a programmer and learning this is hard for me. So many things
going at once, I panic.

I can get more clarification from my instructor but what you have
offered above seems to be on target. I will get some more info
from him as apart from the following, I don't know fully what he wants.

"The Zoo is a container (linked list) of ZooStuff items, arranged in a specific order such that when you traverse the list, you are
walking through the Zoo. The container maintains a linked list of
ZooStuff things, each of which can be some type of derived ZooStuff object. When walking through the Zoo, invoking print()
on each ZooStuff item shoulf cause it to print its respective
description, etc."
That was his reponse to an Email I had sent.

Also, the assignment displayed an example hierarchy......
                 ZooStuff
         
                     |
               ----------------
                    ||

            ZooAnimal  ZooBuildings
      ----------------------------------

                    ||
   ----------------  ---------------------

                   ||||
        Bear  Feline  Gates   Offices

The execution of this program will "simulate" a walk through a
simple zoo. This can be thought of as a long corridor with animals and zoo buildings that you come upon, one after another.
The project should use inheritance, virtual functions, appropriate data hiding techniques, operator overloading.

The following is the minimum that is required for the ZooStuff
class:
class ZooStuff {
public:
virtual void talk() = 0; // this function explains the location
void walk();  // print out a walk
ZooStuff();
~ZooStuff();
protected:
int location;
char *IsOpen;
ZooStuff *next, *prev;
};

As far a the main() being a mess, that is just an example of
what the main() program could look like.

I could do Email, but I want to award a person of your level of
knowledge enough points for all of your time and I'm really
short on cash and trinkets right now. :) I have purchased a large
chunk of points though.

S.  

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I'm not sure where things stand.  Do you still need help?  Were you able to make the changes I suggested?  In particular, making the Zoo a seperate class (ZooClass?) from the ZooStuff class?  We're you able to go on after that or are you stuck again?

If you still need help, it might be a good idea to e-mail me the code, rather than post it here.  As the code gets long, it gets very hard to read here and it gets very slow to load the page.

A couple little things still:

Are you sure that walk() is part of Zoostuff?  I think it might be part of the zoo class.  That is, you walk through a zoo and see the things (zoostuff) in the zoo.  You don't walk through the things in the zoo--otherwise you get eaten.

How does location work?  Is it just that each location is assigned a different number, or is there something else?  Does the walk go through the zoo in increasing location number?

What does isOpen() return?

What do TimeOpen() and TimeEats() do?  They take no parameters and return no values, that seems suspicious.


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:swright243
Comment Utility
Hi nietod;

I'll need to Email my instructor to get some more clarification for the questions you
ask.
I think the Email is best, as EE doesn't seem to have a text only option and it gets
difficult (and foolish) to print out EVERY page, EVERY time.

swright243@aol.com
0
 

Author Comment

by:swright243
Comment Utility
08/23/98
0
 

Author Comment

by:swright243
Comment Utility
09/05/98
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
9/25/98
0
 

Author Comment

by:swright243
Comment Utility
Adjusted points to 500
0
 

Author Comment

by:swright243
Comment Utility
Hi Todd -

I hope this is a fair amount of points.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
That's fine.  But things were not finished...Did you hand them in as is?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Did you adjust the points again?  On Sept 29th it said you adjusted to 500.  But the current value is 60.  If it was at 500, it should be 50 now.  If you didn't change it to 600, let me know and I'll let them know there is a problem
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now