Sei sulla pagina 1di 5

CSCI 428 Program 3

Inheritance and Polymorphism: StarCraft Program


Due Date: Friday, November 20, 2015 by 11:59 PM

Note: You can work in groups of two for this program. Please dont ask me if you can have three or more.

Additional Note: Im also teaching a graduate Object Oriented Programming course this semester using the Java
language. The graduate students had to do a similar StarCraft project but be aware that their project was a little
different. There were a few additional things they had to do and their project did not include multiple inheritance
and the SuperSoldier class (Java does not support multiple inheritance.) I just wanted to let you all know in case
you are working in the lab and getting help from one of the graduate assistants who might assume it is the same
project as theirs. Please do your best to successfully complete this project. We might utilize this as a framework
for the final semester project. Again, you can work with a partner on this.

How to turn in this project


I want you to take all your source code files and zip them up into one zip file. If you are using Dev C++, please also
include your .dev project file along with all the source code files. This will make it easier for me to compile and run
your projects. Each class should have its own header (.h) and implementation (.cpp) file. You will also have a
separate driver (.cpp) file with your main function in it. Please just turn in one submission per group and be sure
to include both student names if you are working with a partner.

Program Description
This programing assignment will focus on C++ inheritance, polymorphism, and multiple inheritance, which are three
recent topics we have covered this semester.

This program will be loosely based on the game of StarCraft. For those unfamiliar with the game, StarCraft is a
military science fiction real-time strategy video game developed and published by Blizzard Entertainment back in
1998 (from Wikipedia). The game revolves around three species fighting for dominance. They are the Terrans
(humans), the Zerg, and the Protoss. Each species has various types of soldiers, vehicles, and weapons that you can
build during the game.

Part One: Single Inheritance with Infantry Classes


I want you to write a program in C++ that will simulate a small number of the Terran units using inheritance. NOTE:
Please refer to the included large class diagram to see associations and relationships between classes. We will
focus on the different types of foot soldiers (or infantry). There will be four different types of infantry: Marine,
Firebat, Ghost, and Medic. You will need to have an abstract class (Infantry) that is the superclass (base class). The
other four should be subclasses (derived classes) that inherit from infantry.
Infantry Class
Infantry should have a name (name of the soldierHicks, Hudson, etc.), health
(percent of current health), pistol (the number of rounds), and type (the type of
soldiermarine, firebat, etc.). Infantry should have the following methods:
constructors, set and get methods for the attributes, speak (an abstract method
(pure virtual function) implemented by the subclasses), display (used to output
infantrys attributes), attack (an abstract method implemented by the
subclasses), renderAid (abstract method), getAttacked (abstract method),
receiveAid (abstract method), and die.

Marine Class
In addition to the things inherited from the Infantry class, the Marine class
should include the attribute of assaultRifle which will keep track of the number
of machine gun rounds. Marine should have constructors, and set and get
methods for the assaultRifle. In the Marine implementation of the speak
method, he should output his name followed by the phrase You wanna piece of
me, boy? The attack method should accept as a reference parameter an
Infantry object (that is referring to an object of any soldier type). The attack
method should then call the getAttacked method of the object and send in the
amount of health to deduct from the attacked soldier. A Marine attack should
probably deduct something like 10 from the health. Attack also needs to deduct some rounds from the assaultRifle
attribute. Each soldier type will have a renderAid method that can be called to help a fellow injured soldier.
Obviously, the Medic class will be able to offer the most help but the Marine should have some limited ability. The
Marine renderAid method should do something like the following: If the injured soldiers health is 75 or higher,
output You aint hurt, boy! Now get up and fight! If the injured soldiers health is greater than 25 but less than
75, output Let me patch you up, boy! and then call the injured soldiers receiveAid method with a value of 10 to
be added to his health. If the injured soldiers health is 25 or less output Sorry, broYou aint gonna make it. and
then give him a 50% chance of dying (in which you call the die method). The die method for Marine (and the other
types) should set the health and weapons attributes to zero and output a message announcing the soldiers death.

Firebat Class
The Firebat is similar to the Marine but has a flameThrower as his weapon. When
he speaks, he says his name followed by the phrase Need a light? Firebats
attack should deduct 20 units of health. Firebat is not able to render aid so his
method for that should output something like Sorry Dude. Ive got no first aid
supplies. Keep thinking positive thoughts!
Ghost Class
The Ghost is a sneaky type of soldier. He has a sniperRifle to keep track of the
number of rifle bullets he has and cloak (should be a Boolean) that keeps track of
the status of his invisibility. When he speaks, the Ghost says his name followed by
the phrase Somebody call for an exterminator? The Ghost renderAid method
should only add 5 to the injured soldiers health and should output a message such
as You dont see meI was never here. If the cloak is set to true, the Ghosts
health should not be affected by an attack.

Medic Class
The only weapon the Medic has is the pistol attribute that is inherited from
Infantry. Medic has a boosterShot. Each time the Medic renderAid method is
called, the number of boosterShot is decremented and will add 30 to the injured
soldiers health. When a Medic speaks, he says his name followed by the phrase
Prepped and ready. The Medic attack only deducts 5 units of health from the
soldier being attacked.

Part 2: Polymorphism and Driver Code


As you are creating your various classes, I would suggest testing them out using some driver code in your main
function. Create objects of the different classes and make sure they work properly. Once you have done that, you
can move on to implementing some polymorphism.

Polymorphism in this Project


Part of the goal of this project is to utilize some polymorphic behaviors. What we want here is to treat all soldiers
as Infantry. Remember, in C++ we do this by utilizing virtual functions and dynamic binding. You will need to have
set up your virtual functions in your various classes. See the naval battle source code for some examples. You will
need to declare some Infantry (base class) pointers and point them at various derived class objects (Marine, Firebat,
etc objects). When you have this properly set up, you should be able to call methods such as speak and attack and
they will behave according to the specifications of the specific derived class, even though they are actually all
Infantry pointers. Also, be sure your attack and renderAid methods promote the same idea. Any type of Infantry
soldier should be able to attack or renderAid to any other type of Infantry soldier. Again, the naval battle example
should help you with this part of the project. Your driver code should effectively demonstrate the functionality of
your various Infantry types.
Part 3: Multiple Inheritance and the SuperSoldier class
One interesting feature that C++ supports is multiple inheritance. Many
other languages such as Java do not support it. Once you get your other
classes and polymorphism working, I want you to add another class
SuperSoldier that will inherit from all of your derived classes (Marine,
Firebat, Ghost, and Medic). That means that a SuperSoldier will have
everything that each of those classes has plus a couple of additional things.
SuperSoldier should also have stickyGrenade and rocketLauncher as
attributes along with set and get methods for them. Just like the other
types of soldiers, SuperSoldier should have its own speak, display, attack,
renderAid, getAttacked, receiveAid, and die methods. You can decide
what kind of awesome phrase the SuperSoldier will speak. Just keep it
clean. One issue that comes up with multiple inheritance is the diamond
inheritance problem and the ambiguity issues it creates. Here, each
derived class component of SuperSoldier would have its own Infantry part, which is a problem. This is solved by
using virtual inheritance. Once again, be sure to read the chapter in the book and look at the naval battle source
code example. Once you are able to create a SuperSoldier object in your driver code, you should be able to create a
new Infantry pointer and point it at your SuperSoldier object and utilize the same polymorphic behaviors as the
other derived objects. Include code in your driver demonstrating this so that I can see that it works properly.

How to turn in this project


I want you to take all your source code files and zip them up into one zip file. If you are using Dev C++, please also
include your .dev project file along with all the source code files. This will make it easier for me to compile and run
your projects. Each class should have its own header (.h) and implementation (.cpp) file. You will also have a
separate driver (.cpp) file with your main function in it. Please just turn in one submission per group and be sure
to include both student names if you are working with a partner.
Overall Class Diagram
This class diagram shows all the classes and how they relate to each other.

Potrebbero piacerti anche