Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please use C++ Problem: We will expand our Warrior a little. Each Warrior will have a weapon. He is born with it, ie the weapon
Please use C++
Problem: We will expand our Warrior a little. Each Warrior will have a weapon. He is "born" with it, ie the weapon is created together with the warrior. It can only be accessed by him. It provides him with hi s strength. In battle, weapons lose their edge and weaken. When a Warrior's weapon loses all of its strength, the Warrior himself dies. Implementation Remember that we are using data hiding. Therefore, every field, aka member variable, must be privat e. What are the types of things in the problem? We will need a class for each type. What do the things / types do? These "behaviors" should be represented as methods. Weapons have both a name and a strength. The weapon is created together with the Warrior and must not be accessed by anyone else. Note that the input file changed a little, compared to the previous assignment. When a Warrior is cr eated, instead of simply specifying his name and strength, the Warrior command specifies the Warrio r's name as well as his Weapon's name and its strength. The Status report is also modified to show the name of the Warrior's Weapon. No one can access a warrior's weapon except the warrior himself. But the weapon is what actually h olds the warrior's strength. How does this effect the programming? Any time the code needs to kno w or change the warrior's strength, the warrior who owns the weapon then "asks" the weapon what the strength is or tells the weapon that the strength needs to be changed. This represents the idea of delegation. We will see this concept frequently, where one object requests that another object d o some task. It is in fact unnecessary for any code other than a Warrior to even know about the Weapon. We wi Il enforce this by nesting the definition of the Weapon class inside the Warrior class. To make sure t hat no code other than Warrior's makes use of Weapon, we need to make the class private. The name of the input file will be "warriors.txt". One last implementation detail. to display the information about an object, whether it is a warrior or a weapon, we will use that object's output operator. Input Our sample input file might now look like: Warrior Jim Glamdring 42 Warrior Lancelot Naegliog 15 Warrior Arthur Excalibur 15 Warrior Torvalds Narsil 20 Warrior Gates Qrcrist 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: Qrcrist, 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: Qrcrist, 0Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started