question about OO.

E

enki

I was reading a book on game programming and they were explaining a
game engine class and they say that correct OO programming is making
base calss functions virtual. I am been reading and larning about C++,
and from what I have been told here is that you should use the right
tool for the right job.

What I have gathered is that I should use virtual functions and dynamic
binding if I want to create similar objects that can be stored in the
same array or container. Such as shape drawing or having different
account types calcuate intrest different.

I have also learned that I should not alwasy believe what I read in
books.

Are virtual functiontions and dynamic binding correct OO programing
practice?
 
P

Phlip

enki said:
I was reading a book on game programming and they were explaining a
game engine class and they say that correct OO programming is making
base calss functions virtual.

Correct programming in general replaces repeated or duplicated code, and
duplicated effort, with virtual methods. This is a very subtle point, but
you can't just say "make all base classes virtual".

Plenty of times, you should not have a base class, if that's simplest.
Are virtual functiontions and dynamic binding correct OO programing
practice?

Stare at these two functions, written in an arbitrary language:

function drawShape(shape, type)
if type == SQUARE
drawSquare(shape)
else
if type == CIRCLE
drawCircle(shape)
else
if type == Triangle
drawTriangle(shape)
end
end

function saveShape(shape, type)
if type == SQUARE
saveSquare(shape)
else
if type == CIRCLE
saveCircle(shape)
else
if type == Triangle
saveTriangle(shape)
end
end

If you want to add a new shape, you must change both of those functions.
(Your tutorials might make you think you should design a program once, then
never change the design. All good designs permit flexibilities and
extensions.)

Now we change the design to the OO equivalent:

class Square
function draw();
function save();
end

class Circle
function draw();
function save();
end

class Triangle
function draw();
function save();
end

function drawShape(shape)
shape.draw()
end

So far, no benefit. If there were only one client, we might not even make
this refactor. But we have two clients, and the second one looks like this:

function saveShape(shape, type)
shape.save()
end

That's it; the second function got short, so overall we have fewer lines of
code. (Tip: Good designs minimize lines of code!)

So if we want to add a new class, we only need to declare it, then pass it
into functions that expect this interface:

class Blob
function draw();
function save();
end

Now pass Blobs into either drawShape() or saveShape(), and they will work.

You don't need to change code in many places just to add new behaviors to
it.

Now read the book /Design Patterns/.
 
E

enki

That is what I thought.

I am more intersted in C++.
That is I usually use regular binding uness I want to create a vector
of shapes or accounts and put them in the same container like vector
<*accounts>acc;

then I could do acc->intrest to do intrest of a checking or savings
account. Other than that, I use static binding.
 
P

Phlip

enki said:
That is what I thought.

I am more intersted in C++.

Translating my pseudo code to C++ would be trivial, in an editor.
That is I usually use regular binding uness I want to create a vector
of shapes or accounts and put them in the same container like vector
<*accounts>acc;

then I could do acc->intrest to do intrest of a checking or savings
account. Other than that, I use static binding.

When you iterate over a polymorphic array, and send the same message to
different methods, this is the Abstract Template Pattern.

There are other patterns, and other ways to polymorph the Abstract Template
Pattern.
 
I

Ivan Vecerina

enki said:
I was reading a book on game programming and they were explaining a
game engine class and they say that correct OO programming is making
base calss functions virtual. I am been reading and larning about C++,
and from what I have been told here is that you should use the right
tool for the right job.

What I have gathered is that I should use virtual functions and dynamic
binding if I want to create similar objects that can be stored in the
same array or container. Such as shape drawing or having different
account types calcuate intrest different.
This would be one example among many. As Phlip did, I can only
recommend reading "Design Patterns" by E. Gamma & al, because it will
show you a number of different ways in which run-time polymorphism
(=virtual functions) can be taken care of.
Or you may try:
http://www.dofactory.com/Patterns/Patterns.aspx
I have also learned that I should not alwasy believe what I read in
books.

Are virtual functiontions and dynamic binding correct OO programing
practice?
Yes.
In an object-oriented design, it hardly ever makes sense to have
a base class without virtual functions.

But C++ also supports other programming paradigms (e.g. generic),
in addition to supporting object-oriented programming.
In C++, base classes are used in ways that are unrelated to
OOP - and virtual member functions are then typically irrelevant.


hth-Ivan
 
B

BigBrian

Are virtual functiontions and dynamic binding correct OO
programing practice?

By definition, OO programming depends on having dynamic binding ( which
is supported in C++ by virtual functions ). However, just making your
member functions virtual doesn't mean you're doing OO programming.
virtual functions and dynamic binding if I want to create similar objects that
can be stored in the same array or container.

This is too specific. You need to use dynamic binding if you want to
deal with objects polymorphically ( not only by storing them in a
container ).

-Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,530
Members
48,252
Latest member
shilpadhussa

Latest Threads

Top