Write the implementation (.cpp file) of the GasTank class of the previous exerci
ID: 3773661 • Letter: W
Question
Write the implementation (.cpp file) of the GasTank class of the previous exercise. 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 ofcapacity.
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 tocapacity.
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 ofamount 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 isgreater 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> // Header File
using namespace std;
// Class Gas Tank
class GasTank
{
private: double amount,capacity; // Data Members
public:
GasTank(double cap) // Constructor
{
capacity=cap; // Assign Capacity to Provided Capacity
}
double addGas(double inc) // Add Some Gas to the amount
{
if((amount+inc)>capacity) // Check Condition for overflow
{
amount=capacity;
}
else
{
amount=amount+inc;
}
}
double useGas(double use)
{
if((amount-use)<0) // Check Condition for UnderFlow
amount=0.0;
else
amount=amount-use;
}
bool isEmpty() // Check Empty Status
{
if(amount< 0.1)
return true;
return false;
}
bool isFull() // Check Full Status
{
if(amount>(capacity-0.1))
return true;
return false;
}
double getGasLevel() // Return Gas Level
{
return amount;
}
double fillUp() // Required Gas to Fill Completely
{
return (capacity-amount);
}
};
// One test Case Program
int main()
{
GasTank gt1(10);
cout<<gt1.isEmpty()<<endl;
cout<<gt1.getGasLevel()<<endl;
gt1.addGas(1);
cout<<gt1.getGasLevel()<<endl;
cout<<gt1.isEmpty()<<endl;
cout<<gt1.isFull()<<endl;
gt1.addGas(2);
cout<<gt1.getGasLevel()<<endl;
gt1.addGas(5);
cout<<gt1.getGasLevel()<<endl;
gt1.addGas(10);
cout<<gt1.getGasLevel()<<endl;
cout<<gt1.isFull()<<endl;
gt1.useGas(5);
cout<<gt1.getGasLevel()<<endl;
cout<<gt1.fillUp()<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.