Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

2nd year engineering C++ Question

Options
  • 23-08-2009 10:33pm
    #1
    Registered Users Posts: 50 ✭✭


    Hi i have here the question and the sample solution provided by my lecturer my questions are below!

    you will see by the questions i about this that i am very confused when it comes to this program if you are willing to put in the effort and answer my questions i cant thank you enough!!
    5. Write the implementation (in C++) of the following class which is a based class for all other animal classes (such as cDog, cCat, etc.). 
    [I]class cAnimal [/I]
    [I]{ [/I]
    [I]private: [/I]
    [I]static cAnimal* mFirstAnimal; [/I][B][½ mark] [/B]
    [I]static cAnimal* mLastAnimal; [/I][B][½ mark] [/B]
    [I]static int mNumberOfAnimals; [/I][B][½ mark] [/B]
    [I]cAnimal* mNextAnimal; [/I]
    [I]cAnimal* mPreviousAnimal; [/I]
    [I]public: [/I]
    [I]cAnimal(); [/I][B][6 marks] [/B]
    [I]~cAnimal(); [/I][B][6 marks] [/B]
    [I]static cAnimal* GetFirstAnimal(); [/I][B][½ mark] [/B]
    [I]cAnimal* GetNextAnimal(); [/I][B][½ mark] [/B]
    [I]static int GetNumberOfAnimals(); [/I][B][½ mark] [/B]
    [I]virtual void PrintDetails() = 0; [/I][B][1 mark] [/B]
    [I]static void PrintDetailsOfAllAnimals(); [/I][B][4 marks] [/B]
    [I]}; [/I]
    [I][/I] 
    [I][/I] 
    [B]Sample solution: [/B]
    [FONT=Courier New]cAnimal* cAnimal::mFirstAnimal = NULL; [/FONT]
    [FONT=Courier New]cAnimal* cAnimal::mLastAnimal = NULL; [/FONT]
    [FONT=Courier New]int cAnimal::mNumberOfAnimals = 0; [/FONT]
    [FONT=Courier New]cAnimal::cAnimal() [/FONT]
    [FONT=Courier New]{ [/FONT]
    [FONT=Courier New]     mNextAnimal = NULL; [/FONT]
    [FONT=Courier New]     mPreviousAnimal = mLastAnimal; [/FONT]
    [FONT=Courier New]     if (mNumberOfAnimals == 0) [/FONT]
    [FONT=Courier New]     { [/FONT]
    [FONT=Courier New]      mFirstAnimal = this; [/FONT]
    [FONT=Courier New]     } [/FONT]
    [FONT=Courier New]     else [/FONT]
    [FONT=Courier New]     { [/FONT]
    [FONT=Courier New]      mLastAnimal->mNextAnimal = this; [/FONT]
    [FONT=Courier New]     } [/FONT]
    [FONT=Courier New]      mLastAnimal = this; [/FONT]
    [FONT=Courier New]      mNumberOfAnimals++; [/FONT]
    [FONT=Courier New]     } [/FONT]
    [FONT=Courier New][/FONT] 
    [FONT=Courier New][/FONT] 
    [FONT=Courier New]cAnimal::~cAnimal() [/FONT]
    [FONT=Courier New]{ [/FONT]
    [FONT=Courier New]   mNumberOfAnimals--; [/FONT]
    [FONT=Courier New]   if (mPreviousAnimal != NULL) [/FONT]
    [FONT=Courier New]   mPreviousAnimal->mNextAnimal = mNextAnimal; [/FONT]
    [FONT=Courier New]   else mFirstAnimal = mNextAnimal; [/FONT]
    [FONT=Courier New]   if (mNextAnimal != NULL) [/FONT]
    [FONT=Courier New]   mNextAnimal->mPreviousAnimal = mPreviousAnimal; [/FONT]
    [FONT=Courier New]   else mLastAnimal = mPreviousAnimal; [/FONT]
    [FONT=Courier New]} [/FONT]
    [FONT=Courier New][/FONT] 
    [FONT=Courier New]cAnimal* cAnimal::GetFirstAnimal() [/FONT]
    [FONT=Courier New]{ [/FONT]
    [FONT=Courier New]   return mFirstAnimal; [/FONT]
    [FONT=Courier New]} [/FONT]
    [FONT=Courier New][/FONT] 
    [FONT=Courier New]cAnimal* cAnimal::GetNextAnimal() [/FONT]
    [FONT=Courier New]{ [/FONT]
    [FONT=Courier New]   return mNextAnimal; [/FONT]
    [FONT=Courier New]} [/FONT]
    [FONT=Courier New][/FONT] 
    [FONT=Courier New]int cAnimal::GetNumberOfAnimals() [/FONT]
    [FONT=Courier New]{ [/FONT]
    [FONT=Courier New]   return mNumberOfAnimals; [/FONT]
    [FONT=Courier New]}[/FONT]
    [FONT=Courier New] [/FONT]
    [FONT=Courier New]void cAnimal::PrintDetailsOfAllAnimals() [/FONT]
    [FONT=Courier New]{ [/FONT]
    [FONT=Courier New]   cAnimal* current_animal = GetFirstAnimal(); [/FONT]
    [FONT=Courier New]   while (current_animal != NULL) [/FONT]
    [FONT=Courier New]   { [/FONT]
    [FONT=Courier New]   current_animal->PrintDetails(); [/FONT]
    [FONT=Courier New]   current_animal = current_animal->GetNextAnimal(); [/FONT]
    [FONT=Courier New]   } [/FONT]
    [FONT=Courier New]}[/FONT]
    

    Im at the stage where i understand the syntax of everything like i can say oh theres a pointer etc etc but im so poor at the logic like i wouldnt know why a pointer is used in certain situations im reading alot and trying to get better however this question even with the solution has me stumped!

    my questions are;

    1.in the line "static cAnimal* mFirstAnimal;"
    mFirstAnimal is a pointer of type cAnimal even though the definition of cAnimal isnt even finished, how is this?

    2.how can an object of the class cAnimal be a pointer? is it that it points to other cAnimal objects fair enough but what is this like why has it to be a pointer?

    3.if you declare an object of type cAnimal within the class cAnimal does it always have to be a pointer ?

    4. I have no clue what the constructor and destructor is supposed to be doing? Can you explain them to me?

    5. And the program as a whole whatis the point in it? i dont understand what its trying to achieve like if it hads stuff like itsAge GetAge(); etc id understand how it could be a base class for animals?

    help is very much appreaciated!!


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    OP,

    I had every question answered, ready to submit but I thought to myself, I'm doing you no favours. This problem right in front of you here is really basic and the logic behind everything is almost spelled out to you. The variable names for one thing, should give you an idea.
    I think you, like a lot of other students have a mental block. You believe it is impossible. You look too closely to detail trying work out what each thing is done and get confused.
    You need to step back and look at the whole picture.

    You have an object cAnimal...we have all these features, such as age, eye colour etc. These are unique to the animal. But the code above is not bothering with this (It's probably assumed you already understand this stuff). The code above is a structure...a list of animals...each pointing to eachother in sequence. I'm not giving anymore now. The code spells this out to you.

    I'll give you few hints here:

    A class is a blue print of an instance of an object. For example, we have a "Human" blue print/class definition. From this, we are all constructed (constructor function!!!). Say we want to refer to our brothers and sisters. We should use a pointer of type "Human" to refer to these. Now think of what you are saying when you say "how can we have a poitner to something that isn't finished". What's finished? the blue print / class is always there. No problem refering to other humans in the human class.

    What if someone dies? - they are distructed (maybe shouldn't have used humans here :) ) - something happens when we die. Our body shuts down...Code for doing this is in the destructor. It is cleanup code for objects.

    What you need to research now is:

    1. Difference between a class definition and an object
    2. What a pointer is. What's it purpose. Why do we use pointers! They not there for the fun of it.
    3. Constructor and destructors


  • Registered Users Posts: 50 ✭✭Robbie12


    Can i maybe get another hint from you please? i know what a constructor and destructor do just not what this cAnimal constructor and destructor do? i know its basic stuff and i really wish i could get over this mental block your right thats exactly what it is it seems so illogical to me when what the whole thing is based on is logic! I have arepeat exam in it and ive been reading thomson C++ programming to no avail i can tell you everything about pointers just dont know where to use them or what the hell there doing when i see them its odd!

    Em also are you saying this class is a list or a linked list because this is something i havnt read yet ? would you need to know about lists to answer this?


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    Robbie12 wrote: »

    1.in the line "static cAnimal* mFirstAnimal;"
    mFirstAnimal is a pointer of type cAnimal even though the definition of cAnimal isnt even finished, how is this?

    2.how can an object of the class cAnimal be a pointer? is it that it points to other cAnimal objects fair enough but what is this like why has it to be a pointer?

    3.if you declare an object of type cAnimal within the class cAnimal does it always have to be a pointer ?

    4. I have no clue what the constructor and destructor is supposed to be doing? Can you explain them to me?

    5. And the program as a whole whatis the point in it? i dont understand what its trying to achieve like if it hads stuff like itsAge GetAge(); etc id understand how it could be a base class for animals?

    help is very much appreaciated!!

    I am actually going to answer this is accurately as I possibly can because I believe you are making an effort to understand a solution you have been given.

    1. The reason the pointer "static cAnimal* FirstAnimal" can be declared is because pointers are a uniform size. Pointers are always just a number (memory address) and so in order for the compiler to allow a declaration like above all it has to have seen before is the declaration of the class, i.e. "class cAnimal".

    2. An object of class cAnimal is not a pointer. The way to read it is: Pointer to an object of type cAnimal. Think of a zoo, there is a sign saying "Larry the Lion - The lion enclosure 200 meters ahead". That sign is the equivalent of a pointer to a cAnimal. It is something we can look at that can show us the location of Larry The Lion. It is nothing more than a sign.

    3. No it doesn't. Pointers are just the simplest way for one object to be able memorize where it can locate another object.

    4. I'm assuming you mean theoretically what the constructor and destructor are supposed to be doing? They are used to make a destroy new objects. So the class is the outline of the object, what the constructor is saying is that, any time someone wants to create a new Animal, they can provide a name/age etc. and the Animal will be created with those attributes applied.

    The destructor is the place where an Animal is being deleted or forgotten about. Any memory it has used for its attributes are removed here and if there is a static object counter like HowManyAnimalsInTheZoo, here is where you would decrement that number because the destructor is called any time an Animal leaves the zoo.

    Jesus I've realised just how bad I am at explaining things, so I really hope it helps ye. If ye get confused by what I've said, stop reading :)


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    Robbie12 wrote: »
    Em also are you saying this class is a list or a linked list because this is something i havnt read yet ? would you need to know about lists to answer this?

    Effectively it is list of animals, because any time an animal is created, it is linked to the other animals through the nextAnimal and previousAnimal pointers.

    Constructor adding this new animal to the list.

    Animal1 -><- Animal2 -> <- this new animal (each arrow indicates a previous or nextAnimal pointer).

    So each animal has a 'link' to the animal that was created before it and the animal that was created just after it.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Jesus I've realised just how bad I am at explaining things, so I really hope it helps ye. If ye get confused by what I've said, stop reading :)
    Better than me Jimmy :pac:


  • Advertisement
Advertisement