Programming in C++

Programming in C++ 150 150 Affordable Capstone Projects Written from Scratch

CS 190 – Programming in C++

Final Exam – Part 1

Follow the instructions exactly for each problem. Answer all questions for each problem. All work is

to be done individually. Any external sources used must be cited. Submissions should include an

archive of all program files used in programming problems (including build scripts, Makefiles, etc),

along with proof that your program runs properly, in the form of typescript or screenshot of the compile

and run of the program. Responses should be in their own files. Submit this archive on Canvas and

print a copy to submit in person. All submissions must be made to Canvas before the start of the

written portion of the exam.

  1. On the following pages, you will find a class definition for a Unit. Add a constructor to this class that

takes a string as a parameter. This string should be put into a string stream, which should be used to

extract three values – the hp, power, and defense attributes, respectively. Then, add another function to

the class, which outputs the hp, power, and defense attributes, separated by a space.

  1. Write two classes which are subclasses of the Unit class from the previous problem. Modify the

attack and damage functions for each of these, depending on how you want attack and damage

calculations done for the given unit types you are creating. They don’t have to be sophisticated, but

make sure they are at least a little different from one another.

  1. Write your main function, which will demonstrate a battle between units of the types that you

created. First, you should open a file called “fighters.txt”, which you will find on the following page,

where each unit is on it’s own line. Read each line, creating a new object using the constructor you

wrote in the first problem. You can choose to have any mix of unit types that you wish, as long as each

gets a chance to appear. Then, you can simulate a battle between all of the units read in. The first two

fighters are the firsts two that are read in, and will be fighting each other. Each fighter takes a turn

attacking the other. If a unit falls in battle, it is replaced by the next one read in. For example, if fighter

one is defeated by fighter two, the battle continues with fighter two battling fighter three.

unit.cpp

class Unit {

public:

Unit(int hp, int attack, int defense);

int get_hp();

int get_power();

int get_defense();

bool is_alive();

void attack(Unit &other);

void damage(int amount);

private:

int hp;

int power;

int defense;

};

Unit::Unit(int hp, int power, int defense) {

this->hp = hp;

this->power = power;

this->defense = defense;

}

int Unit::get_hp() {

return this->hp;

}

int Unit::get_power() {

return this->power;

}

int Unit::get_defense() {

return this->defense;

}

bool Unit::is_alive() {

return this->hp > 0;

}

void Unit::attack(Unit &other) {

if(this->get_power() > other.get_defense()) {

other.damage(this->get_power() – other.get_defense());

}

else {

this->damage(other.get_defense() – this->get_power());

}

}

void Unit::damage(int amount) {

this->hp -= amount;

}

fighters.txt

10 04 06

07 12 01

13 05 02

12 07 01

09 08 03

03 05 12

10 05 05


 

TO GET THIS OR ANY OTHER ASSIGNMENT DONE FOR YOU FROM SCRATCH, PLACE A NEW ORDER HERE