I think you don't understand what a class is for.
Let's start simple. You know what an
int is? Conceptually, when you make an
int, you are creating an object which is an integer.
int x;
Here, we have made an object of type
int, that we have called x. We can use this object for storing information - the information we can store is quite limited. Just a single integer value.
char y;
Here, we have made an object of type
char, that we have called y. We can use this object for storing information - the information we can store is quite limited. Just a single character.
What if we want to be a bit more sophisticated with data? What if we want to make an object that can do more than just store a single integer value, or a single char? Then we need classes. When we define a class, we are defining a new kind of object that we can make. Because these things can get complicated, we (sometimes) make it easy on ourselves by defining a class in such a way that we can easily think about it; we can easily develop a mental model of what that class object should and should not be able to do.
So, you have decided to create a class of type "odometer" that should be able to keep track of miles driven and fuel efficiency, in a car. The odometer will be fed in data that it needs to calculate miles driven and fuel efficiency. When you think of it, it may help to think of it as an actual odometer in a car - this will give you an intuitive feel for what it should be able to do. This is (to my mind - other people's opinions may differ) a big part of why we have OOP (object oriented programming); because it makes it easier for us to think about how to solve problems, which ultimately is the whole point.
Now, let's think about what an odometer should be able to do. What data does an actual car odometer have? Well, it keeps track of the total distance the car has ever travelled, and it keeps track of the distance on an individual trip, and (because it's a sophisticated modern odometer) it keep track of how much fuel has been used.
So, let's start fleshing this thing out:
1 2 3 4 5 6
|
class odometer
{
double totalDistanceEverTravelled;
double distanceTravelledThisTrip;
double fuelUsedThisTrip;
};
|
What kind of input does an odometer receive? Well, it gets told how far we've travelled, so let's have a function for that:
1 2 3 4 5 6 7 8
|
class odometer
{
double totalDistanceEverTravelled;
double distanceTravelledThisTrip;
double fuelUsedThisTrip;
void inputDistanceTravelledThisTrip(double);
};
|
Does it get anything else in? Yes, the amount of fuel used:
1 2 3 4 5 6 7 8 9
|
class odometer
{
double totalDistanceEverTravelled;
double distanceTravelledThisTrip;
double fuelUsedThisTrip;
void inputDistanceTravelledThisTrip(double);
void inputFuelUsedThisTrip(double);
};
|
Great. What do we get back out of this odometer? We want to be able to see the total distance travelled, and the distance travelled this trip, and the fuel efficiency:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class odometer
{
double totalDistanceEverTravelled;
double distanceTravelledThisTrip;
double fuelUsedThisTrip;
void inputDistanceTravelledThisTrip(double);
void inputFuelUsedThisTrip(double);
double getFuelEfficiency();
double getDistanceTravelledThisTrip();
double getDistanceTravelledEver();
};
|
Aha. We also need to be able to reset the trip distance (and fuel used) back to zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class odometer
{
double totalDistanceEverTravelled;
double distanceTravelledThisTrip;
double fuelUsedThisTrip;
void inputDistanceTravelledThisTrip(double);
void inputFuelUsedThisTrip(double);
double getFuelEfficiency();
double getDistanceTravelledThisTrip();
double getDistanceTravelledEver();
void resetTripAndFuelUsedToZero();
};
|
So, that seems to cover it. We have now modelled a real odometer - our class (once we write the functions) will be able to do everything we need.
How might we use this? Well, for any given car, we'll need to create one odometer. Since the assignment indicates that this is all about a single car, something like this:
1 2 3 4 5 6
|
int main()
{
odometer theCarsOdometer;
...
...
}
|
Let's take a look at what you originally did:
Odometer miles, fuel;
This creates TWO odometer objects. One is called miles, and one is called fuel. Why would you create TWO odometers when you only have ONE car to think about? Why would you call an odometer
miles
? It's not a distance, it's an odometer! Wy would you call an odometer
fuel
? An odometer isn't fuel, it's an odometer!
The above is a simplifed train of thought showing how you can think about this. Hopefully it's made clear how to go about designing and implementing classes. Making a class to model an odometer is very easy, as you can easily think about the real-world actual odometer to work out what it should and should not be capable of.