Program name: pgm2.cpp Description: Define a class called Odometer that will con
ID: 3886496 • Letter: P
Question
Program name: pgm2.cpp
Description:
Define a class called Odometer that will contain member variables to define the number of miles driven, the number of gallons, and the fuel efficiency (miles per gallon). The class should have a default constructor that will initialize all the variables to zero. It should also have a constructor that will take the number of miles driven and the number of gallons as parameters. This constructor will calculate the miles per gallon. There will be a function that can add a trip and a function to check mileage. Include a function that will print out the information from the function. Write a main program that will test the class. You should create at least three trips and make sure all of the member functions are executed and work properly.
Ignore for this assignment: Name your program pgm2.cpp.
Put your class definition in odometer.hpp. Your member functions should be in odometer.cpp, and your main function that implements and tests the class should be in main.cpp. Turn in all three files and a makefile.
Class variable definitions:
miles driven – integer number
gallons – should be able to hold a fractional number
miles per gallon – should be able to hold a fractional number
Class member functions
Default constructor – zeros out all of the variables
Constructor – takes the number of gallons and number of miles and calculates miles per gallon – replaces previous values
Add trip – takes the number of gallons and number of miles and adds it to the previous values, calculating the new miles per gallon for the total trip
Check mileage – Given a number of gallons as a parameter, based on the miles per gallon in the class will return how many miles can be driven.
Print – prints out the miles, gallons, and miles per gallon
Requirements:
Test at least 3 trips
The following declarations should be able to be made:
Odometer od;
Odometer od1 (10, 100);
Sample output to check miles per gallon
Miles driven: 100 Gallons Used: 10 Miles Per Gallon: 10
Make sure pseudo code is written before coding is done.
Explanation / Answer
main.cpp
#include <iostream>
#include "odometer.hpp"
using namespace std;
int main()
{
//testing the class
cout << "Testing the Odometer class several times: " << endl << endl;
//testing the default constructor
cout << "Creating od with default constructor and printing: " << endl;
Odometer od;
od.print_odometer();
//testing the add_trip function, mpg will be 0 if gallons is 0
// in this case gallons is 20
cout << endl << "Adding a trip to 'od' and printing again: " << endl;
od.add_trip(1000, 20);
od.print_odometer();
//testing the check_mileage function for 8.5 gallons at current mpg and print
cout << endl << "Checking the mileage of od for 8.5 gallons: " << endl;
cout << "Miles left to be driven at current mileage: " <<
od.check_mileage(8.5) << " miles" << endl;
//testing non-default constructor
cout << endl << "Creating od1 with normal constructor and printing: " << endl;
Odometer od1(100, 10);
od1.print_odometer();
//add trip to this new odometer and print
cout << endl << "Adding a trip to od1 and printing again: " << endl;
od1.add_trip(15678, 265.597);
od1.print_odometer();
//check_mileage for 157.988 gallons at current mpg
// this shows it can handle decimal values
cout << endl << "Checking the mileage of od for 157.988 gallons: " << endl;
cout << "Miles left to be driven at current mileage: " <<
od.check_mileage(157.988) << " miles" << endl;
cout << endl << "Creating od2 with normal constructor with 0 gallons: " << endl;
Odometer od2(500, 0);
od2.print_odometer();
cout << endl << "Since gallons is 0 the constructor sets mpg to 0 because of";
cout << " possible divide by zero error." << endl;
cout << endl << "Adding a trip to od2 and printing again: " << endl;
od2.add_trip(1500, 0);
od2.print_odometer();
cout << endl << "Since after adding a trip gallons is still 0,";
cout << " so mpg is again set to 0" << endl;
cout << endl << "Since mpg is still 0 when we check mileage we can";
cout << " technically only travel 0 miles according to check_mileage:" << endl;
cout << "Miles left to be driven at 0 mpg for 15 gallons: " <<
od.check_mileage(15) << " miles" << endl;
return 0;
}
odometer.cpp
#include <iostream>
#include "odometer.hpp"
using namespace std;
//Default constructor
Odometer::Odometer()
{
miles = 0;
gallons = 0;
mpg = 0;
}
//Constructor; if gallons = 0, mpg has divide by zero error
// so if gallons == 0, mpg is set to 0
Odometer::Odometer(int mi, float gal)
{
miles = mi;
gallons = gal;
if(gallons == 0)
mpg = 0;
else
mpg = miles/gallons;
}
//add_trip function, same case as above if gallons still = 0
// otherwise it simply adds a trip to the odometer
void Odometer::add_trip(int mi, float gal)
{
miles += mi;
gallons += gal;
if(gallons == 0)
mpg = 0;
else
mpg = miles/gallons;
}
//check_mileage sees how many miles could be traveled based on
// how gallons are left and the current mpg
float Odometer::check_mileage(float gal)
{
return gal*mpg;
}
//print_odometer prints the contents of the odometer named
void Odometer::print_odometer()
{
cout << "Miles Driven: " << miles << " Gallons Used: " << gallons <<
" Current MPG: " << mpg << endl;
}
odometer.hpp
/*******************************************************************************
Description of the problem:
* This program implements the Odometer class
* We will be able to:
* print miles driven, gallons used, and miles
* per gallon
* add a trip and recalculate the mpg
* check the remaining miles we could drive given the number of
* gallons left
Psuedo Code:
Class Name: Odometer
Simulate an Odometer
Data:
miles - integer
gallons - float
miles per gallon - float
Mutator Functions:
Accesor Functions:
Function: Odometer
Default constructor - zero out all variables
Inputs: None
Outputs: None
Return: N/A (zero out all variables)
Function: Odometer
constructor that accepts miles and gallons and calculates mpg
Inputs: miles, gallon
Outputs: None
Return: N/A
miles = miles passed in
gallons = gallons passed in
miles per gallon = miles / gallons
Function: add_trip
takes the number of gallons and number of miles and adds it to
the previous values, calculating the new miles per gallon for
the total trip
Inputs: miles to add, gallons to add
Outputs: None
Return: N/A
miles += new miles
gallons += new gallons
mpg = miles / gallons
Function: check_mileage
Given a number of gallons as a parameter, based on the miles per
gallon in the class will return how many miles can be driven
Inputs: gallons in tank
Outputs: None
Return: miles that can be driven given current miles per gallon
return gallons in tank * miles per gallon
Function: print_odometer
prints out the miles, gallons, and miles per gallon
Inputs: None
Outputs: miles, gallons, and miles per gallon
Return: N/A
print miles
print gallons
print miles per gallon
*/
class Odometer
{
//variable
int miles;
float gallons;
float mpg;
public:
//default constructor
Odometer();
//constructor
Odometer(int mi, float gal);
//add_trip adds miles and gallons and recalculates mpg
void add_trip(int mi, float gal);
//check mileage returns how many miles left to driven given gallons
//and current mpg
float check_mileage(float gal);
//print_od prints miles driven, gallons used, and current mpg
void print_odometer();
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.