C++ newbie question
1) I am a newbie to game programming, and C++
2) I am pretty familiar with c, and I''m trying to understand the differences between C and C++ before I start jumping into game dev (I figure start small
Anyways, Inheritance. What does it buy you? I understand the power of a class, being a c programmer, I can certainly appreciate the power of a structure with functions in it. But the whole inheritance thing, I just don''t get it. Ok I can derive classes from base classes and change some functions so they behave differently - but just to keep things in focus as I study - where does the huge advantage from inheritance come from? Do directX functions use it regularly and thus I should understand it? Or is it common to actually use inheritance all over your game. Can somebody give me a general idea of inheritance''s power? - because I think I''ve missed the boat...
Thanks
White Rider
This is a rather large topic, and since I''ve already responded to someone many many months ago on the uses of derived classes with virtual functions I''m just gonna paste it here:
...
It''s hard to understand how powerful polymorphism (derived classes with virtual functions) is without a real world example, so here goes. Warning, this might be long
Say you are writing program that the user will need different kinds of shapes (square, circle, triangle, etc) that you will keep in an array. These shapes need to be created, drawn, and printed. You could go about it by creating 3 different classes, one for each shape:
This is all fine and dandy, except you will need 3 different arrays, one for each class type. You could go a different route and create one class that contains all shapes as so:
Now you can include all shapes in one array. However you will need a type variable stating what kind of shape it is so you know what functions to call, plus you''ll have extra variables needed for every shape, in every single instance. A big waste of memory. Let''s look at a 3rd choice:
We added a 4th class here, called Shape. This is an abstract class, meaning you can''t declare an instance of it. And there would be no reason too either, because it is too generic to be of any use. Now, this is where it gets interesting. You can declare a square as so:
Why not just declare it as Square mysquare; you ask? Simple. With the first approach you can put ALL shapes in the same array. Just create the array of type: Shape*, for example:
Since myarray accepts type Shape*, and the base class of each shape is Shape*, they can all be grouped together. Now, if you need to draw all the shapes, you can do it like thus:
What this will do, is call the appropriate draw function for each shape! You as the programmer are oblivious as to what shape those might be, but the program knows and the correct draw function for each shape will be called. Pretty spiffy, eh?
For games, for example, you could create an abstract class Monster, and derive Ogre, Grunt, Werewolf, from them and keep all monsters in the same array/linked list.
There are of course other cool uses and reason for using classes/polymorphism, but I think this is long enough =)
Hope it helped!
...
- Houdini
...
It''s hard to understand how powerful polymorphism (derived classes with virtual functions) is without a real world example, so here goes. Warning, this might be long
Say you are writing program that the user will need different kinds of shapes (square, circle, triangle, etc) that you will keep in an array. These shapes need to be created, drawn, and printed. You could go about it by creating 3 different classes, one for each shape:
class Square{public:void Create();void Draw();void Print();};class Circle{public:void Create();void Draw();void Print();};class Triangle{public:void Create();void Draw();void Print();};
This is all fine and dandy, except you will need 3 different arrays, one for each class type. You could go a different route and create one class that contains all shapes as so:
class Shape{public:void CreateSquare();void DrawSquare();void PrintSquare();void CreateCircle();void DrawCircle();void PrintCircle();void CreateTriangle();void DrawTriangle();void PrintTriangle();};
Now you can include all shapes in one array. However you will need a type variable stating what kind of shape it is so you know what functions to call, plus you''ll have extra variables needed for every shape, in every single instance. A big waste of memory. Let''s look at a 3rd choice:
class Shape{public:virtual void Create() = 0;virtual void Draw() = 0;virtual void Print() = 0;};class Square : public Shape{public:virtual void Create();virtual void Draw();virtual void Print();};class Circle : public Shape{public:virtual void Create();virtual void Draw();virtual void Print();};class Triangle : public Shape{public:virtual void Create();virtual void Draw();virtual void Print();};
We added a 4th class here, called Shape. This is an abstract class, meaning you can''t declare an instance of it. And there would be no reason too either, because it is too generic to be of any use. Now, this is where it gets interesting. You can declare a square as so:
Shape *mysquare = new Square;
Why not just declare it as Square mysquare; you ask? Simple. With the first approach you can put ALL shapes in the same array. Just create the array of type: Shape*, for example:
Shape *myarray[3];myarray[0] = new Square;myarray[1] = new Circle;myarray[2] = new Triangle;
Since myarray accepts type Shape*, and the base class of each shape is Shape*, they can all be grouped together. Now, if you need to draw all the shapes, you can do it like thus:
for (int x = 0; x < TOTAL_SHAPES; x++) myarray[x]->Draw();
What this will do, is call the appropriate draw function for each shape! You as the programmer are oblivious as to what shape those might be, but the program knows and the correct draw function for each shape will be called. Pretty spiffy, eh?
For games, for example, you could create an abstract class Monster, and derive Ogre, Grunt, Werewolf, from them and keep all monsters in the same array/linked list.
There are of course other cool uses and reason for using classes/polymorphism, but I think this is long enough =)
Hope it helped!
...
- Houdini
- Houdini
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement