473,320 Members | 1,921 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,320 software developers and data experts.

OOP vs procedural

Please, excuse me for starting this discussion.
I'm not a troll, and I love the OOP architecture.
However I'm a C++ newbie, and sometimes it's hard for me to fully
understand the improvement of OOP. Please excuse my poor english too.
I hope it's sufficient to explain my question.

Generally OOP is better than procedural, but in a case I'm working on,
I'm not sure. This is an example of my doubts.

I'm writing a simple GUI openGL based.
I've some buttons, that must call some functions.

PROCEDURAL STYLE

I assume that:

-The button widget has an "identity".
-The identity is assigned when the button is created; example:
new Button("foo") assign the ID = "foo" to button created.
-The button widget has a boolean button.mouseOver.
-I use GLUT: when mouse is pressed, a callback is executed. This
callback check, for all buttons, if mouseOver is true.
-If mouseOver is true, the button return his ID

The procedural code is very simple (it's pseudo code):

------------------------------------------
//declare some functions

myfunction1(string)
myfunction2(int)
myfunction3(string, int)

//declare some buttons:

new Button("foo1");
new Button("foo2");
new Button("foo3");

//the mouse callback function for GLUT

mouse{
string ID
for each widget:
if widget.mouseOver == true:
switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())
case ("foo3"):
myfunction1(widget.getname(),widget.getvalue())
}
------------------------------------------

that's all.

What I see:
- It's flexible: I can link a button with any function I want, without
limits in number and type of arguments.
- It's readable and easy, even for a newbie.
- It don't need complex pattern, pointers, and avoid memory lacks or
dangling references possibilities.
However, I know, it's not fully OOP. So some coders, experts in C++,
tell me that I should use functionoids. So I'm starting from here:

http://www.parashift.com/c++-faq-lit...html#faq-33.10

but I don't understand why functionoids (in this case) is better.
I've not fully followed them, however seem that, in order to use them, I
must write a lot of extra code (very complex for me) lines.

1) I must write a base class with a pure-virtual method
2) I must write a derived classes for each function (so if I've 100
functions, I must write 100 derived classes ?)
3) how to delete the pointers created by a functionoids? Maybe for you
(gurus of C++) this is easy, but for a newbie/medium level coder this
mean a lot of difficults, hard debug, possible memory lacks or dangling
references.

Note that I must write, in both cases, the same number of functions.
But in second case, it's not sufficient write a simple function, but
it's needed insert it in the functionoid pattern...so I'm almost sure
that the lines to write is increased in second case.

However, the doubt is not only about the increase of complexity, but
about the advantages of functionoids (alway, in this particular case);
again, I admit to have not fully understand functionoids, but seem that
advantages is about the arguments that is possible to pass:

"Then later you pass the remaining args using that pointer, and the
system magically takes your original args (that were freeze-dried),
combines them with any local variables that the function calculated
prior to being freeze-dried, combines all that with the newly passed
args, and continues the function's execution where it left off when it
was freeze-dried. "

This sound good! But I've all flexibility I need with procedural
approach: the buttons can call any type of function, with any type and
number of parameters, mixing local variable with widgets variables...

To conclude, I quote "Thinking in C++", vol.2 chapter 10:
-------------------------------------------------------
"Understanding inheritance and polymorphism is such a challenge that you
may begin to assign undue importance to these techniques. We see many
over-complicated designs (our own included) that result from
"inheritance indulgence"”- for example, many multiple inheritance
designs evolve by insisting that inheritance be used everywhere.

One of the guidelines in Extreme Programming is "Do the simplest thing
that could possibly work." A design that seems to want inheritance can
often be dramatically simplified by using composition instead, and you
will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So
when pondering a design, ask yourself: "Could this be simpler using
composition? Do I really need inheritance here, and what is it buying me?"
-------------------------------------------------------

So, the final question is: in this SPECIFIC case is really true that OOP
is better of procedural?

Please, remember I'm only a newbie, and maybe I can't see the advantages
of OOP in this case because my little knowledge of C++, or maybe exist a
better OOP way that don't require complex functionoids and I don't know
because my ignorance.
Again, I'm not a troll...only I want understand deeply because all tell
me I must use, for this simple GUI, a different approach, not "switch
based".

I hope to don't boring you, and please, don't reply me so hard...

regards,

Manuel

Jan 3 '06 #1
16 4363

Manuel wrote:
Please, excuse me for starting this discussion.
I'm not a troll, and I love the OOP architecture.
However I'm a C++ newbie, and sometimes it's hard for me to fully
understand the improvement of OOP. Please excuse my poor english too.
I hope it's sufficient to explain my question.

Generally OOP is better than procedural, but in a case I'm working on,
I'm not sure. This is an example of my doubts.

I'm writing a simple GUI openGL based.
I've some buttons, that must call some functions.

PROCEDURAL STYLE

I assume that:

-The button widget has an "identity".
-The identity is assigned when the button is created; example:
new Button("foo") assign the ID = "foo" to button created.
-The button widget has a boolean button.mouseOver.
-I use GLUT: when mouse is pressed, a callback is executed. This
callback check, for all buttons, if mouseOver is true.
-If mouseOver is true, the button return his ID

The procedural code is very simple (it's pseudo code):

------------------------------------------
//declare some functions

myfunction1(string)
myfunction2(int)
myfunction3(string, int)

//declare some buttons:

new Button("foo1");
new Button("foo2");
new Button("foo3");

//the mouse callback function for GLUT

mouse{
string ID
for each widget:
if widget.mouseOver == true:
switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())
case ("foo3"):
myfunction1(widget.getname(),widget.getvalue())
}
------------------------------------------

that's all.

What I see:
- It's flexible: I can link a button with any function I want, without
limits in number and type of arguments.
- It's readable and easy, even for a newbie.
- It don't need complex pattern, pointers, and avoid memory lacks or
dangling references possibilities.
However, I know, it's not fully OOP. So some coders, experts in C++,
tell me that I should use functionoids. So I'm starting from here:

http://www.parashift.com/c++-faq-lit...html#faq-33.10

but I don't understand why functionoids (in this case) is better.
I've not fully followed them, however seem that, in order to use them, I
must write a lot of extra code (very complex for me) lines.

1) I must write a base class with a pure-virtual method
2) I must write a derived classes for each function (so if I've 100
functions, I must write 100 derived classes ?)
3) how to delete the pointers created by a functionoids? Maybe for you
(gurus of C++) this is easy, but for a newbie/medium level coder this
mean a lot of difficults, hard debug, possible memory lacks or dangling
references.

Note that I must write, in both cases, the same number of functions.
But in second case, it's not sufficient write a simple function, but
it's needed insert it in the functionoid pattern...so I'm almost sure
that the lines to write is increased in second case.

However, the doubt is not only about the increase of complexity, but
about the advantages of functionoids (alway, in this particular case);
again, I admit to have not fully understand functionoids, but seem that
advantages is about the arguments that is possible to pass:

"Then later you pass the remaining args using that pointer, and the
system magically takes your original args (that were freeze-dried),
combines them with any local variables that the function calculated
prior to being freeze-dried, combines all that with the newly passed
args, and continues the function's execution where it left off when it
was freeze-dried. "

This sound good! But I've all flexibility I need with procedural
approach: the buttons can call any type of function, with any type and
number of parameters, mixing local variable with widgets variables...

To conclude, I quote "Thinking in C++", vol.2 chapter 10:
-------------------------------------------------------
"Understanding inheritance and polymorphism is such a challenge that you
may begin to assign undue importance to these techniques. We see many
over-complicated designs (our own included) that result from
"inheritance indulgence""- for example, many multiple inheritance
designs evolve by insisting that inheritance be used everywhere.

One of the guidelines in Extreme Programming is "Do the simplest thing
that could possibly work." A design that seems to want inheritance can
often be dramatically simplified by using composition instead, and you
will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So
when pondering a design, ask yourself: "Could this be simpler using
composition? Do I really need inheritance here, and what is it buying me?"
-------------------------------------------------------

So, the final question is: in this SPECIFIC case is really true that OOP
is better of procedural?

Please, remember I'm only a newbie, and maybe I can't see the advantages
of OOP in this case because my little knowledge of C++, or maybe exist a
better OOP way that don't require complex functionoids and I don't know
because my ignorance.
Again, I'm not a troll...only I want understand deeply because all tell
me I must use, for this simple GUI, a different approach, not "switch
based".

I hope to don't boring you, and please, don't reply me so hard...

regards,

Manuel

No problem Manual, but you need to find more appropriate usenet groups
that deals with your questions - you can still post c++ related
questions here, and we will gladly answer and discuss them with you; I
haven't found any in your post. For oo design - take a look at
comp.object.

Similarly, we usually do not do homeworks for others but only help with
language specific obstacles. I am sure you can find resourses only
wherein your assignments can be completed for a certain payment.
Good luck

Jan 3 '06 #2
puzzlecracker wrote:
Manuel wrote:
Please, excuse me for starting this discussion.
[..excessive quoting removed..]

regards,

Manuel

No problem Manual, but [..]


Two mistakes, as I see it. Misspelling of the OP's name and
excessive quoting of what is deemed to be essentially an off-
topic posting. Your reply is to the point, though, and quite
informative. Keep at it.
Jan 3 '06 #3
puzzlecracker wrote:

No problem Manual, but you need to find more appropriate usenet groups
that deals with your questions - you can still post c++ related
questions here, and we will gladly answer and discuss them with you; I
haven't found any in your post. For oo design - take a look at
comp.object.
I think OOP is strongly related to OOP.
In this case in particular (I've underlined various time it's specific
case) it's about use of C++ pattern I've linked, called functionoid.


Similarly, we usually do not do homeworks for others but only help with
language specific obstacles.
Eh..eh!
Really I wish this was an homework...
The fact I'm C++ newbie not mean I'm young...
my first computer was msx 128 Kb ram...

I am sure you can find resourses only
wherein your assignments can be completed for a certain payment.

Payment?
So I must explain details.
I've developed this Open Source project (so free, gratis ect..) since 2000:

www.makehuman.org

for some reason the project is changed, from python (that I know)
to C++. So I need to study C++, to port python code (in this case GUI)
in C++.

I hope anyone want help me as I've helped the opensource community for
years...

Regards,

Manuel
Jan 3 '06 #4
Manuel wrote:
Please, excuse me for starting this discussion.
No prob - I added comp.object to the newsgroup list.
I'm not a troll, and I love the OOP architecture.
OOP sucks. Feel free to diss it; honestly disagreeing with a newsgroup's
average opinion is not trolling.
Generally OOP is better than procedural, but in a case I'm working on, I'm
not sure. This is an example of my doubts.
OOP subsumes procedural, and adds a new technique - polymorphism. For a
given design, if you don't need the technique, don't use it. Polymorphism is
not automatically "better".
I'm writing a simple GUI openGL based.
Ouch. Okay, polymorphism is automatically better! ;-)
I've some buttons, that must call some functions.

PROCEDURAL STYLE

I assume that:

-The button widget has an "identity".
-The identity is assigned when the button is created; example:
new Button("foo") assign the ID = "foo" to button created.
-The button widget has a boolean button.mouseOver.
-I use GLUT: when mouse is pressed, a callback is executed. This callback
check, for all buttons, if mouseOver is true.
-If mouseOver is true, the button return his ID

The procedural code is very simple (it's pseudo code):

------------------------------------------
//declare some functions

myfunction1(string)
myfunction2(int)
myfunction3(string, int)

//declare some buttons:

new Button("foo1");
new Button("foo2");
new Button("foo3");

//the mouse callback function for GLUT

mouse{
string ID
for each widget:
if widget.mouseOver == true:
switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())
case ("foo3"):
myfunction1(widget.getname(),widget.getvalue())
}
------------------------------------------

that's all.

What I see:
- It's flexible: I can link a button with any function I want, without
limits in number and type of arguments.
- It's readable and easy, even for a newbie.
- It don't need complex pattern, pointers, and avoid memory lacks or
dangling references possibilities.
The big problem is this: If you want to add a new capacity to all buttons,
you must find a list of switch/case statements, clone it, and change all the
target functions.

Then, if you want to add a new button, you must find every list of switch
case statements that list buttons, then add the new button to the list.
However, I know, it's not fully OOP. So some coders, experts in C++, tell
me that I should use functionoids. So I'm starting from here:

http://www.parashift.com/c++-faq-lit...html#faq-33.10

but I don't understand why functionoids (in this case) is better.
I've not fully followed them, however seem that, in order to use them, I
must write a lot of extra code (very complex for me) lines.
Focus on the big problem I listed. How can you define new buttons in only
one place, and define new functions for buttons in only one place? How can
you replace this...

switch(widget.getID())
case ("foo1"):
myfunction1(widget.getname())
case ("foo2"):
myfunction1(widget.getvalue())

with this:

widget.myfunction();

Different widgets respond to myfunction() with different methods.
1) I must write a base class with a pure-virtual method
Or write a virtual method with nothing in it.
2) I must write a derived classes for each function (so if I've 100
functions, I must write 100 derived classes ?)
No. Write a derived class for each method that differs. Most will be the
same. Each class may have fewer methods than its parent class.

In a GUI, when you perform a trivial action like skate the mouse back and
forth, most widgets don't implement their "rollover()" method. They do
nothing at rollover time, so your putative base class would have a
rollover() with nothing in it.

Only those few widgets that pop up a tooltip or change their appearance at
rollover time would implement that function.
3) how to delete the pointers created by a functionoids? Maybe for you
(gurus of C++) this is easy, but for a newbie/medium level coder this mean
a lot of difficults, hard debug, possible memory lacks or dangling
references.
In this specific case, don't 'new' them, hence don't 'delete' them. Put them
in your global space before main().

You will eventually need to Google for "smart pointer".
Note that I must write, in both cases, the same number of functions.
But in second case, it's not sufficient write a simple function, but it's
needed insert it in the functionoid pattern...so I'm almost sure that the
lines to write is increased in second case.
The point is to write much fewer functions, and fewer lines in each
function, while increasing your ability to change classes and functions
easily.
However, the doubt is not only about the increase of complexity, but about
the advantages of functionoids (alway, in this particular case); again, I
admit to have not fully understand functionoids, but seem that advantages
is about the arguments that is possible to pass:

"Then later you pass the remaining args using that pointer, and the system
magically takes your original args (that were freeze-dried), combines them
with any local variables that the function calculated prior to being
freeze-dried, combines all that with the newly passed args, and continues
the function's execution where it left off when it was freeze-dried. "

This sound good!
It's wrong, and it's not "OOP". All functions should take the same, single
parameter - probably an Event object. And if your GUI has different kinds of
events (keyboard, mouse, etc.), then these should also have derived classes
with different virtual methods. So MouseEvent::getXY() returns the mouse
location relative to your GUI, and KeyboardEvent::getXY() returns a location
relative to the control with the keyboard focus.

When your input parameter is a reference to a polymorphic object, then it
typesafely expresses the kind of polymorphism which your variable argument
trick attempts to express.
But I've all flexibility I need with procedural approach: the buttons can
call any type of function, with any type and number of parameters, mixing
local variable with widgets variables...
You mean that you have the flexibility to write different kinds of lines
inside each case of each switch statement. Sometimes that's the flexibility
you need. However...

Imagine if all widgets can respond to several kinds of events. Mouse,
Keyboard, Timer, Network, Collision, etc. You need one switch/case for each
of those. Now to add a widget, you have to write new lines into each
switch/case. Oh, and you have a different XY system for each kind of widget
that needs XY, and you repeat this system for each kind of Event that
supplies a default screen location. You must re-type all that each time you
add new events or new widgets.

The flexibility to perform lots and lots of typing is _not_ the kind of
flexibility we need.

So instead if you have this...

pWidget->doEvent(pEvent);

....then some doEvent() implementations do nothing. Some call
pEvent->getXY(). This sometimes pulls the hardware mouse's location, and
sometimes reads the location from a widget. To add new widgets and events
you only write their class, and inside it you override and change only the
few methods that differ.
To conclude, I quote "Thinking in C++", vol.2 chapter 10:
-------------------------------------------------------
"Understanding inheritance and polymorphism is such a challenge that you
may begin to assign undue importance to these techniques. We see many
over-complicated designs (our own included) that result from "inheritance
indulgence"”- for example, many multiple inheritance designs evolve by
insisting that inheritance be used everywhere.

One of the guidelines in Extreme Programming is "Do the simplest thing
that could possibly work." A design that seems to want inheritance can
often be dramatically simplified by using composition instead, and you
will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So
when pondering a design, ask yourself: "Could this be simpler using
composition? Do I really need inheritance here, and what is it buying me?"
-------------------------------------------------------

The XP slogan "do simple things" applies _after_ you learn how to simplify
complex designs. (The correct books for this are /Refactoring/, and
/Refactoring to Patterns/). It does not mean the simplest design to code. XP
also expects you to add features to code without adding bugs or lowering its
design quality. If you don't clean up early, and use virtual methods where
they reduce the line count, then later you will add lots of patches and
hacks, such as global variables, to add new features.
So, the final question is: in this SPECIFIC case is really true that OOP
is better of procedural?
Uh, all the best GUIs use OOP, generally like I said, to polymorph their
widgets.
Please, remember I'm only a newbie, and maybe I can't see the advantages
of OOP in this case because my little knowledge of C++, or maybe exist a
better OOP way that don't require complex functionoids and I don't know
because my ignorance.


You have in fact asked the most important question about OOP - how to
replace a switch statement with polymorphism. There are those who call this
the "Replace Conditional with Polymorphism Refactor".

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Jan 3 '06 #5
Victor Bazarov wrote:

Two mistakes, as I see it. Misspelling of the OP's name and


Eh?
OOP = Object-Oriented Programming
where is the error?
Jan 3 '06 #6
Manuel wrote:
[..]
I think OOP is strongly related to OOP.
Uh... As strongly as you're related to yourself, my guess would be.
In this case in particular (I've underlined various time it's specific
case) it's about use of C++ pattern I've linked, called functionoid.


Yes, but the principals and topics you're discussing (or wishing to
discuss) are not language-specific, and 'comp.object' NG is, in fact,
a much better place for that. Also, consider comp.programming for
comparing procedural and OO types of programming, since procedural
(functional) programming would probably be off-topic in comp.object.

Just my $0.02...

V
Jan 3 '06 #7
Manuel wrote:
Victor Bazarov wrote:

Two mistakes, as I see it. Misspelling of the OP's name and
Eh?


What? I wasn't replying to you and I wasn't talking of your
mistakes.
OOP = Object-Oriented Programming
where is the error?


Your name is ManuEl, the spelling used was ManuAl. That is
a mistake as I see it. BTW, 'OP' means "Original Poster" in
Usenet speak.

V
Jan 3 '06 #8
Victor Bazarov wrote:
Manuel wrote:
[..]
I think OOP is strongly related to OOP.

Uh... As strongly as you're related to yourself, my guess would be.


Sorry...I mean I think OOP is strongly related to CPP.
I'm a bit tired...

Ciao,

Manuel
Jan 3 '06 #9
Phlip wrote:

Imagine if all widgets can respond to several kinds of events. Mouse,
Keyboard, Timer, Network, Collision, etc. You need one switch/case for each
of those. Now to add a widget, you have to write new lines into each
switch/case. Oh, and you have a different XY system for each kind of widget
that needs XY, and you repeat this system for each kind of Event that
supplies a default screen location. You must re-type all that each time you
add new events or new widgets.

The flexibility to perform lots and lots of typing is _not_ the kind of
flexibility we need.


Hi Phlip,
Thanks for long reply...
I've almost completely understand the problem, you are very clear.

The simple classes I'm writing already have partially the
polymorphism...: setThis(), doThat(), etc.. a base widget class with
virtual functions to be inherit...etc...
In example, a loop like

for widget in widgetList:
widget.draw()

Draw all widgets type, because draw is a virtual function, with
different meaning in various derivated classes.

But all these methods change the internal status of widget itself, or do
internal functions, or change the status of openGL machine, and this is
very easy.

The problem is to link the button with a specific "external" function.

So my post was related only to these special functions...about how to
call them in application code. So, it is almost impossible to write more
of one case for each button. And to add a method to widget is not
related to switch/case...I can add all methods/property I want...only
the main callback need to parsed in switch/case...

Ah...before continue...this is not an homework! I'm too old to have
homework :-) This is related to this opensource project:

www.makehuman.org

However, reading your reply, I'm thinking that maybe is not needed the
functionoids. It's not sufficient pass a pointer_to_function as argument
of button?
Thanks,
Manuel
Jan 3 '06 #10
Manuel wrote:
The problem is to link the button with a specific "external" function.
Now the question is "what's an object?"

Not just widgets should be objects. Anything that has functions should be
objects. So the answer is "the thing that needs this external function
should itself also be an object".

And this topic is leading us to Observer Pattern and Model View Controller.
Look them up, and crack the book /Design Patterns/.
Ah...before continue...this is not an homework!
You are allowed to post questions about learning projects, so long as you
make a good-faith effort yourself, which you have done.
www.makehuman.org


However, reading your reply, I'm thinking that maybe is not needed the
functionoids. It's not sufficient pass a pointer_to_function as argument
of button?


Try this:

EditField nameField;
EditField addressField;
MaskField zipCode("99999");

....

switch(ID)
case ("name"):
return nameField;
case ("address"):
return addressField;
case ("zip"):
return zipCode;
...

The switch happens only once, to turn an ID into an object. (This always
happens at the boundary of a program.) So all remaining functions can pass a
Widget & around, and all these classes inherit Widget.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Jan 3 '06 #11
> The big problem is this: If you want to add a new capacity to all buttons,
you must find a list of switch/case statements, clone it, and change all the
target functions.

This depends on the implementation of the buttons. Often lots of
switch/case statements are a suggeston that perhaps a data structure
should be used instead (I prefer tables, but they are not
well-supported of late). If every element of the structure is to get
(inherit) new behavior, then you simply add it in the one place that
processes the structure, all without OOP.

However, the tricky part is when some get such behavior and some don't.
If the pattern is not hierarchical, then inheritance (and possibly
polymorphism) can turn into a mess.

I have been round-and-round with regards to case/switch statements and
OOP. I do not buy the general argument that case/switch statements are
necessarily more code or more duplication. For an example of a long
prior debate, see:

http://www.c2.com/cgi/wiki?SwitchStatementsSmell

Further, I don't think GUI engines should be tied to one particular
language because it means reinventing the wheel for each language; and
a language-neutral GUI system will be less likely to use polymorphism.

-T-

Jan 3 '06 #12
> The big problem is this: If you want to add a new capacity to all buttons,
you must find a list of switch/case statements, clone it, and change all the
target functions.

This depends on the implementation of the buttons. Often lots of
switch/case statements are a suggeston that perhaps a data structure
should be used instead (I prefer tables, but they are not
well-supported of late). If every element of the structure is to get
(inherit) new behavior, then you simply add it in the one place that
processes the structure, all without OOP.

However, the tricky part is when some get such behavior and some don't.
If the pattern is not hierarchical, then inheritance (and possibly
polymorphism) can turn into a mess.

I have been round-and-round with regards to case/switch statements and
OOP. I do not buy the general argument that case/switch statements are
necessarily more code or more duplication. For an example of a long
prior debate, see:

http://www.c2.com/cgi/wiki?SwitchStatementsSmell

Further, I don't think GUI engines should be tied to one particular
language because it means reinventing the wheel for each language; and
a language-neutral GUI system will be less likely to use polymorphism.

-T-

Jan 3 '06 #13
Phlip wrote:
You are allowed to post questions about learning projects, so long as you
make a good-faith effort yourself, which you have done.
Thanks...

Try this:

EditField nameField;
EditField addressField;
MaskField zipCode("99999");
....
The switch happens only once, to turn an ID into an object. (This always
happens at the boundary of a program.) So all remaining functions can pass a
Widget & around, and all these classes inherit Widget.


The "old" switch happen only once too...
I've written a better example to show the question:
what's the difference returning an object instead an ID ?

----------------------------------------------------

#include "mhcontainer.h"
#include "mhwidget.h"
#include "square.h"
#include "mesh.h"

mesh meshObj; //mesh class to handle the geometry
mhcontainer widgetContainer //A canvas class to contain widgets

//Some functions that use local objs

void applymorph(){
meshObj.function1();
meshObj.getThis();
meshObj.getThat();
}

void smoothMesh(){
meshObj.function2();
meshObj.getThis2();
meshObj.getThat2();
}

void hideContainer(){
widgetContainer.setOff()
aFuntion();
anotherFuntion();
}
//THIS IS THE NOT OOP FUNCTION!!!!!
//Pseudocode here!!!
void mouseFunction(x, y ){
for each widget in widgetContainer:
if widgetContainer.getStatus() == on:
if widget.onMouseOver(x,y) == true:

switch(widget.ID):
case (Morphing): applymorph()
case (Smooth): smoothMesh()
case (Close): hideContainer()
int main ( int argc, char** argv )
{

widgetContainer.addWidget(new Button ("Morphing"));
widgetContainer.addWidget(new Button ("Smooth"));
widgetContainer.addWidget(new Button ("Close"));

glutInit ( &argc, argv );
MainWindow.initWindow();
glutReshapeFunc ( reshape );
glutDisplayFunc ( display );
glutMouseFunc ( mouseFunction ); //MOUSE FUNCTION NO OOP!!!!
glutMainLoop ( );
system("PAUSE");
return 0;
}
----------------------------------------------------

Thanks for help,

Manuel
Jan 3 '06 #14
Once upon a time (Dienstag, 3. Januar 2006 02:50) Manuel wrote some very
nice things
Victor Bazarov wrote:
Manuel wrote:
[..]
I think OOP is strongly related to OOP.

Uh... As strongly as you're related to yourself, my guess would be.


Sorry...I mean I think OOP is strongly related to CPP.


.... and to many other programming languages like Java, C#, Smalltalk, and
many more. Your question is not C++ specific and should be posted
somewhere else.

Sebastian

Jan 3 '06 #15
Good points topmind.

Jan 9 '06 #16
I think this thread is rather worrying, because most everyone is
focused on the use of polymorphism within a particular objects methods.
However, the thing that appears to be glossed over is the use of
"swtch/case" which isnt necessary if OOP is used.
This code should be simple like the following (given in psuedocode)

:
widgetContainer.applyToEachWidget(myFunctionObject ThatTakesOnArgument)
:

The "myFunctionObject" would then receive a widget object and on each
one it would invoke the same function.
ie: it would not do a "switch" on the widgetID as each widget should
have an "understanding" of what a widget
can do. For example, each widget might understand the message
"doAction()" and in response to this it
does the "Morph", "Smooth" or "hide". The caller does not have to
determine which method to invoke.

Polymorphism isnt just about sending different parameters to the same
method name.
Think in terms of a set of "like" objects, in this case widgets, all
understanding the same message "doAction" or
"handleMouseEvent". This way to code is smaller, clearer and faster.

I hope this helps.

Rgs, James.

Jan 9 '06 #17

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

Similar topics

4
by: Mark Mikulec | last post by:
Hi there, I wonder if anyone can shed some light on a very frustrating problem. I'm running a debian linux 3.0 "woody" server, nothing special, with the latest version of postres that apt-get...
10
by: Spencer | last post by:
Is it possible to execute procedural language outside of a procedure in DB2 Command Center? For instance I need the ability to declare some variables and do some conditional processing similar to...
2
by: John Gabriele | last post by:
The way I've always "designed" procedural C programs has just been to: A. Think about what I need to do B. Draw a flowchart with pencil and paper C. Implement the flowchart in C from the top...
13
by: parley | last post by:
After several years of programming WWW applications in ASP.NET (and several other frameworks) our division has come to what might seem a counterintiutive conclusion: Writing ASP.NET code in a...
2
by: --CELKO-- | last post by:
I am trying to collect a few examples of SQL queries that were done with a "procedural mindset" and another solution done with a "set- oriented mindset". I have a short article at...
4
by: Mohitz | last post by:
Any pointers as to how one should go about porting procedural type code into object oriented code?? --- Mohit
2
by: san1014 | last post by:
Hi I have few questions. Pleae give me the answers. 1) In postgreSQL, how to declare and call the cursors in a stored procedural function? Ex: I want to display all the columns in a table in one...
0
by: FLANDERS | last post by:
Hi all, Is it possible to declare a SQL type of result set or similar? I want to do use the IN predicate like you can in a non-procedural SQL like this: UPDATE TABLE1 SET COL1 = 123 WHERE COL2 IN...
14
by: mesut | last post by:
hi colleagues, I don't know if this is the right group for but it's in C# so I try. I have a #3 procedural function called GetInfo.. and those are 3 overloaded methods. I would like to use the...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.