SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Defining Classes
Exercise:
Programming Projects
Question:11 | ISBN:9780321531346 | Edition: 7

Question

Define a class called Odometer that will be used to track fuel and mileage

for an automotive vehicle. Include private member variables to track the

miles driven and the fuel efficiency of the vehicle in miles per gallon. The

class should have a constructor that initializes these values to zero.

Include a member function to reset the odometer to zero miles, a member function to set the fuel efficiency, a member function that accepts miles driven for a trip and adds it to the odometer’s total, and a member function that returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset.

Use your class with a test program that creates several trips with different

fuel efficiencies.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>
using namespace std;

/*----------class Odometer-------------*/
class Odometer{
    double milesDriven, fuelEfficent;

public:
    Odometer(); //default constructor that setsmiles and fuel to 0
    void resetMiles(); //member function to resetmiles
    void setFuelEfficent(double amount); // memberfunction to set fuel efficent
    void addMiles(double miles); //member functionadding miles
    double getConsumption(); //member function toreturn # of gallons of gas since last reset
    double getMiles(); //member function gettingmiles
    double getFuelEfficent(); //member functiongetting fuelefficent
};

Odometer::Odometer(){
milesDriven = 0;
fuelEfficent = 0;
};

void Odometer::resetMiles(){
milesDriven = 0;
}

void Odometer::setFuelEfficent(double amount){
fuelEfficent = amount;
}

void Odometer::addMiles(double miles){
milesDriven += miles;
}

double Odometer::getConsumption(){
    return milesDriven / 10; //made up value of 10miles per gallon
}

double Odometer::getMiles(){
return milesDriven;
}

double Odometer::getFuelEfficent(){
return fuelEfficent;
}


int main(){

Odometer test1; //constructor that sets all values to 0

cout << "Initial Odometer: " << endl;
cout << "Miles Driven: " << test1.getMiles()<< endl;
cout << "Fuel Efficency: " <<test1.getFuelEfficent() << endl << endl;

cout << "Adding 54.5 miles to odometer" <<endl;
test1.addMiles(54.5); //add 54.5 miles
cout << "Miles Driven: " << test1.getMiles()<< endl << endl;

cout << "The consumption rate of 54.5 is: " <<endl;
   //find out the consumption rate
   cout << "Fuel Efficency: " <<test1.getConsumption() << " miles per gallon(MPG)" <<endl << endl;

cout << "Reseting the odometer" << endl;
test1.resetMiles(); //reset miles
cout << "Miles Driven: " << test1.getMiles()<< endl;

return 0;
}

0 0

Discussions

Post the discussion to improve the above solution.