Topic: c++ object creation


Author: nagy@cpsc.ucalgary.ca (Michael Nagy)
Date: Thu, 17 Feb 1994 01:00:29 GMT
Raw View
First of all, I should say I am using Borland's C++ v3.1

This may seem like a silly question when you've been working
with objects for some time, but I'm having some problems creating
a class structure.

Here's a stripped down example of what I am trying to do.

Take a game for example.  A character can have a list of "items" in
his/her possession, each referred to as a generic Item

class Item
{
}

Now, in the "character" class, I want to be able to refer to all
these things simply as an Item

class Character
{
Item *Inventory;
}

However, what if each Item is of a different type, like "armour", or
"weapon" or "other".

class Armour
{
int Defense;
int CalcDamageTaken(int Damage);
}

class Weapon
{
int Offense;
}

How can I declare Item (or Armour and Weapon) so it would allow
me to treat them all simply as Items?  I realize that I would probably
have to put a marker in the sub-objects (armour and Weapon) to mark
them as such when I had to refer back to them.

Any help would be appreciated.  Mail directed to nagy@cpsc.ucalgary
is preferred, but any response would be appreciated.

Michael

P.S. Stupid example?  Perhaps, but at least it works!
--
===============================================================================
"Good evening. Here is the news on Friday the 27th of Geldof.
Archaeologists near Mount Sinai have discovered what is believed to be a
missing page from the Bible. The page is presently being carbon dated in
Bonn. If genuine, it belongs at the beginning of the Bible and is believed
to read, 'To my darling Candy. All characters portrayed within this book are
ficticious and any resemblance to persons living or dead is purely
coincidental.' The page has been universally condemed by church leaders."
    - Groovy Channel 27 News   "Red Dwarf: Better Than Life"
===============================================================================