Class / struct question for objects

Started by
11 comments, last by paulcoz 24 years ago
Hi Paulcoz,

Sorry about my previous attempt at explaining... it was a bit all over the place

What SiCrane meant about being unable to instantiate and object of class Object is that using the calls:


Object *thing; //this line is OKthing = new Object();thing->Render(); 



... would be invalid, as it is an abstract class (defined only for the purpose of unifying some subclasses). It doesn''t make sense that an object of this type can, for example, draw itself. What does an Object look like?

You can, if I remember correctly, have a pointer to this abstract class, you just can''t create an object of that class.

Now, this is useful in that you can have an array of pointers to ''objects'' of this abstract class, but assign objects of subclasses to these pointers:
NOTE: I''m using parentheses for array subscripts, as this message board uses the square brackets for stuff.


Object *things(3);thing(0) = new Door();thing(1) = new Wall();thing(0)->Render();thing(1)->Render(); 



... this is valid, because Door and Wall are not abstract classes and are subclasses of Object. The beauty of this is that you can iterate through your array calling the Render() method for each object, and polymorphism/function overloading takes care of determining what type of object (subclass) it is dealing with, and calls the appropriate function code for that class.

This leads me to answer the next part of your question. Each subclass of Object (staying with the established example) can have any methods they want, but they must implement all virtual methods defined in the superclass (Object). The way they implement it is, however, entirely up to you. For example, to implement the Render() method for a Door object, you write your drawing code to render a door. Now obviously it takes different code to render a wall (OK, maybe not that different), so you would write a different implementation of the Render() method for the Wall class:


class Door : public Object{public: Door();void Render();};void Door::Render(){drawPanel;drawKnob;}class Wall : public Object{public:Wall();void Render();};void Wall::Render(){drawBricks;} 



Do you see what I mean. Each class has its own way of providing an implementation for the virtual method/s of the superclass (Object) that is relevant to an object of that particular class .

Sorry if I just repeated what you already knew. Hope that was a bit more helpful than my last try



-------------
squirrels are a remarkable source of protein...
Advertisement
Goddamn server screwed it up. That''s my post above with the squirrel message at the end.

-------------
squirrels are a remarkable source of protein...
Thanks a lot folks, I know I should read more about these concepts before posting, but then again I didn''t know that classes could do that sort of thing when I posted. I have been ''clinging'' to my procedural programming skills for as long as I can, but it looks like now is the time for me to learn something new - those virtual functions look great.

Your help has been extremely useful & Bad Monkey your first explanation wasn''t bad, I was just that I had a very limited knowledge of the subject, thankyou.

Paulcoz.

This topic is closed to new replies.

Advertisement