Re: How to hide implementation details?
Immortal Nephi <Immortal_Nephi@hotmail.com> wrote:
The client is able to see private data members and private member
functions in the class in header. The class should be interface.
You are creating rules that don't exist. There is no rule that says
private data members and private member functions cannot be in a header
file.
I did not say very clear. House is an abstraction. You write "class
house". How do you break complexity into simple code? You need to
divide or decompose house into several classes.
The answer is that you break it up according to behaviors, not according
to what takes up space. Don't think of what a physical house is made of,
think about what a House has to do in your program, then you will have a
better idea of how to break things up.
Several classes are derived from house, which is called windows,
doors, siding, wiring, plumbing, insulation, etc. I focus to
decompose door class. Knob class is derived from door class. Colored
knob class is derived from knob class.
Does a window do the same thing as a house, just in a different way? I
don't think so, that means deriving window from house is a bad idea.
Read up on the Liskov Substitution Principle. Also, you might want to
take this discussion over to comp.object.
I hope that you know what I mean. I talk about decomposing classes.
You can notice Lock() and Unlock() are in knob class and should not be
inherited into colored knob class. Also, Open() and Close() from door
class should not be inherited into knob class.
So don't use inheritance.
I guess you are suggesting not to use inheritance hierarchy. Then,
write two base classes of knob and colored knob. Put them into door
class as composition.
What does a door do? What behavior does it have? What does a door need
the knob to do for it? How is a knob's behavior the same, or different
from a colored knob's behavior? All these questions need to be answered
before the above can be decided.
Think about behavior, assign intention to every object in your program,
then all of this will start making more sense.