How to use OO well

Started by
9 comments, last by Zahlman 19 years, 8 months ago
I know there are many books on this, and I have already searched google, but I'm wondering if any of you know of any good resources and/or could just tell me yourselves some good object oriented practices my first and only programming language is C++, so I know about object orientedness (is that a word?) and how to implement it in the code, but I'm not sure if I'm using the concepts well...like just what to make classes and how to divide things, and things like that I think I sometimes try to split things too much...since I've only known C++, I try to make everything fit in a class...
Advertisement
This book is meant to be good. The main thing to understand though(if coming from a procedural background), is that objects are SIMPLE. I say this because i used to try and make my classes overly complicated, i think procedural programming encourages you to think of more specific functions, but in oop it's more abstract functions.. if i'm making any sense :P
It will come with experience, just keep writing programs and eventually they will become neater and neater and OO will be easier to implement. Also you might want to play around with a OO scripting/programming language. I learned somethings about OO from reading Python code.
yes, one way I learn very well is with examples...does anyone know of any open-source oo programs that are well-written (so as to provide a good example)?
I learn oop by learning smalltalk. I would recomend it as its a good starting point for oop but I guess there is most likely a book to follow thats easier. ahve you tried learning uml?
nope, the only languages/sublanguages I've worked with are C++, SDL, and a little HTML
look into UML m8 as most books on the subject teach good modelling processes. its not a programming lanuage more of a descriptive one and very handy for use in designing games.
Since data changes more frequently than interfaces folks recommend you code to abstract interfaces which represent abstract concepts that won't change much. A car has an engine, what kind doesn't matter as long as user can start it. So some code in your codebase will code to the engine abs interface and then later you can change the engine to electric one for example and that part that uses the abs engine interface will still work and won't have to change. This reduces code maintenance.

The second thing is that you don't want to be pulling data out of an object and instead ask the object to act on its own data. This way that data is localized to one class and it isn't all over the codebase. Again simplifies code maintenance. The danger here is the creation of god classes by shoving every kind of data into one object so that other parts have direct access to that data. Sometimes you need to pull data out which is ok but go easy on it.

Also, objects should communicate among each other to do things. Some algos will be described by this communication. I like to use finite state machines for my tasks and for algos. They're easier to understand when written on paper as a state diagram. Look up www.objectmentor.com website for more ideas.
A good thing to look into are patterns. I myself learned a lot about class design and class/module interaction from patterns.

I can recommend the Gamma on this subject, here's the url for it: Design Patterns

Also, you can try to google for "Programming Patterns". These are two sites that I've found and that look decent on first glance:
Patterns Link 1
Patterns Link 2

I hope that helps.
--Perfection, my messenger from hell.
Thanks for the advice everyone. I've started to read Thinking in C++ and I kind of like theory like that, so it's been helpful. I've also looked into UML, and that looks cool...

This topic is closed to new replies.

Advertisement