So I have to create a program that asks the user for the amount of miles and gallons that they used in their car for the trip and compute the miles per gallon and miles per gallon total. So is there any way that I can corporate the formulas like totalMiles += miles and if(gallons != 0) then totalGallons += gallons in the header file?
This is my header file below
Now the object's values don't make sense in the real world. How can they have traveled 1000 miles in this trip and only 200 miles total? If they travelled 1000 miles on 50 gallons then they got 20 miles per gallon, but if you ask for the mileage, it will return 1, due to the call to setMilesAndGallons().
Logically, the way to modify the class is to add a new trip. That means adding both the miles and the gallons at the same time. Store the trip miles and gallons, as well as the overall totals. Compute the miles per gallon from miles and gallons. That way it's impossible for it to be inconsistent. With this interface, it's impossible for the values to become inconsistent:
Another way: ( it would be better to model Trips and sub-tTrips better with a data structure of a trip array or even better an extra Journey class. But a single class does the job, not unlike a mechanical speedometer/trip meter from the Steam Age
#include <iostream>
class Trip{
private:
double miles = 0;
double gallons = 0;
double total_miles = 0;
double total_gallons = 0;
public:
Trip(double aMiles = 0, double aGallons = 0){
miles = aMiles;
gallons = aGallons;
total_miles += miles;
total_gallons += gallons;
}
void addTrip(const Trip sub_trip){
total_miles += sub_trip.miles;
total_gallons += sub_trip.gallons;
}
double fuel_consumption(){
return total_miles / total_gallons;
}
double total_distance(){
return total_miles;
}
double total_fuel(){
return total_gallons;
}
};
using std::cin;
using std::cout;
using std::endl;
int main()
{
Trip myTrip;
double miles;
double gallons;
while(
cout << "Enter miles driven (-1 to quit): "
and cin >> miles
and miles != -1
){
cout << "Enter Gallons used: ";
cin >> gallons;
myTrip.addTrip( Trip(miles, gallons) );
cout << " MPG this trip: " << miles/gallons << endl;
cout << " Total MPG is: " << myTrip.fuel_consumption() << endl;
cout << "Total travelled: " << myTrip.total_distance() << endl;
cout << " Total fuel: " << myTrip.total_fuel() << endl << endl;
}
}
Enter miles driven (-1 to quit): 10
Enter Gallons used: 2
MPG this trip: 5
Total MPG is: 5
Total travelled: 10
Total fuel: 2
Enter miles driven (-1 to quit): 15
Enter Gallons used: 3
MPG this trip: 5
Total MPG is: 5
Total travelled: 25
Total fuel: 5
Enter miles driven (-1 to quit): 25
Enter Gallons used: 7
MPG this trip: 3.57143
Total MPG is: 4.16667
Total travelled: 50
Total fuel: 12
Enter miles driven (-1 to quit):
So is there any way that I can corporate the formulas like totalMiles += miles and if(gallons != 0) then totalGallons += gallons in the header file?
There is nothing sacred about header files so call them what you like eg "xyz_whatever_no_dot" and put any functions you like inside the class as a method and/or outside as a completely separate function. Give them the same name and see how that works. Try it out.
There are conventions involved but none that will break anything or blow up the linking if you decide to ignore them. Obviously file names have to be correct and c++ rules on syntax still apply etc.
As far as the mpg calculation requiring >0 gallons it is not unreasonable to force the users to meet a precondition of the function, if it is made clear, that gallons must be >0 and one simple way to handle that is by way of exception handling or some other simple error capture if statement in main(). Unless you really want them to be inline ( eg you want to enhance performance, and why not) the 'inline' keyword isn't a mandatory requirement).