Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This question needs to be answered in C++ This is the input file that would be used Focus: - Object-oriented programming - Data Hiding -
This question needs to be answered in C++
This is the input file that would be used
Focus: - Object-oriented programming - Data Hiding - Encapsulation - Delegation Output operator Overloading 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. - Remember that we are using data hiding. Therefore, every field, aka member variable, must be private. - 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 created, instead of simply specifying his name and strength, the Warrior command specifies the Warrior'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 holds the warrior's strength. How does this effect the programming? Any time the code needs to know 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 do some task. - It is in fact unnecessary for any code other than a Warrior to even know about the Weapon. We will enforce this by nesting the definition of the Weapon class inside the Warrior class. To make sure that 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". Please don't ask the user for the file's name. - 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. Dur 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 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 StatusStep 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