Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in c++ use functions and calls main should not have a lot of code in it ernpioye

ID: 3903652 • Letter: I

Question

in c++
use functions and calls
main should not have a lot of code in it

ernpioyee division and office. PROGRAMMING. Write the code for the specified program. Use proper style and naming. You may omit comments, inclusions, and namespace specifiers. (20 pts) 42) In a role playing game, there is a class called Adventurer. Its constructor makes a random character, and it has functions int getHealthO to return the health value, and void takeHit(int) to apply damage to the health. Assume this class is already written. Write a class called Party that represents a group of adventurers together. The constructor should accept an integer argument and dynamically allocate an array with that many adventurers. The destructor should free the memory held by the array. It should also have these functions: - Get an adventurer at a given index. Apply an amount of damage to the character at a given index. Count how many adventurers are still alive (have health above zero). Demonstrate the class in a driver program that asks the user for a number, creates createsa party of that size, and prints out all their health values.

Explanation / Answer

#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;

class Adventurer{

   private:
      int health;
   public:
      Adventurer(){

         srand(time(NULL));
         health = rand() % 100;
      }
      void takeHit(int a){
         health = health - a;
      }
      int getHealth(){
          return health;
      }
};

class Party{

    private:
        Adventurer *list;
        int max;
    public:
        Party(int a){
            list = new Adventurer[a];
            max = a;
        }
        ~Party(){
            delete [] list;
        }
        void damage(int i, int a){
            if (i < max){
               list[i].takeHit(a);
            }
            else {
               cout << "Invalid index ";
            }
        }
        int count(){
           int cnt = 0;
           for (int i = 0; i<max; i++){
               if (list[i].getHealth() > 0)
                  cnt++;
           }
           return cnt;
        }
};

int main(){

  
   Party p(10);
   cout << p.count() << endl;
   p.damage(7,56);
   p.damage(5,99);
   cout << p.count() << endl;
   return 0;
}