The Class LightBulb represents an electrical device that shines Consider the fol
ID: 3771103 • Letter: T
Question
The Class LightBulb represents an electrical device that shines Consider the following diagrams below.
This instance was created by saying:
LightBulb offBulb( 60, false );
This instance was created by saying:
LightBulb onBulb;
Each LightBulb includes a wattage value and whether it is turned on or off. For the constructors shown above, here is the class definition (.h)
LightBulb( int wattage, bool isOn );
void light( );
void unlight( );
int getWattage( ) const;
bool isTurnedOn( ) const;
LightBulb(int wattage=50, bool isOn=true);
void light( ); // turn on the bulb
void unlight( ); // turn off the bulb
int getWattage( ) const;
bool isTurnedOn( ) const;
int myWattage;
bool myIsTurnedOn;
Based on the information shown here, a possible implementation (.cpp) for LightBulb is shown below.
LightBulb::LightBulb( int wattage, bool turnedOn ) :
myWattage( wattage ), myIsTurnedOn( turnedOn )
{ // empty... }
int LightBulb::getWattage( ) const
{
return( myWattage );
}
bool LightBulb::isTurnedOn( ) const
{
return( myIsTurnedOn );
}
void LightBulb::light( )
{
myIsTurnedOn = true;
}
void LightBulb::unlight( )
{
myIsTurnedOn = false;
}
Refrigerator r; // fridge with currentTemp=60, light is off
Refrigerator r1( 40 ); // fridge with currentTemp=40, light is off
Refrigerator r2( 35, true ); // fridge with currentTemp=35, light on
In addition, create these two operations on Refrigerator:
void Refrigerator::openDoor();
void Refrigerator::closeDoor();
on Refrigerator which properly turns on its bulb when the door opens and turns its bulb off when the door closes.
This instance was created by saying:
LightBulb offBulb( 60, false );
50Watt LightBulb, turned onThis instance was created by saying:
LightBulb onBulb;
Explanation / Answer
/****chegg.h*****/
class LightBulb{
public:
LightBulb(int wattage = 50, bool isOn = true);
void light();
void unlight();
int getWattage() const;
bool isTurnedOn() const;
private:
int myWattage;
bool myisTurnedOn;
};
class Refrigerator
{
public:
Refrigerator(int temp = 60, bool turnedOn = false);
void openDoor();
void closeDoor();
int getTemp() const;
bool myisTurnedOn() const;
private:
int currentTemp;
LightBulb light;
};
/****chegg.cpp*****/
#include "chegg.h"
#include <iostream>
using namespace std;
LightBulb::LightBulb(int wattage, bool turnedOn): myWattage( wattage), myisTurnedOn(turnedOn)
{
//empty
}
int LightBulb::getWattage() const
{
return myWattage;
}
bool LightBulb::isTurnedOn() const
{
return myisTurnedOn;
}
void LightBulb::light()
{
myisTurnedOn = true;
}
void LightBulb::unlight()
{
myisTurnedOn = false;
}
Refrigerator::Refrigerator(int temp, bool turnedOn): currentTemp(temp)
{
if(!turnedOn)
light.unlight();
}
void Refrigerator::openDoor()
{
light.light();
}
void Refrigerator::closeDoor()
{
light.unlight();
}
int Refrigerator::getTemp() const
{
return currentTemp;
}
bool Refrigerator::myisTurnedOn() const
{
return light.isTurnedOn();
}
int main()
{
LightBulb offBulb(60,false);
LightBulb onBulb;
Refrigerator r;
cout<<"fridge with currTemp = "<<r.getTemp()<<", light is "<<(r.myisTurnedOn()?"on":"off")<<endl;;
Refrigerator r1(40);
cout<<"fridge with currTemp = "<<r1.getTemp()<<", light is "<<(r1.myisTurnedOn()?"on":"off")<<endl;;
Refrigerator r2(35, true);
cout<<"fridge with currTemp = "<<r2.getTemp()<<", light is "<<(r2.myisTurnedOn()?"on":"off")<<endl;;
}