28 The Class DoorKnob represents some kind of turnable handle. The instances kno
ID: 640786 • Letter: 2
Question
28
The Class DoorKnob represents some kind of turnable handle.
The instances knob and goldenknob were created by saying:
DoorKnob knob;
By default, an unlockable door knob
DoorKnob goldenknob( true );
A lockable door knob
Each DoorKnob can be manipulated by the user and perhaps even locked. For the constructors shown above, here is the class definition (.h)
Class Diagram
Class Definition (.h file)
DoorKnob
DoorKnob ();
DoorKnob ( bool isLockable );
void turn();
void push();
void pull();
bool canBeLocked() const;
void lock(); // only does something if the knob
// is lockable
void unlock();
bool myIsLockable;
bool isLocked;
class DoorKnob {
public:
DoorKnob ();
DoorKnob ( bool isLockable );
void turn();
void push();
void pull();
bool canBeLocked() const;
void lock(); // only does something // if the knob is lockable
void unlock();
private:
bool myIsLockable;
bool isLocked;
};
Based on the information shown here, a possible implementation (.cpp) for DoorKnob is shown below.
DoorKnob::DoorKnob() {
myIsLockable=false;
myIsLocked=false;
}
DoorKnob::DoorKnob( bool isLockable ) {
myIsLockable=isLockable;
myIsLocked=false;
}
void DoorKnob::turn() {
if (!myIsLocked) {
cout << "turning knob..." << endl;
} else
cout << "knob is locked!" << endl;
}
}
void DoorKnob::push() {
if (!myIsLocked) {
cout << "pushing knob..." << endl;
} else
cout << "knob is locked!" << endl;
}
}
void DoorKnob::pull() {
if (!myIsLocked) {
cout << "pulling knob..." << endl;
} else
cout << "knob is locked!" << endl;
}
}
bool DoorKnob::canBeLocked() const {
return( myIsLockable );
}
void DoorKnob::lock() {
if (myIsLockable) { myIsLocked=true; }
}
void DoorKnob::unlock() {
if (myIsLockable) { myIsLocked=false; }
}
Based on this implementation of DoorKnob, define and implement the class Door (a .h file and a .cpp file). A Door instance represents a partitioning device that incorporates a DoorKnob. The relationship between Door and DoorKnob is shown in the class diagram below.
Make sure your definition of Door supports the following kinds of constructors:
// by default, a door that whose knob is lockable and is locked
Door d;
// a door that is not lockable and is unlocked
Door UnlockedAndOpen( false, true );
In addition, create these two operations on Door:
(1)make a new member operation defined as:
void Door::open();
on Door which unlocks, turns and pushes on the door
DoorKnob knob;
By default, an unlockable door knob
DoorKnob goldenknob( true );
A lockable door knob
Explanation / Answer
//DoorKnob.h
class DoorKnob {
public:
DoorKnob ();
DoorKnob ( bool isLockable );
void turn();
void push();
void pull();
bool canBeLocked() const;
void lock(); // only does something // if the knob is lockable
void unlock();
private:
bool myIsLockable;
bool myIsLocked;
};
// DoorKnob.cpp
#include<iostream>
using namespace std;
#include "DoorKnob.h"
DoorKnob::DoorKnob() {
myIsLockable=false;
myIsLocked=false;
}
DoorKnob::DoorKnob( bool isLockable ) {
myIsLockable=isLockable;
myIsLocked=false;
}
void DoorKnob::turn() {
if (!myIsLocked) {
cout << "turning knob..." << endl;
} else
cout << "knob is locked!" << endl;
}
void DoorKnob::push() {
if (!myIsLocked) {
cout << "pushing knob..." << endl;
} else
cout << "knob is locked!" << endl;
}
void DoorKnob::pull() {
if (!myIsLocked) {
cout << "pulling knob..." << endl;
} else
cout << "knob is locked!" << endl;
}
bool DoorKnob::canBeLocked() const {
return( myIsLockable );
}
void DoorKnob::lock() {
if (myIsLockable) { myIsLocked=true; }
}
void DoorKnob::unlock() {
if (myIsLockable) { myIsLocked=false; }
}
// Door.h
#include "DoorKnob.h"
class Door{
private:
DoorKnob myDoorKnob;
public:
Door();
Door(bool notLockable, bool unLocked );
void open();
void close();
};
// Door.cpp
#include "Door.h"
Door::Door():myDoorKnob(true){
myDoorKnob.lock();
}
Door::Door(bool notLockable, bool unLocked ):myDoorKnob(notLockable){
if(unLocked)
myDoorKnob.unlock();
else
myDoorKnob.lock();
}
void Door::open(){
// on Door which unlocks, turns and pushes on the door
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.