I need help on my C++ project The Class DoorKnob represents some kind of turnabl
ID: 3771852 • Letter: I
Question
I need help on my C++ project
The Class DoorKnob represents some kind of turnable handle.
The instances knob and goldenknob were created by saying:
Knob ImageDoorKnob knob; By default, an unlockable door knob
Knob ImageDoorKnob goldenknob( true ); A lockable door knob
Based on this implementation of DoorKnob, define and implement the class SmartKnob (a .h file and a .cpp file). A Smart DoorKnob instance represents an internet connected Knob that sends its state information to other connected devices. The relationship between SmartDoorKnob and DoorKnob is shown in the class diagram below.
DoorKnob
^
|
|
|
SmartDoorKnob
Make sure your definition of SmartKnob supports the following kinds of constructors:
Locked Door // by default, a smart door knob that is locked
SmartKnob knob;
Unlocked Door // a smart door knob that is unlocked and lockable
// false indicates it is not locked
// true parameter indicated is lockable
Door UnlockedAndOpen( false, true );
The SmartKnob constructor must call the parent-class Knob constructor
----------------------------------------DriverSample.cpp -------------------------------------
#include <iostream>
#include <stdexcept>
#include <string>
#include "DoorKnob.h"
using namespace std;
int main()
{
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;
return 0;
}
---------------------------------DoorKnob.cpp-----------------------------------
#include <iostream>
#include <stdexcept>
#include <string>
#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; }
}
-----------------------------------------------DoorKnob.h------------------------------------------------
#ifndef DOORKNOB_H
#define DOORKNOB_H
#include <iostream>
using namespace std;
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;
};
#endif
Explanation / Answer
Based on your implemented code is not complied and given compilation errors.
Explanation:
These Compilation errors because of your main method doesn't declare the objects for DoorKnob..
Step 1) So I have created two objects one is knob..with default constructor will called..so the data members of
DoorKnob is assigned to false..
Step 2) The second object is golden knob ..this object will send the true value to DoorKnob parameterised
constructor so..It will start the functional of DoorKnob methods..
Step 3) you need to access the DoorKnob methods with the help of DoorKnob object called golden knob..
See the updated code for main method..
int main()
{
DoorKnob knob();
DoorKnob goldenknob(true);
goldenknob.turn();
goldenknob.push();
goldenknob.pull();
goldenknob.canBeLocked();
goldenknob.lock(); // only does something if the knob
// is lockable
goldenknob.unlock();
return 0;
}
So that It is working fine and eliminated compilation errors and run time errors also..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.