Write the implementation functions for a class to hold and display gas mileage data. Use the class definition provided below. Use the GasData struct given to hold the data for each gas stop. The miles member of the structure holds the odometer reading at the time gas is purchased. The initial mileage is a parameter of the constructor. An optional second constructor parameter can be used to specify an initial size for the dynamic array that holds the data. The main program following the class definition produces the output shown when used with the class.
struct GasData
{
float miles;
float gallons;
};
class Mileage
{
public:
Mileage(float initialMileage, int initialSize=2);
void addData(float mil, float gals);
void displayAllData();
void displayMileage();
private:
int size;
int count;
GasData *data;
float initialM;
void doubleArray();
};
int main()
{
Mileage md(1000);
md.addData(1200, 10);
md.addData(1500, 10);
md.addData(1750, 12.5);
md.addData(1900, 5);
md.addData(2000, 12.5);
md.displayAllData();
md.displayMileage();
return 0;
}
Output:
Mileage: 1200.0 Miles:200.0 Gallons:10.0 MilesPerGallon:20.0
Mileage: 1500.0 Miles:300.0 Gallons:10.0 MilesPerGallon:30.0
Mileage: 1750.0 Miles:250.0 Gallons:12.5 MilesPerGallon:20.0
Mileage: 1900.0 Miles:150.0 Gallons: 5.0 MilesPerGallon:30.0
Mileage: 2000.0 Miles:100.0 Gallons:12.5 MilesPerGallon: 8.0
Miles Per Gallon = 20.0