The Class DoorKnob represents some kind of turnable handle. The instances knob a
ID: 640776 • Letter: T
Question
The Class DoorKnob represents some kind of turnable handle.
The instances knob and goldenknob were created by saying:
Each DoorKnob can be manipulated by the user and perhaps even locked. For the constructors shown above, here is the class definition (.h)
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;
Based on the information shown here, a possible implementation (.cpp) for DoorKnob is shown below.
myIsLockable=false;
myIsLocked=false;
myIsLockable=isLockable;
myIsLocked=false;
cout << "turning knob..." << endl;
cout << "knob is locked!" << endl;
cout << "pushing knob..." << endl;
cout << "knob is locked!" << endl;
cout << "pulling knob..." << endl;
cout << "knob is locked!" << endl;
return( myIsLockable );
if (myIsLockable) { myIsLocked=true; }
if (myIsLockable) { myIsLocked=false; }
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
Explanation / Answer
Class Doorknob
{
Public:
DoorKnob() {
myIsLockable=false;
myIsLocked=false;
}
DoorKnob( bool isLockable ) {
myIsLockable=isLockable;
myIsLocked=false;
}
void turn() {
if (!myIsLocked) {
cout << "turning knob..." << endl;
} else
cout << "knob is locked!" << endl;
}}
void push() {
if (!myIsLocked) {
cout << "pushing knob..." << endl;
} else
cout << "knob is locked!" << endl;
}}
void pull() {
if (!myIsLocked) {
cout << "pulling knob..." << endl;
} else
cout << "knob is locked!" << endl;
}}
bool canBeLocked() const {
return( myIsLockable );
}
void lock() {
if (myIsLockable) { myIsLocked=true; }
}
void unlock() {
if (myIsLockable) { myIsLocked=false; }
}
}
Class Door:Doorknob
{ bool myknob;
Public:
void open()
{Doorknob mk;
Mk->Doorknob(myknob);
Mk->turn();
Mk->push();
}
void close()
{Doorknob mk;
Mk->Doorknob(myknob);
Mk->turn();
Mk->pull();
}
}
Void main()
{Door d;
Bool choice
Cout<<