Archived

This topic is now archived and is closed to further replies.

element2001

To OOP or not to OOP, that is the question

Recommended Posts

I''m new to programming in C++ and opengl. I see in NeHe''s tuts he doesn''t use OOP. I do understand OOP code its just that I''m not how shall I say, fluent in it. So I open up the newest Zelda code and it has been converted to OOP. Well my question is, what is the point to use OOP? I mean if one can code all those tuts without using OOP, then one can also code an entire game without OOP correct? Maybe its just me, because its new to me and I''m used to reading non-OOP code that reading and following OOP code is more difficult, and writing OOP just seems like more unnecessary and complex work compared to writing non-OOP code. Is this just because I''m new to it or does everyone share my views on this? Thanks for responding.

Share this post


Link to post
Share on other sites
OOP is very useful when writing large programs. It allows the programmer to break the game into smaller pieces of code that are easier to write and debug. It also allows the programmer to change parts of code without needing to recompile the entire project.
The most useful feature (and why most people use it) is it allows for code reuse. If you use OOP you can write a code segment, like a engine, and use it in many different programs. This allows you to write programs much faster and easier than if you wrote a special engine for every game you make.


"cogito, ergo sum" -Descartes

Share this post


Link to post
Share on other sites
Along the lines of what what he was saying, if you think about it, everything breaks down into objects.

For example, you have a human object. Then you have arms, legs and feet, and then fingers and toes. Each part controls the other part, and thus, OOP is created.

I find it very easy to implement OOP rather than just functions and variables. It does make maintaining large projects extremely easy.

Just wanted to add that.

-Vic

Go away or I will replace you with a regular expression.

Share this post


Link to post
Share on other sites
It''s a small hill to climb over when your making the transition from procedural to OO programming, but when your on the other side, your glad you made the trip. Just as an example, when writing a game similar to the classic (ect)Mario(ect) sidescrollers:

PROCEDURAL:
Mario''s position is defined in a struct/ globals/ whatever, likewise with all his enemies. All of the functions that deal with that data (jumping, collision detection, firing fireballs, ect) are called somewhere else in the code. In effect, poor mario is spread out all over the place. As far as adding/removing anything goes- you''ll have to hunt for the right location, given that you have functions called from functions called... all the way back to WinMain(); Finally, the function names: Jump(Mario, JUMP_SUPER);
OBJECT-ORIENTATED:
You define a class "character" in character.cpp/.h that has attributes such as screen position, size, health, or whathaveyou... and any functions that deal with that data (move to the left, CheckFalling, ThrowFireball). Everything is in a nice, neat, sorted package. It gets much easier to read, as well- Mario.Jump(JUMP_SUPER);

Anyways... do what you like... but oop definately gets my vote as the way to go.
-Tok.

Share this post


Link to post
Share on other sites
Ah I see. Very nice explanations, thanks very much. Only thing I don''t yet get is in what way 1 class is derived from another. Or even if that need be done in all situations or just in some. But I still have a lot of reading to do on the subject so I''m sure I''ll get there eventually.

Share this post


Link to post
Share on other sites
Just some. To continue with the Mario/character example:

You have a class named Character that contains screen position, size, health...basically anything that all characters would have.

Then you have a class named Mario which has the jump ability, the ability to toss things (since the screen is of SMB2), and so forth. This class is derived from Character since Mario also needs to keep track of screen position, size, health, etc.

You have another class, Monster (since I don''t remember that boss''s name) that is derived from Character and has the functions for spitting eggs.

You also have a generic Character for each of the little monsters that appear on the level that don''t do anything special.

And thus, you have an OOP design (OOD) of both derived classes and non-derived classes.

Hope that made sense : )

Share this post


Link to post
Share on other sites
I love OOP. When I think of making a program, I think in terms of what classes I''ll need to create. That way, if I give up building a program, I''ll have already created classes that I can use elsewhere, even if the program never gets finished. One example I can think of is I designed and coded a whole thing that implements LAN connectivity (both TCP and UDP) with classes for client and server and auto-server-finding capabilities, but to date I haven''t used the code. (Anyone interested?)

Anyway, OOP is good.

Share this post


Link to post
Share on other sites
Required reading for anyone interested in OOP: Design Patterns - Elements of reusable object-oriented software by Gamma, Vlissides et al.

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Silvanis
You have another class, Monster (since I don''t remember that boss''s name)...

Trivia: Bowser.

Share this post


Link to post
Share on other sites
That is not even a question .. of course use OOP.. every new language is OO.. (like Java/C#/).. Of course you can do everything both ways but...

There are more worlds than the one that you hold in your hand...

Share this post


Link to post
Share on other sites
Now i love OOP programming, it is so useful, BUT i have alot of errors with it. The book i got never really touched upon or stressed the oop in a way that it should be. It is much better then it sounds, and i just cant seem to grasp it. I would spend alot of time creating the class and inputs and crap, but in the end i get like 50 errors. So if anybody happens to know of a very GOOD oop book, please tell!

Share this post


Link to post
Share on other sites
quote:
Original post by DarkHunter
I would spend alot of time creating the class and inputs and crap, but in the end i get like 50 errors. So if anybody happens to know of a very GOOD oop book, please tell!


Only a small portion of programming is about reading (and thereby understaning) books - what a GOOD programmer really needs, is tons of practice. Books are important, sure, but they can never make things as clear to you as things become when you understand them ALL BY YOURSELF.

You could try out http://math.hws.edu/eck/cs124/javanotes3/
Even though it is meant for those interested in Java, it is also gives a nice simple overview of OOP and some other basic computer-related stuff. If you''re not planning on doing coding in Java, you probably don''t have to read more than a few first chapters to get a basic understanding of the thing...

Keep cool!

Share this post


Link to post
Share on other sites
Just out of interest ...

How many of you have been programming for more than 15 years?

My Job requires OOPS, but fot games (unless you''ve got the best hardware) Use REAL code ... OOPS only hides the complexity - for a price!

Share this post


Link to post
Share on other sites
quote:
Original post by Shag
Use REAL code ... OOPS only hides the complexity - for a price!



Are you referring to lowlevel languages (asm''s), element2001??
I don''t think a programmer has to do THAT lowlevel stuff nowadays - it''s good for a number of things, but the complexity and code length will eventually become your enemy... Besides, what''s the standard these days??? Hmmm... Athlon 1000 with 256 Mb RAM and at least a Geforce2?

Keep cool!

Share this post


Link to post
Share on other sites
In my experience, OOP does not necessarily give you performance hits, as it did before, when there were no decent C++ compilers. Compiled C code will not look the same as compiled C++ code, however. A good programmer can write C++ code that easily outperforms straight C code. (It requires some knowledge of what''s different between the way each compiles.) I find it easier to optimize OOP code than non, though some people, like Shag, may find C code easier to optimize. Its all about your preferences and your coding style. Try OOP, my guess is that you''ll like it better. If not, at least you learned something, right?

email

Something kinda sad about
the way that things have come to be.
Desensitized to everything.
What became of subtlety?
--Tool

Share this post


Link to post
Share on other sites
I agree with Lord Karnus. OOP does not cause performance hit. It''s the compiler. 5-10 years ago OO code was realy slower than straight C code... but this has changed.. (rember that years ago(in era of 286/386) it took +5 years for compilers to make use of all registers).

There are more worlds than the one that you hold in your hand...

Share this post


Link to post
Share on other sites
Given the fact that almost all the abilities of OOP progamming in C++ are created through the use of pointers, it will be just as fast, if not faster, than normal C. And it will be MUCH easier to read, and most of all, MUCH MUCH MUCH MUCH easier to maintain. That is the main thing, maintenance. Sooner or later you or someone else is going to have to crack open the code for that project you made, and if it''s OOP, it will be much easier to correct / add things without side effects.


Also, OOP languages are such a breeze to learn! For example, Python is very powerful, and simple to understand, and the nice thing is because it''s OOP design, there are a ton of modules already out there to do things for you..if you already know another OOP language, Python can be learned very quickly. That is another benefit of OOP. It abstracts the actual code, into more intuitive thinking.

-nt20


"nikolatesla20"

Share this post


Link to post
Share on other sites
np glgreenhorn

Hey thanks everyone for your thoughts, I see now this is essential learning for me even if it is a slightly uphill battle to learn to think in OOP. I see it will be well worth the effort.

Edited by - element2001 on November 20, 2001 3:06:57 AM

Share this post


Link to post
Share on other sites