// vehicle2.cpp - program to compute the range in miles of a vehicle #include #include using namespace std; // declare Vehicle class class Vehicle { public: // prototypes for member functions (methods) // compute and return range int range(); // declare constructor and destructor Vehicle(int num_passengers, int gal_fuel_capacity, int miles_gallon); ~Vehicle(); // data members (attributes) string carMake; int passengers; // number of passengers in vehicle int fuel_capacity; // fuel tank capacity in gallons int mpg; // miles per gallon }; // Implement Constructor for Vehicle Vehicle::Vehicle(int num_passengers, int gal_fuel_capacity, int miles_gallon) { carMake = "unknown"; passengers = num_passengers; fuel_capacity = gal_fuel_capacity; mpg = miles_gallon; } // Implement Destructor for Vehicle Vehicle::~Vehicle() { cout << "Destructing... Run!" << endl; } // implement the member function range(). Note that :: is the scope resolution operator int Vehicle::range() { return mpg * fuel_capacity; } int main() { // create Vehicle objects and pass values to Vehicle constructor Vehicle minivan(7, 16, 21); Vehicle sportscar(2, 14, 12); int range1, range2; // ranges for minivan and sportscar cout << "Make of minivan? "; getline (cin, minivan.carMake); cout << "Make of sportscar? "; getline (cin, sportscar.carMake); // compute ranges, assume full tank range1 = minivan.range(); range2 = sportscar.range(); cout << "Minivan can hold " << minivan.passengers << " with a range of " << range1 << " miles.\n"; cout << "Sportscar can hold " << sportscar.passengers << " with a range of " << range2 << " miles.\n"; }