Class Car

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:





Car my_beemer(29); // 29 miles per gallon

my_beemer.add_gas(20); // Tank 20 gallons

my_beemer.drive(100); // Drive 100 miles

cout << my_beemer.get_gas() << "\n"; // Print fuel remaining

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.

#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

class Car
{
double fuel_mpg;
double miles;
double mpg;
double gasused;
public:
Car();
Car(double mpg);
void add_gas(double gas);
void drive(double miles);
void out_of_gas();
double get_gas();
double maxmiles;
double gaslevel;
};
Car::Car(double mpg)
{
fuel_mpg = mpg;
miles = 0;
gaslevel = 0.0;
cout<<"\nAverage of Car is ">>fuel_mpg>>"Miles/Gallon\n";
}
void Car::add_gas(double gas)
{
gaslevel=gaslevel+gas
maxmiles=mpg*gaslevel
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.
Thank you for your assistance I will try to keep it simple. As for the output Im asking for input so all that need to be in the int main correct?
So i fixed my code as follows Im still not sure what im missing according to the problem posted previously:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

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();
       };


       Car::Car(double mpg)
       {
       mpg = 0;
       
       gaslevel = 0.0;
       cout << "\nAverage of Car is " << mpg << "Miles/Gallon\n";
       }
       void Car::add_gas(double gas)
       {
       gaslevel=gaslevel+gas;
       }
       void drive(double miles)
       {
       cout<<"How many miles will you drive: ";
       cin >> miles;
       }
       double get_gas()
       {
       cout<<"You have "<<get_gas<<"Gallons Remaining\n";
       return 0;
       }
The program still is not running right
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?
Also, which mpg is 0 on line 21?
1
2
3
4
5
6
7
Car::Car(double defaultmpg)
       {
       mpg = defaultmpg;
       
       gaslevel = 0.0;
       cout << "\nAverage of Car is " << mpg << "Miles/Gallon\n";
       }
@Zhuge yes i meant to print fuel remaining instead, How would I do that? and @ats15 The problem says mpg must be 0 in the beginning.
No, the problem states that the fuel level is 0, not the mpg
The problem says mpg must be 0 in the beginning.


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.
Last edited on
Topic archived. No new replies allowed.