Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a function drive that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, and functions get_gas, to return the current fuel level, and add_gas, to tank up. Sample usage:
This is how far I have come on my code, I just need some guidance as whether or not im on the right track, and how to finish it because classes are extremely confusing to me Thank you in advance.
Try to keep it simple. Also, put some comments in your code, so others can understand what you are doing.
For now, you should have only one constructor, that takes the mpg, a function to print the gas level remaining, a function to add gas, and a function to drive. The only private variables are the mpg and the gas in the tank.
Also, when you post in this forum, please use the code tags, <> button
1 2 3 4 5 6 7 8 9 10 11
class Car
{
double gaslevel; //how much fuel is left in the tank
double mpg; //the mpg
public:
Car(double mpg); //constructor
void add_gas(double gas);
void drive(double miles);
double get_gas();
};
In your code you have fuel_mpg and mpg. You don't need both. You also have a constructor that takes no parameters. Think if you need that. The problem did not ask for maxmiles or for gasused, so don't put them in for now.
In the drive() method, you aren't doing anything to reduce the fuel in the tank like you should. Also, on line 37 you are trying to print get_gas; did you meant to print the fuel left instead?
If that was the case why bother having a mpg parameter :P
Also remember when you drive X miles you are going to have to remove X/MPG from the gas. Since if you have 10 mpg and you drive 20 miles you will have used 2 gallons of gas. So you might want to fix
1 2 3 4 5
void drive(double miles)
{
cout<<"How many miles will you drive: ";
cin >> miles;
}
To something like:
1 2 3 4 5 6
void drive(double miles)
{
cout<<"How many miles will you drive: ";
cin >> miles;
gaslevel -= miles/mpg;
}
*Edit I would suggest you keep your code style the same throughout. Highly recommend each level of braces you indent.