Potentially stupid newbie question

Mr. Red garfieldrules at sbcglobal.net
Fri Aug 1 13:12:05 PDT 2008


Hi.  You might say I'm kind of a greenhorn programmer... I had just begun to settle into C++ when I found out about D.  As such I understand the basics of OOP, inheritance and that kind of thing, but there's one thing I can't figure out.  Say I have a base class Cat.  Then I have derived (non-nested) classes Tiger, Leopard, and Lion, all just derived from the base class Cat.  Then in my program, I want to have an array of Cats to keep track of them all, and at any one point in the program the exact species could change.  Now, I've tested and found that:

Cat cat[10];

cat[0] = new Tiger();
cat[1] = new Leopard();
cat[2] = new Lion();
cat[3] = new Lion();

...
And so on and so forth, compiles, but I'm not able to access the member functions of the derived class, even if I've set them to public.  What would I do if I wanted to access the member functions?  I know that this is probably something simple... Is there perhaps a better way to do what I'm trying to do then the array approach?  

My compiler is GDC on OS X.



More information about the Digitalmars-d mailing list