🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How far do you really go with OO techniques?

Started by
3 comments, last by BASSOFeeSH 24 years, 5 months ago
For instance, lets say I have the following: class CSound{ public: LPDIRECTSOUND lpds; int rate; ..... ..... }; Now if you were to follow OO standards, lpds and rate should be private members and you would have to include getRate() and getLPDS() functions. I am just curious if anybody really does this in games, or if you just leave members public to save on more function calls. I would think that in this context, the overhead would not be justified for just returning a member. Just curious how the masses get things done. "Why am I the only one on the away team with a red shirt!?" BASSOFeeSH ><>
-- What would Sweetness do?
Advertisement
Normally, I wouldn''t expose a class to the global program. I would have an abstract interface, and then have the class implementation derived from there.

When i implement such an class, I have to consider, is it a class that can and will operate on its own? Are there any other classes needing it? If there is, are they similar in logical functionality?

Meaning, if I have a SoundManager, that loads and even duplicates the sound, I would consider them as one logical package. Thus, it is ok to have these member as public, because this is a developer implementation detail. However, take care not to have too many classes or over bloated classes in the package, or maintance would be nasty and encapsulation is definately needed
One small thing to add: GetWhatever() calls don''t impose any overhead if you inline them. Generally though, you should design your classes to be good enough that you''d rarely need to get to the inside.

--TheGoop
If you want to be strict to OO, but the data members are frequently used by other functions and class and you want to make them all public just make it a regular struct, or if it makes since based on what the object is to make it all public, use a struct. structs in c++ are almost the exact same thing as classes except in structs, the default is public.

Usually, when I encapsulate things in classes, I make all the data members private.

Edited by - Domini on 2/2/00 5:23:25 PM
Get and Set functions != good OOP design. What you want to hide is data that is private to a class (that the user doesn't care about). Providing get/set functions for everything can actually break encapsulation and OO design by allowing the end user (programmer) access to stuff they shouldn't have.

Using get/set for LPDIRECTSOUND lpds actually breaks encapsulation. The idea is to seperate the interface (what the programmer uses) from the implementation (how it gets done). The end user (programmer) doesn't care that you're using directsound.

What you should do is make LPDIRECTSOUND lpds a private: member, and then have public functions like initSystem(), loadSound(), playSound(), setVolume(). There's really no reason that anything using the class should have direct access to the direct sound context. That way if you switch to a different sound library or a different platform, you rewrite the internal stuff and the code that calls it will work the same on Mac Linux or whatever else doesn't have directSound.

I'm going to get flamed for this, but you don't even need get/set functions for the rate, depending on how you use it.[Please refer to the get/set sections on the C++ FAQs before flaming] For some reason people think that if it's a public variable and not a class you NEED to use get/set functions. But on a purely abstract level, there's no such thing as a variable in C++. They're just predefined system classes. There's no difference between int myInt and class myClass. C++ went out of its way to allow operator overloading so you don't have to do things like myClass.setValue(), myClass.addOtherClass(), myClass.divide().

People who say you need to use get/set are already saying to themselves that when you overload a sign you don't have to just add, subtract, etc (like you would for a var). That's exactly the reason JAVA got rid of operator overloading. Imagine a situation where a programmer using your stuff calls up and asks "Why did my screen just turn red?" and you replay "You must have set CSound->rate.set(11232)".

You want to set up your classes so that even though the implementation is black-boxed, the interface isn't. An end user should be able to guess what effects setting the rate, volume, whatever has on the class. So if it doesn't matter at what point how or why you set a variable, you don't need to use get/set functions.

As a beginner, it's hard to tell what variables need get/set functions and what ones don't, so you probably want to do it anyway, but as you gain experience, you should get a better idea of how everythings going to work before you start coding.

Hope I wasn't too confusing and didn't rant too much.


Edited by - logistix on 2/2/00 5:24:17 PM
-the logistical one-http://members.bellatlantic.net/~olsongt

This topic is closed to new replies.

Advertisement