Skip to main contentSkip to footer

Computer Science 236 C++ Programming

Programming Project #3 Polymorphism and Virtual Functions (10 points)

For project #3 we will implement an inheritance hierarchy with polymorphism and virtual functions. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with a main method. Watch the following videos to prepare for this project:

OO Design #1
OO Design #2
OO Design #3
OO Design #4

Phase 1 :

Here is the following set of classes you will implement and their inheritance relationship:

Hierarchical Chart of Classes

The instructor will provide you with your assigned types. DO NOT make up new types or pick your own.

Type 1 Type 2 Type 3
PitBull Jet Soldier
Cougar Helicoptor Robot
Lion Drone CarJacker
Tiger Blimp Terrorist
Bear Bomber BadCop


The generic BASE class Enemy will serve as the parent class to three child classes.
The variables will be private: x_position, y_position, width, height and status
These methods will be protected: a 'get' and 'set' for EACH variable listed above
These methods will be pure virtual and public: move_position, fire_weapon, update_status


The main() function should be in it's own .cpp file; It will create Enemy objects and store them in an array. The methods of an object define what the object DOES, the actions it will perform. main() will loop and ask the Enemy objects to perform actions like move_position, fire_weapon and update_status. The basic layout will be:

#include "stdafx.h" // not needed by some IDE's
#include <conio.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{const int max_enemy = 20;
Enemy* enemy_ptr[max_enemy];
int num_enemy;

// create Enemy objects, place in array
// set value of num_enemy
while ( true ) {
// every Enemy object should move_position// Pick a random Enemy to fire_weapon
// Pick a random Enemy to update_statusgetch();
cout << endl;
return 0;

You can google 'C+ random number' for help on generating a random number, then use the modulus '%' operator: rand() % num_enemy This will select which Enemy in the array should perform an action.

Keep main() simple! The objects will do the work, and print messages reporting what they have done. The ONLY enemy object methods you are allowed to call are move_position, fire_weapon and update_status.

Type 1 - Type 2 - Type 3 Classes

First, start simple, create your assigned classes and have each one print a simple message for each action it takes:

PitBull moves position Jet moves position Soldier moves position
Jet fire weapon: missile
PitBull update status: I have been hit (bark)

These should be simple classes, a variable to track the name of the object and simple cout statements to report each action. Make sure move_position does NOT output an end of line, if there are 10 objects we DO NOT want it to print 10 lines of output. Do custom messages to match the Enemy, a pitbull will bite and a soldier will shoot a rifle when they fire a weapon. A soldier will say ouch and a Jet will make a ping sound when update status records a hit.

Make sure you can get the simple version of your program working. Test it with different numbers and different combinitions of enemy objects. Each time through the main loop it should output messages like the example above. Do NOT move on until the simple version is working!

Better Enemy Classes!

When you have the simple version working, improve your child classes. Make them behave like they would in a video game. Any change to main() or the Enemy parent class should be very minor (if at all), the focus is on the child classes. Make them move, and fire weapons and record hits when update status is called:


Each object exists in a 2D space, their X position can range from 0 to 800, y position 0 to 600. Position 0,0 is the top left corner, 800,600 is the bottom right. The ground is at 500 so a person or animal will be at y position 500. A low flying object at y position 300, a high flying one a 100. Objects move on the X axis, have walking objects move 3, running 6 each time move position is called. Flying objects move 15 to 30 with each call depending on their speed. Do not make them all start in the same position, do not make them move at the same speed and do not make them all move in the same direction. When an objects status is zero, it is DEAD, it should no longer move.


Make sure each object fires an appropriate weapon. In general type 1 objects bite or slash. Type 2 objects have missiles or bombs. Type 3 have guns. Keep track of ammo, if a jet has 4 missiles, have fire weapon report out of ammo on the 5th call. Also check status, when dead, fire weapon should say NO WEAPON FIRED. Since animals do not use ammo, have them vary the attack, e.g. bite leg, slash chest, bite neck.


Update status means the Enemy object has been HIT. It is to lose status points and DIE if status reaches zero. Type 1 objects should take only 1 or 2 hits before they die, type 3 objects 4 to 5 hits and type 2 should take 7 to 8 hits. As always, make it match the enemy, a Robot will take more hits before death than a Car Jacker. Always report current status points when called, output an extra special statement when death occurs. A soldier might say ouch for a non-lethal hit, but ARRRGH for a lethal one. The simple way to do this is to start each object with a status that matches the number of hits to kill it and subtract one every time.

Once you have your improved child classes, the output should update each time through the loop, the example below shows the program with 3 loops:

PitBull move to 710,500 Jet move to 320,100 Soldier move to 518,500
Jet fire weapon: missile (2 left)
PitBull update status: hit by bullet, status points 0 (dead)
PitBull move to 710,500 Jet move to 360,100 Soldier move to 514,500
PitBull fire weapon: dead!!!!!
Soldier update status: hit by bullet, status points 3 (ouch)
PitBull move to 710,500 Jet move to 400,100 Soldier move to 510,500
Soldier fire weapon: rifle (12 bullets left)
Jet update status: hit by bullet, status points 7 (ping)