I need help on my C++ project. The Class Battery represents some kind of charged
ID: 3771764 • Letter: I
Question
I need help on my C++ project.
The Class Battery represents some kind of charged electrical unit.
The instances doubleA and nineVolt were created by saying:
Double A BatteryBattery doubleA; By default, a battery that holds 1.5 volts
Nine Volt BatteryBattery nineVolt( 9 ); A battery holding 9 volts
Based on this implementation of Battery, define and implement the class WallClock (a .h file and a .cpp file). A WallClock instance represents a timepiece powered by a battery. The relationship between WallClock and Battery is shown in the class diagram below.
Wallclock ---------> Battery
Make sure your definition of WallClock supports the kinds of constructors shown above. In addition, create these two operations on WallClock:
(1) make a new member operation defined as:
void WallClock::turnOn();
on WallClock which starts running the battery.
(2) make a new member operation defined as:
void WallClock::turnOff();
on WallClock which stops running the battery.
As currently defined, can any of the member operations you defined on the class WallClock be marked const?
DriverCode.cpp
#include <iostream>
#include <stdexcept>
#include <string>
#include "Battery.h"
using namespace std;
int main()
{
Battery ();
Battery ( int volt );
void startRunning();
void stopRunning();
bool isCharged() const;
void charge();
void uncharge();
bool isCharged;
double myVolts;
return 0;
}
Battery.cpp
#include <iostream>
#include <stdexcept>
#include <string>
#include "Battery.h"
Battery::Battery() {
myVolts = 1.5;
isCharged = true;
}
Battery::Battery( double volts ) {
myVolts = volts;
isCharged = true;
}
void Battery::startRunning() {
if (isCharged) {
cout << "start running..." << endl;
}
else {
cout << "battery is dead!" << endl;
}
}
void Battery::stopRunning() {
if (isCharged) {
cout << "stopped running..." << endl;
}
else {
cout << "battery is dead!" << endl;
}
}
bool Battery::isCharged() const {
return( isCharged );
}
void Battery::charge() {
isCharged=true;
}
void Battery::uncharge() {
isCharged=false;
}
Battery.h
#ifndef BATTERY_H
#define BATTERY_H
#include <iostream>
using namespace std;
class Battery
{
public:
Battery ();
Battery ( int volts );
void startRunning();
void stopRunning();
bool isCharged() const;
void charge();
void uncharge();
private:
double myVolts;
bool isCharged;
};
#endif
Explanation / Answer
//mistakes are highlighted
Battery.h
#ifndef BATTERY_H
#define BATTERY_H
#include <iostream>
using namespace std;
class Battery
{
public:
Battery ();
Battery ( double volts );
void startRunning();
void stopRunning();
bool isCharged() const;
void charge();
void uncharge();
private:
double myVolts;
bool isCharge; //donot use same name for function and varaible previously it was isCharged
};
#endif
Battery.cpp
#include <iostream>
#include <stdexcept>
#include <string>
#include "Battery.h"
Battery::Battery() {
myVolts = 1.5;
isCharge = true;
}
Battery::Battery( double volts ) {
myVolts = volts;
isCharge = true;
}
void Battery::startRunning() {
if (isCharge) {
cout << "start running..." << endl;
}
else {
cout << "battery is dead!" << endl;
}
}
void Battery::stopRunning() {
if (isCharge) {
cout << "stopped running..." << endl;
}
else {
cout << "battery is dead!" << endl;
}
}
bool Battery::isCharged() const {
return( isCharge );
}
void Battery::charge() {
isCharge=true;
}
void Battery::uncharge() {
isCharge=false;
}
a.h
#ifndef A_H
#define A_H
#include "Battery.h"
//create class Wallclock which extends Battery class
class Wallclock : public Battery
{
public:
Wallclock ();
Wallclock ( double volts );
//declare two methods turnOn and turnOff
void turnOn();
void turnOff();
};
#endif
Wallclock.cpp
#include <iostream>
#include "a.h"
using namespace std;
//pass argument volts to parent Battery class
Wallclock::Wallclock(double volts) : Battery(volts){
}
//define method turnOn to call startRunning method of Battery class
void Wallclock::turnOn(){
startRunning();
}
//define method turnOff to call stopRunning method of Battery class
void Wallclock::turnOff(){
stopRunning();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.