C++ Assignment 3

Hierarchy of Vehicles

Due July 4th 2006

 

 

 

The purpose of this assignment is to try out C++ features involving inheritance, virtual functions, and polymorphism. You are to code up a hierarchy of classes somewhat similar to the in-class examples. Your application should resemble the inventory list of vehicles in a car dealership, with a Vehicle class at the top level and two derived classes Truck and Car below that. You are to use and implement a separate List class. Its header is supplied below:

 

//  list.h: a simple list class

//  Assumes a class Vehicle, and uses pointers to

//  members of that class

 

class List {

 

  //an array of pointers to Vehicle

Vehicle* list;

int num; // the current number of vehicles on the list

 

public:

  List();         // default constructor

   ~List( );       //destructor

  void insert(const Vehicle& ); // insert another vehicle

 

void sort_inventory();

// sort into order using to key_inventory()

  

void print(); // print the list in its current order

};

 

list.cpp implements a simple List class to maintain a list of vehicles. It 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 and print(), to print the whole list

 

After you’ve finished the implementation for the List class, you should define three classes: Vehicle and, derived from Vehicle as public derived classes should be Truck and Car. Here is code you

should use for the Vehicle class (file vehicle.h):

 

// vehicle.h -- base class declaration

 

class Vehicle {

 

protected:

  char* inventory_code;

  int delivery_date;

 

public:

  Vehicle();

  Vehicle(char* code, int date);

  Vehicle(const Vehicle&);

  ~Vehicle();

  virtual void print();

  char* key_inventory();

};

 

 

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>

using namespaces std;

#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();

}

 

 

 

with sample output below:

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