How far do you really go with OO techniques?
Members - Reputation: 122
Posted 01 February 2000 - 07:43 PM
Members - Reputation: 160
Posted 01 February 2000 - 10:01 PM
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
Members - Reputation: 126
Posted 02 February 2000 - 11:20 AM
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
Members - Reputation: 122
Posted 02 February 2000 - 11:22 AM
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