Answered step by step
Verified Expert Solution
Question
1 Approved Answer
car.cpp #include #include car.h // define custom constructor // define get information functions // define change information functions // define private helper functions car.h #ifndef
car.cpp #include#include "car.h" // define custom constructor // define get information functions // define change information functions // define private helper functions
car.h
#ifndef CAR_H #define CAR_H class Car { public: // public member functions // constructors Car(){} // default constructor defined here Car(double, double); // initialize mpg and fuelCapacity // get information double getMileage() const; double getAvailableFuel() const; double getFuelCapacity() const; // change information bool drive(double); void refuel(); private: // data members, only accessible by member functions double mpg = 20; double mileage{5}; // number of miles driven (odometer) double availableFuel{2}; // amount of gas in tank double fuelCapacity{15}; // how many gallons tank can hold // private helper functions void addMiles(double); void useFuel(double); }; #endif
main.cpp
#include#include #include #include "car.h" using namespace std; void takeTrip(Car*, int); int main() { // create a car double mpg = 24.5; double fuelTank = 18; Car carA{mpg, fuelTank}; // display 2 decimal places on numbers cout > miles; while (cin.fail()){ cin.clear(); cin.ignore(std::numeric_limits ::max(), ' '); cout > miles; } double startingFuel = drivingCar->getAvailableFuel(); double startingMileage = drivingCar->getMileage(); if (drivingCar->drive(miles)){ cout getMileage() getAvailableFuel() getAvailableFuel() refuel(); cout getAvailableFuel() Project Description: Complete the definitions of the functions for the Car class in the car.cpp file. The class definition and function prototypes are in the provided car.h header file. A testing program is in the provided main.cpp file. You don't need to change anything in car.h or main.cpp. Submission: Submit the completed car.cpp file and a screenshot of your output on Blackboard. Due: Friday, February 15h, 2019, end of day. The Car class has the following: private data and helper functions that are only accessible by the class member functions: mpg-how many miles the car can go on a gallon of fuel mileage-the current number of miles on the car's odometer availableFuel-the current number of gallons of fuel in the car's tank fuelCapacity-the maximum number of gallons of fuel that can go in the car's tank add Mi les-this adds miles to the data member mi 1e age useFuel-this subtracts gallons of fuel from the data member availableFuel publicly available functions: Car default constructor-this is defined in car.h, you don't need to define in car.cpp Car custom constructor-takes in values to initialize mpg and fuelCapacity getMileage-returns the current value of data member mileage getAvailableFuel-returns the current value of data member availableFuel getFuelCapacityreturns the current value of fuelCapacity drive-takes in a number of miles for the trip as a parameter. It will first calculate the number of gallons of fuel needed for the trip given the number of miles and the car's mpg. If the number of gallons needed exceeds the gallons available, the function will return a value of false. If the number of gallons needed is available, drive will call the helper addMiles function with the number of miles driven, call the helper useFuel function with the number of gallons used, and return a value of true. refuel-will set the value of data member availableFuel to the value of data member fuelCapacity Sample output: See the following screenshot for sample output using the provided main.cpp testing program. Enter miles for trip 1: 78 Trip 1 not taken. Need to refuel. Current fuel level: 2.00 New fuel level: 24.50 Enter miles for trip 2: 45 **Trip 2 taken.* Miles driven: 45.00 Starting mileage: 5.00 Ending mileage: 50.00 Gallons used: 2.50 Gallons renaining 22.00 Enter miles for trip 3: 67 **Trip 3 taken.** Miles driven: 67.00 Starting mileage: 50.00 Ending mileage: 117.00 Gallons used: 3.72 Gallons remaining 18.28
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started