473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

question about OO.

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?

Jul 23 '05 #1
5 1218
enki wrote:
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/.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #2
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.

Jul 23 '05 #3
enki wrote:
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.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #4
"enki" <en*****@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #5
>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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.