CS1220 – C++ Programming

Homework Assignment #4

Due: See course web site for due date

Points: 70

Name: ______

I. Requirements: Restate the problem specification, and any detailed requirements

II. Design: How did you attack the problem? What choices did you make in your design, and why? Show class diagrams for more complex designs.

III. Implementation: Outline any interesting implementation details.

IV. Testing: Explain how you tested your program. Explain why your test set was sufficient to believe that the software is working properly, i.e., what were the range of possibilities of errors that you were testing for.

V. Summary/Conclusion: Present your results. Did it work properly? Are there any limitations? If it is an analysis-type project, this section may be significantly longer than for a simple implementation-type project.

VI. Lessons Learned: List any lessons learned. What might you have done differently if you were going to attack this again.

Objective:

Develop class definitions and implementations to demonstrate C++ inheritance:

Specifically, you are to:

  1. Develop an abstract base class called Vehicle(in a header file vehicle.h and implementation file vehicle.cpp) having data members:
  2. A name (type char*) which stores the name of the Vehicle object as a C-style character array and
  3. Astatic attribute out (an ostream*) which is the output object for the print function (see member functions below) initialized to reference standard output (i.e.,cout).

Vehicle should use a protection scheme for its attributes which allows derived classes (but not general users of the class) the ability to inspect or modify the attributes.

The member functions for Vehicle are:

  1. A default constructor initializing name to the empty string
  2. A constructor taking a single string parameter to initialize the name. Since name is stored internally as a char*, the constructor should handle the conversion internally.

NOTE: you may combine this constructor with the default constructor via default parameters.

  1. A copy constructor which provide for a non-aliased (i.e., deep) copy of a Vehicle object.
  2. An assignment operator providing a deep copy on a Vehicle.
  3. A destructor which returns any storage allocated in the constructors (or assignment operator). The destructor should be specified so that it and its descendants are dynamically dispatched.
  4. A pure virtual function printhaving the signature:void print().
  5. A pure virtual functionreadhaving the signature: void read().
  1. Develop another class called MotorVehicle, derived from Vehicle,within files motorVehicle.h and motorVehicle.cpp, havingadditional data members:
  2. make (type string) which stores the name of the motorVehicle manufacturer (e.g, “Ford”, “Jeep”, etc.),
  3. model (type string) which stores the nameplate (e.g., “Focus” or “Wrangler”), and
  4. mpg (type double) which stores the combined EPA fuel economy estimate in miles per gallon.

In addition, the MotorVehicle class should have as member functions:

  1. A default constructor. Default values are empty strings for name, make and model, and zero for the mpg.
  2. A constructor taking initial values for all fournon-static attributes (or you may combine these constructors if you choose). Again use the string type as a parameter for the name.
  3. Accessor and Mutator functions for all five attributes (i.e., out, name, mpg, model, and make). Accessors and mutators should be named in the standard fashion (e.g., getName(), setMpg(), etc.).
  4. The accessor and mutator for name should use string as the return type and parameter, respectively, and handle the conversion to char* internally. Additionally, the accessor for out should return an ostream& and the mutator for out should have a single ostream& parameter.
  5. A print function whichoverrides its base class and prints each non-staticattribute of MotorVehicle to the out attribute.

NOTE: because the out attribute is an ostream*, the syntax for printing to out becomes: *out < valueToPrint;

  1. A read function which overrides its base class and,for each non-staticattribute, writes a descriptive prompt to stdout to request a value and then reads that value from stdin. For example, for the mpg attribute MotorVehicle::read() could output a prompt such as, “Please enter the miles per gallon for this vehicle: ”to stdout and then read from stdin a mpg value. NOTE: To keep the read function simple, read all input as a string without white space and then (if necessary) convert the value to the proper type. Input validation on the values you read is not required.
  1. Develop twoderived classes called Car(in files car.h and car.cpp) andTruck(files truck.h and truck.cpp) each inherited from MotorVehicle. Each derived class should support similar kinds of constructors, accessors, mutators, and I/O functions as MotorVehicle (e.g., there should be a default constructor and a constructor initializing all non-static attributes, and there should be accessors and mutators for the new attributes, the read function should operate similarly as before, etc.). In addition:
  2. Car should have an attribute trim (type string).
  3. Truck should have an attribute cargoCapacity (type double).
  1. Writethe overridden functions for read and printtouse the capabilities of theirparent class versions. For example, since the MotorVehicle version of print writes the values of name, mpg,model and make to out, the derived version of print should NOT re-create this capability.
  1. Test your classes using the programvehicleTest.cppand the test file vehicleTestCases.txt. If your classes are properly written, you will be able to exercise the test program without modifications.
  1. NOTE: The test program and test cases I have provided does not test all capabilities of your implementation (e.g., such as self-assignment from one Vehicle to another) so you may want to modify one (or both) of the test cases or program in order to fully exercises the code you have written (I know your instructor will :-).

Other Details

You may use either Visual Studioor g++ to develop your classes. Test your classes with vehicleTest.cppandvehicleTestCases.txt using input and output redirection (see example below). You may use the links above or find copies online on johnat /home/shomperk/CS1220/public/assignments/vehicleTest.cpp and /home/shomperk/CS1220/public/assignments/vehicleTestCases.txt.

As always, you can run a sample of the program (called vehicleTest) to get a sense of its dynamic behavior. The program is in the assignmentsdirectory. Copy the sample files to your home directory and run the program from the command line as: vehicleTest < vehicleTestCases.txt > /dev/null

Once run, vehicleTest will produce an output file called testOutput.txt. You can compare this output file with correctTestOutput.txt (also in assignments) to get a sense of the correctness of your source code.

NOTE: passing the given test cases is not altogether indicative of the absence of programing errors.

Required for turn-in:

  1. The completed coversheet for this assignment.
  2. Program listings of all class definitions (.h files) and implementations (.cpp files) for Vehicle, MotorVehicle, Car, and Truck.
  3. NOTE: your solution should compile, link and run unchanged on john, because that is where it will be tested, so please verify this condition before submitting your solution.
  4. An electronic zipped submission of your four.h and four .cpp files via the “submit20” command.