You are to use a separate List class that is supplied below (files list.h and list.cpp).
// list.h: a simple list class
// Assumes a class Vehicle, and uses pointers to
// members of that class
#ifndef LIST_H
#define LIST_H
#include "vehicle.h"
const MaxVehicles = 50;
class List {
Vehicle* list[MaxVehicles]; // an array of pointers to Vehicle
int num; // the current number of vehicles on the list
public:
List(); // default constructor
void insert(Vehicle* ); // insert another vehicle
void sort_inventory(); // sort into order using to key_inventory()
void print(); // print the list in its current order
};
#endif
// list.cpp: a simple List class to maintain a list of vehicles
// Assumes a class Vehicle, and uses pointers to
// members of that class
// The List class must have member functions
// key_inventory(), to determine inventory order for sorting
// print(), to print the whole list
#include "list.h"
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
List::List()
{
num = 0;
}
void List::insert(Vehicle* vehicle)
{
if (num >= MaxVehicles) {
cout << "Overflow of List of Vehicles" << endl;
exit(1);
}
list[num++] = vehicle;
}
void List::sort_inventory() // sort by inventory number
{
int i, j;
for (i = 0; i < num - 1; i++)
for (j = 0; j < num - 1; j++)
if (strcmp(list[j]->key_inventory(), list[j+1]->key_inventory() )
> 0) {
Vehicle* temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
void List::print()
{
cout << "List of all vehicles:" << endl;
for (int i = 0; i < num; i++)
list[i]->print();
}
Here is code you should use for the Vehicle class (file vehicle.h):
// vehicle.h -- base class declaration
#ifndef VEHICLE_H
#define VEHICLE_H
class Vehicle {
protected:
char* inventory_code;
int delivery_date;
public:
Vehicle();
Vehicle(char* code, int date);
~Vehicle();
virtual void print();
char* key_inventory();
};
#endif
Here the member function key_inventory returns
the inventory_code as a string. This member function
is used inside List to sort items according to
the value returned. The data constructor
needs to allocate storage for the string and the destructor needs
to delete the string. This version of the print function
will print the two data members.The class Truck should have data members int capacity and char* name. The constructor for Truck has 4 input parameters, and it can call the constructor for Vehicle to initialize the data members inventory_code and delivery_date. The function print in Truck should use the print in Vehicle and then print the additional two members.
The class Car should be similarly derived from Vehicle, with data members char* name (the same as Truck and int mileage (different from Truck.
Creating test data can be done with constructors, as in the sample main program:
#include <iostream.h>
#include "list.h"
#include "vehicle.h"
#include "truck.h"
#include "car.h"
int main()
{
List list;
Vehicle v1 ("F99-2387", 990923);
list.insert(&v1);
Vehicle v2 ("S98-4572", 981014);
list.insert(&v2);
Vehicle v3 ("F99-4587", 990817);
list.insert(&v3);
Truck t1 ("F97-4747", 970810, 200, "Dodge Ram");
list.insert(&t1);
Truck t2 ("F96-2122", 960920, 150, "Ford F250");
list.insert(&t2);
Car c1 ("F98-6569", 981029, 24, "Chevy Classic");
list.insert(&c1);
Car c2 ("F99-2252", 990817, 28, "Toyota Camery");
list.insert(&c2);
Car c3 ("S97-2343", 970321, 30, "Mazda Protege");
list.insert(&c3);
list.sort_inventory();
list.print();
}
List of all vehicles: Inventory Code: F96-2122, Delivery Date: 960920 Truck, capacity: 150, name: Ford F250 Inventory Code: F97-4747, Delivery Date: 970810 Truck, capacity: 200, name: Dodge Ram Inventory Code: F98-6569, Delivery Date: 981029 Car, mileage: 24, name: Chevy Classic Inventory Code: F99-2252, Delivery Date: 990817 Car, mileage: 28, name: Toyota Camery Inventory Code: F99-2387, Delivery Date: 990923 Inventory Code: F99-4587, Delivery Date: 990817 Inventory Code: S97-2343, Delivery Date: 970321 Car, mileage: 30, name: Mazda Protege