CIS 150 Programming Project: Classes and objects

Objectives

  • Follow instructions (program requirements)
  • Create a class
  • Create private instance variables
  • Create constructors
  • Create accessor functions
  • Create mutator functions
  • Declare private and public access to class members
  • Create separate header and implementation files

Overview

The goal of this project is to create a class that represents a Tank. There is test code provided that will create a couple of tank objects and check to see if certain parts of your Tank class are functioning properly.

Requirements for the Tank class

  1. (2 pts) Create separate header and implementation files for the Tank class. All of the implementation must be done in the implementation (.cpp) file.
  2. (1 pt) The header file must include header guards to avoid problems if the header file is included twice.
  3. (1 pt) A private integer variable for the ID.
  4. (1 pt) A private float variable for the speed.
  5. (1 pt) A private integer variable for the ammo.
  6. (2 pts) An accessor function named getId. It takes no arguments and returns the ID.
  7. (2 pts) An accessor function named getSpeed. It takes no arguments and returns the speed.
  8. (2 pts) An accessor function named getAmmo. It takes no arguments and returns the ammo.
  9. (2 pts) A mutator function named setId. It takes one integer argument and uses it to set the ID. If the argument is less than 0, it sets the ID to 0. If the argument is greater than 50, it sets the ID to 50. Otherwise it sets the ID to whatever the argument is.
  10. (2 pts) A mutator function named setSpeed. It takes one float argument and uses it to set the speed. If the argument is less than 0.0, it sets the speed to 0.0. If the argument is greater than 60, it sets the speed to 60. Otherwise it sets the speed to whatever the argument is.
  11. (2 pts) A mutator function named setAmmo. It takes one integer argument and uses it to set the ammo. If the argument is less than 0, it sets the ammo to 0. If the argument is greater than 40, it sets the ammo to 40. Otherwise it sets the ammo to whatever the argument is.
  12. (2 pts) A public default constructor that sets all instance variables to 0 using the mutator functions.
  13. (2 pts) A public constructor that takes three arguments (an int, a float, and an int) for the three instance variables and sets them using the mutator functions.
  14. (2 pts) A function named increaseSpeed. It takes no argument and tries to increase the speed by 3.5 using setSpeed to provide error checking.
  15. (2 pts) A function named decreaseSpeed. It takes no argument and tries to decrease the speed by 3.5 using setSpeed to provide error checking.
  16. (2 pts) A function named fire. It takes no argument and tries to decrease the ammo by 1 using setAmmo to provide error checking.
  17. (2 pts) A function named reload. It takes no argument and tries to increase the ammo by 5 using setAmmo to provide error checking.

Sample Run

Initial setup: Tank: 0, Speed: 0.0, Ammo: 0 Tank: 50, Speed: 60.0, Ammo: 0 Tank: 3, Speed: 35.5, Ammo: 37 After changes: Tank: 1, Speed: 2.0, Ammo: 3 Tank: 2, Speed: 60.0, Ammo: 0 Tank: 3, Speed: 39.0, Ammo: 40 After more changes: Tank: 50, Speed: 60.0, Ammo: 40 Tank: 2, Speed: 60.0, Ammo: 0 Tank: 3, Speed: 46.0, Ammo: 40

Starting code (main)

The starting code is available at TestTank.cpp.txt

Rubric

Point values for specific items are located in the requirements section.