Your cart is currently empty!
Focus: Object-oriented programming Data Hiding Encapsulation Delegation Problem: We will expand our Warrior a little. Each Warrior will have a weapon. He is “born” with it, i.e. the weapon is created together with the warrior. It can only be accessed by him. It provides him with his strength. In battle, weapons lose their edge and…
Focus:
Problem:
We will expand our Warrior a little. Each Warrior will have a weapon. He is “born” with it, i.e. the weapon is created together with the warrior. It can only be accessed by him. It provides him with his strength. In battle, weapons lose their edge and weaken. When a Warrior’s weapon loses all of its strength, the Warrior himself dies.
Implementation
Input
Our sample input file might now look like:
Warrior Jim Glamdring 42
Warrior Lancelot Naegling 15
Warrior Arthur Excalibur 15
Warrior Torvalds Narsil 20
Warrior Gates Orcrist 8
Status
Battle Arthur Lancelot
Battle Jim Lancelot
Battle Torvalds Gates
Battle Gates Lancelot
Status
Output
The corresponding output would be:
There are: 5 warriors
Warrior: Jim, weapon: Glamdring, 42
Warrior: Lancelot, weapon: Naegling, 15
Warrior: Arthur, weapon: Excalibur, 15
Warrior: Torvalds, weapon: Narsil, 20
Warrior: Gates, weapon: Orcrist, 8
Arthur battles Lancelot
Mutual Annihilation: Arthur and Lancelot die at each other’s hands
Jim battles Lancelot
He’s dead, Jim
Torvalds battles Gates
Torvalds defeats Gates
Gates battles Lancelot
Oh, NO! They’re both dead! Yuck!
There are: 5 warriors
Warrior: Jim, weapon: Glamdring, 42
Warrior: Lancelot, weapon: Naegling, 0
Warrior: Arthur, weapon: Excalibur, 0
Warrior: Torvalds, weapon: Narsil, 12
Warrior: Gates, weapon: Orcrist, 0
Turn in
Hand in a single cpp file, hw02.cpp, containing your program.
Grading Comments:
In main(), item object should not be instantiated here. The warrior constructor should accept 3 parameters warriorName, weaponName and strength. The constructor should instantiate a Weapon item in the member initialization list.
Weapon item(weaponName, str);
Warrior awarrior(name, item);
In method battleWith of class Warrior, to get or set a warrior’s strength, call a method in the Warrior to get or set the strength which in turn calls the respective method in the Weapon class.
Use functions to process the file, handle each command like “Battle”, “Warrior” and “Status”.
void display() in class Warrior and int getStr() in Weapon should be a const method.