Recommended Posts

Hi,
What reasons are there for me learning to use advanced classes and methods like Polymorphism and Inheritance. What advantages are there when programming them along side DX compared to just using Singletons?
regards,
Ace

0

Share this post

Link to post

Share on other sites

Advertisement

Guest Anonymous Poster

Why have pants when shirts exist? It is because they do different things. Every programming construct exists because someone thought that it would be useful to him. Will it be useful to you? Maybe not, maybe you are not working on a similar problem or in a similar setting.

Share this post

Link to post

Share on other sites

quote:Original post by ace_lovegrove What reasons are there for me learning to use advanced classes and methods like Polymorphism and Inheritance....

Polymorphism is just the RIGHT way to do things.

Basically, it means you can write code which handles generic objects without worrying about their exact behaviour or state details.

If you have a class Player and another class Monster - they have some behaviour and state in common. For example they might both have a hitpoints property, and perhaps a move() and draw() method.

Your manager routine doesn''t need to worry about whether a specific object is an instance of Player, Monster, or something else that you hadn''t yet invented when you wrote them, it will just do the right thing when it asks them to move, draw, or their hitpoints.

You will need to write new code in fewer places when you add a new type of object to your game. Code for handling that class will all be in one place.

Share this post

Link to post

Share on other sites

with respect to abstraction, polymorphism and inheritance - they are almost always used for one (or both) of two reasons:

1. It makes for a simpler, more concise solution to the problem

2. It''s extensible, allowing for added functionality without any major changes to the bulk of the existing code.

Short of writing a small essay I doubt that anyone here can answer your problem. I suggest you find some tutorials on OOP (Object Oriented Programming) or some of the universities CS lecture slides on OOP/PRG type modules.

hthJack

0

Share this post

Link to post

Share on other sites

a good reason for polymorphism is so you can have all kinds of objects like Enemy, Monster, Bullet, Player, etc and do things like have a list of pointers to them all in one place to do specific common things. for intsance all of them perhaps extend a PhysicsBase object so that they physics engine can just keep a list of pointers to them (PhysicsBase * physObjs). then it can just iterate through them and call the common collision functions or something. at the same time your graphics engine can have a list of something like (DrawableObjs * drawObjs then it can iterate through them and call the common Draw() function so that each object can draw itself. there are many many reasons to use polymorphism. it''s one of the most powerful tools in the C++ tool chest.

Inheritence again is another powerful tool with which you can inherit common properties of objects. for instance, every object in your world will need a matrix to hold it''s unique position & orientation. why code 30 different classes each with their own matrix member var (Enemy, Monster, Bullet, etc) when you can have a single base class with the matrix var and other common things.

Basically you are asking why is Object Oriented design useful. This question can best be answered by learning what OO design is, and then you should see why it can be useful. It''s obviously not the only way to do things. equally powerful engines have been written with procedural design. it''s a tool that has some very powerful uses. it''s a good thing to learn about.

-me

0

Share this post

Link to post

Share on other sites

quote:Original post by markr Polymorphism is just the RIGHT way to do things.

No, just A way to do things. you can write equally powerful and concise apps using either OO design or procedural design. sometimes OO is more useful, sometimes procedural is more useful. they''re both just tools that you can use to solve problems. neither is inherently better than the other.

For instance, AFAIK, no id software engine has so far been written with OO design. I doubt you can claim that their engines are "wrong".

Link to post

Share on other sites

Guest Anonymous Poster

just to be picky I thought that Carmack switched the graphics engine to C++ around the Quake-3 build. Whether that was just "C with a C++ compiler" or proper OOP C++ I dunno.

Jack

To be honest I don''t think there have been many (any ? LCC maybe) widely used pure C compilers - I thought since the late 1990''s the situation has been that C is conventionally compiled on a C++ compiler in a pure C mode, which I assume is what you''re insinuating.