C++ myprogramming lab Q 10897: (not a whole program, no int main!) Write the imp
ID: 3832019 • Letter: C
Question
C++ myprogramming lab Q 10897: (not a whole program, no int main!)
Write the implementation (.cpp file) of the GasTank class. The full specification of the class is:
A data member named amount of type double .
An data member named capacity of type double .
A constructor that accepts a parameter of type double . The value of the parameter is used to initialize the value of capacity. The constructor also sets amount to zero.
A function named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter . However, if the value of amount is increased beyond the value of capacity, amount is set to capacity.
A function named useGas that accepts a parameter of type double . The value of the amount data member is decreased by the value of the parameter . However, if the value of amount is decreased below 0, amount is set to 0.
A function named isEmpty that accepts no parameters and returns a boolean value . isEmpty returns true if the value of amount is less than 0.1, and false otherwise.
A function named isFull that accepts no parameters and returns a boolean value .. isFull returns true if the value of amount is greater than capacity-0.1, and false otherwise.
A function named getGasLevel that accepts no parameters . getGasLevel returns the value of the amount data member.
A function named fillUp that accepts no parameters and returns a double . fillUp increases amount to capacity and returns the difference between the value of capacity and the original value of amount (that is, the amount of gas that is needed to fill the tank to capacity).
Explanation / Answer
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class GasTank
{
// Declaring variables
private:
double amount;
double capacity;
public:
//Function declarations
GasTank(double capacity);
void addGas(double amt);
void usegas(double amt);
bool isEmpty();
bool isFull();
double getGasLevel();
double fillUp();
};
// parameterized constructor
GasTank::GasTank(double capacity)
{
this->capacity = capacity;
this->amount = 0;
}
// This function will add gas
void GasTank::addGas(double amt)
{
if (amt > capacity)
this->amount = capacity;
else
this->amount += amt;
}
// This function will subtract gas
void GasTank::usegas(double amt)
{
if (this->amount - amt < 0)
this->amount = 0;
else
this->amount -= amt;
}
// This function will check whether the tank is empty or not
bool GasTank::isEmpty()
{
if (amount < 0.1)
return true;
else
return false;
}
// This function will check whether the tank is full or not
bool GasTank::isFull()
{
if (amount > (capacity - 0.1))
return true;
else
return false;
}
// This function will return the gas level in the tank
double GasTank::getGasLevel()
{
return amount;
}
// This function will fill the tank to full
double GasTank::fillUp()
{
this->amount += (capacity - amount);
return (capacity - amount);
}
_________________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.