I
Immortal Nephi
I want to create an interface of a door. The client is able to see
the implementation behind the interface because inheritance is
present. The code is stored in the header. How do I hide the
implementation?
A door is an abstraction of a particular arrangement of a rectangular
piece of material with hinges and a doorknob. It has four routines of
Open(), Close(), isOpened(), and isClosed().
And the doorknob is in turn an abstraction of a particular formation
of brass, nickel, iron, or steel. It has four routine of Lock(),
Unlock(), IsLocked(), and IsUnlocked().
And also, Color_Knob is in turn an abstraction of some colors.
How do I put three objects of Door, Knob, and Color_Knob together
into a function or another class. Is it the way how design looks
good?
The inheritance does not make any sense, but it should be
composition. Open() and Close() belongs to Door and they have no
reasons to be inherited into doorknob class nor color_knob class.
Also, Lock() and Unlock() belongs to doorknob class and should not be
inherited into color_knob.
Should knob_door and color_knob objects be created into memory and
place them in Door class like composition?
For example:
/* Header Door.h */
class Door
{
public:
Door() {}
virtual ~Door() {}
virtual void Open() {}
virtual void Close() {}
virtual bool IsOpened() { return m_door; }
virtual bool IsClosed() { return !m_door; }
private:
bool m_door;
};
class Knob : public Door
{
public:
Knob() {}
virtual ~Knob() {}
virtual void Lock() {}
virtual void Unlock() {}
virtual bool IsLock() { return m_Knob; }
virtual bool IsUnlocked() { return !m_Knob; }
private:
bool m_Knob;
};
class Color_Knob : public Knob
{
public:
Color_Knob() {}
virtual ~Color_Knob() {}
void Select_Brass() { color = Brass; }
void Select_Nickel(){ color = Nickel; }
void Select_Iron(){ color = Iron; }
void Select_Steel(){ color = Steel; }
bool IsBrass() { return color == Brass; }
bool IsNickel() { return color == Nickel; }
bool IsIron() { return color == Iron; }
bool IsSteel() { return color == Steel; }
private:
enum Color
{
Brass,
Nickel,
Iron,
Steel
} color;
Color Get_Color() { return color; }
};
#include “Door.h”
int main()
{
Color_Knob color_knob;
Knob knob;
Door door;
color_knob.Select_Brass();
bool bColor;
bColor = color_knob.IsBrass();
bColor = color_knob.IsIron();
door.Close();
knob.Lock();
return 0;
}
the implementation behind the interface because inheritance is
present. The code is stored in the header. How do I hide the
implementation?
A door is an abstraction of a particular arrangement of a rectangular
piece of material with hinges and a doorknob. It has four routines of
Open(), Close(), isOpened(), and isClosed().
And the doorknob is in turn an abstraction of a particular formation
of brass, nickel, iron, or steel. It has four routine of Lock(),
Unlock(), IsLocked(), and IsUnlocked().
And also, Color_Knob is in turn an abstraction of some colors.
How do I put three objects of Door, Knob, and Color_Knob together
into a function or another class. Is it the way how design looks
good?
The inheritance does not make any sense, but it should be
composition. Open() and Close() belongs to Door and they have no
reasons to be inherited into doorknob class nor color_knob class.
Also, Lock() and Unlock() belongs to doorknob class and should not be
inherited into color_knob.
Should knob_door and color_knob objects be created into memory and
place them in Door class like composition?
For example:
/* Header Door.h */
class Door
{
public:
Door() {}
virtual ~Door() {}
virtual void Open() {}
virtual void Close() {}
virtual bool IsOpened() { return m_door; }
virtual bool IsClosed() { return !m_door; }
private:
bool m_door;
};
class Knob : public Door
{
public:
Knob() {}
virtual ~Knob() {}
virtual void Lock() {}
virtual void Unlock() {}
virtual bool IsLock() { return m_Knob; }
virtual bool IsUnlocked() { return !m_Knob; }
private:
bool m_Knob;
};
class Color_Knob : public Knob
{
public:
Color_Knob() {}
virtual ~Color_Knob() {}
void Select_Brass() { color = Brass; }
void Select_Nickel(){ color = Nickel; }
void Select_Iron(){ color = Iron; }
void Select_Steel(){ color = Steel; }
bool IsBrass() { return color == Brass; }
bool IsNickel() { return color == Nickel; }
bool IsIron() { return color == Iron; }
bool IsSteel() { return color == Steel; }
private:
enum Color
{
Brass,
Nickel,
Iron,
Steel
} color;
Color Get_Color() { return color; }
};
#include “Door.h”
int main()
{
Color_Knob color_knob;
Knob knob;
Door door;
color_knob.Select_Brass();
bool bColor;
bColor = color_knob.IsBrass();
bColor = color_knob.IsIron();
door.Close();
knob.Lock();
return 0;
}