can someone please help me? stuggling with C++ . thank you! Write the Car class
ID: 3856166 • Letter: C
Question
can someone please help me? stuggling with C++. thank you!
Write the Car class that has tank and speed as member variables. If a negative value is passed to the function pumpGas, an exception must be thrown. Also if a value is passed that will make the value of tank more than 15 another exception must be thrown. For example if you ask to pump 10 gallons of gas and you already have 8 gallons in the tank, so the exception that is thrown should indicate that you have asked for 3 extra gallons and the catch block should return 3 * $4 = $12 back to you.Explanation / Answer
The answer is as follows:
The code is as follows:
#include<iostream>
#include<exception>
#include<string>
using namespace std;
class myException : public exception{
private:
string message;
public:
myException(string msg){
message = msg
}
const char * what() const throw()
{
return message;
}
};
class Car {
private:
int tank;
int speed;
public:
Car(){
tank = 0;
speed = 0;
}
void pumpGas(int amt){
try {
if (amt < 0){
myException ex1("Amount is negative");
throw ex;
}
if ((amt + tank) > 15){
myException ex2(std::to_string((amt + tank) - 15) + " * $4=" + std::to_string(((amt + tank) - 15) * 4));
throw ex;
}
if ((amt + tank) <= 15) {
tank = tank + amt;
}
}
catch(exception& e)
{
cout << e.what();
}
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.