Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, create a base case named Vehicle that has the odometer reading. Class Ve

ID: 3727983 • Letter: I

Question

In C++, create a base case named Vehicle that has the odometer reading. Class Vehicle requires its inheritance to override its member function print. Derive from Vehicle classes Car and Truck. Classes Car and Truck member function print is to output their type and odometer reading (eg. Truck at 23000 miles). Show a main function that declares of an array of 100 pointers to Vehicle, calls the inventory function to initialize the array, prints the inventory while adding 100 miles to each of the vehicle's odometer reading.

Given: int inventory(Vehicle * v[], int maxsize); //returns the actual number of vehicles initialized

Explanation / Answer

#include <iostream>

#include <cstdlib>

using namespace std;

class Vehicle{

protected:

int odometer;

public:

Vehicle(int o){

odometer = o;

}

virtual void print() {

cout<<"odometer: "<<odometer<<endl;

}

};

class Car : public Vehicle {

private:

string type;

public:

Car(string t, int o):Vehicle(o){

type = t;

}

void print() {

cout<<"odometer: "<<odometer<<", Type: "<<type<<endl;

}

};

class Truck : public Vehicle {

private:

string type;

public:

Truck(string t, int o):Vehicle(o){

type = t;

}

void print() {

cout<<"odometer: "<<odometer<<", Type: "<<type<<endl;

}

};

int inventory(Vehicle * v[], int maxsize) {

int count = 0;

for(int i=0; i<maxsize; i++) {

if(v[i] != NULL)

count++;

}

return count;

}