Re: How to use objectiented programming.
Thomas Kowalski wrote:
This all works. I am trying to show that I understand how to do dynamic
binding but not so well how to use it and how it can make my programs
better. I know about the shape example but often times, I don't find
this kind of programming useful for what I do.
Well, in OOP (object-oriented-programming) polymorphism is one of the
most important things.
Polymorphism means that you call a method like "move" or "attack" on
an object and different things happen.
class Actor {
private :
m_life_points;
public:
virtual void attack( Actor &foo ) = 0;
virtual void move() = 0;
virtual void reduce_life_points() {m_life_points--;};
}
class Hero : public Actor {
virtual void attack( Actor &foo ) {
swing_sword();
foo.reduce_life_points();
}
virtual void move() {
run_forward();
}
}
class Monster : public Actor {
virtual void attack( Actor &foo) {
use_claws();
foo.reduce_life_points();
}
virtual void move() {
run_forward();
}
}
In this case the main loop might do something like this (pseudo-code):
for each actor in actors do
actor.move()
without caring whether actor is a hero or a monster or any other kind
of actor that might be added to the game in the future. Unlike using
an switch statement there is no need to change the loop to add more
kinds of actors.
To sum it up. If you want to execute different behavior at _RUN-TIME_
(in opposite to Compile-time) there is a good chance that OOP is
beneficial for you.
For me it was helpful to learn about design pattern to fully
understand OO. However since design pattern are not easy to understand
without knowledge of OOP you should try an easy lecture like "Head
First Design Pattern". It's fun to read and quick helpful to gasp OO.
Regards,
Thomas Kowalski
Yes, to respond to this post and all the others. I know the general
principle of OO programming. I use objects all the time but seldom use
dynamic binding. This is not so much about how but why. I know that it
is a great deal to ask but I will link to some of my work. I have
written some pretty large programs and in one case I set it up for
dynamic binding but never added the the different kinds of things that
would require using all that.
http://www.games.siten.nl/component/option,com_remository/Itemid,6/
My latest is SBGII.7 and I have a project called SBGIII unfinished in
demos. They are my latest works.
I have some demos and full games. I just want to write better code with
C++. Basically I would like some clues on how to have better program
design. I have read some books and I can do the code but I am trying to
get some ideas to what are the best solutions to common problems.
I would like some good starting points for object oriented programs.