CIS 150 C++ Programming Project: Classes and objects

Objectives

  1. Follow instructions (program requirements)
  2. Create a class
  3. Create private instance variables
  4. Create constructors
  5. Create accessor functions
  6. Create mutator functions
  7. Declare private and public access to class members
  8. Create separate header and implementation files

Program requirements

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.

Starting code (main)

The mainline is available at TestTank.cpp.txt

/*
    TestTank.cpp
    Dave Klick
    CIS 150
    2017-01-09
    
    This program tests out the Tank class.
*/

#include <iomanip>
#include <iostream>
#include "Tank.h"
using namespace std;

void displayTank(Tank& tank);

int main() {
    Tank tank1;
    Tank tank2(120, 100, -30);
    Tank tank3(3, 35.5f, 37);
    
    cout << "Initial setup:\n";
    displayTank(tank1);
    displayTank(tank2);
    displayTank(tank3);
    
    tank1.setId(1);
    tank1.setSpeed(2.0f);
    tank1.setAmmo(3);
    tank2.setId(2);
    tank2.fire();
    tank2.increaseSpeed();
    tank3.increaseSpeed();
    tank3.reload();
    cout << "After changes:\n";
    displayTank(tank1);
    displayTank(tank2);
    displayTank(tank3);
    
    tank1.setId(788);
    tank1.setSpeed(2000.0f);
    tank1.setAmmo(300);
    tank3.increaseSpeed();
    tank3.increaseSpeed();
    cout << "After more changes:\n";
    displayTank(tank1);
    displayTank(tank2);
    displayTank(tank3);
    return 0;
}

void displayTank(Tank& tank) {
    cout << "Tank: " << setw(2) << tank.getId()
         << ", Speed: " << fixed << showpoint
         << setprecision(1) << setw(4) << tank.getSpeed()
         << ", Ammo: " << setw(2) << tank.getAmmo()
         << endl;
}

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

Rubric

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