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

wite a program in C++ you will be making 1 base class and 2 derived classes. Bas

ID: 3772363 • Letter: W

Question

wite a program in C++ you will be making 1 base class and 2 derived classes.

Base Class: Character -- have hit points

Derived 1: Monster - high damage, low damage, xp value

Derived 2: Non Player - talkative (true false)

For all variables, have set, get and display functions.

You will be submitting 7 files

Monster.h and cpp

Non Player h.cpp

Character.h and cpp

main.cpp

Remember to use the preprocessor trick on the header files

Main should create an object of each class, make sure the variables are initialized and call display.

Also, make a UML class inheritance diagram, showing what you made.

Explanation / Answer

//monster.h
#ifndef Monster_h //header guards
#define Monster_h

#include <iostream>
using namespace std;

#include "Character.h"
#include “main.h”  
class Monster
   {
   public:
       string xp;
       high damage;
       low damage;

       Monster(string newxp, int newHealth, int newDamage);
  
       void attack(Character&);
   };
#endif


//Character.h
#ifndef Character_h
#define Character_h

#include "Monster.h" //we just made this
#include "main.h"   //I’ll get to this (I promise)

#include <iostream>
using xpspace std;

class Character
   {
   public:
       string xp;
       high damage;
       low damage;
       short arrows;

       Character(string newxp);

       void attack(Monster& target);
      
       void rangedAttack(Monster& target);
   };
#endif

//Non Player.h
#ifndef Non Player_h
#define Non Player_h

#include <iostream>
using xpspace std;

#include "Character.h"
#include "Monster.h"

class Non Player
   {public:
       Monster& M;
      
       Non Player(Character& C);
      
       void Non PlayerChoice(Character& C);
      
       void Non Player1(Character& C);
   };
#endif


#include “Monster.h”

void Monster::attack(Character& target)
{
   target.health-=damage;
   cout << xp << " attacks " << target.xp << " doing " << damage << " damage!" << endl;
   cout << xp << " health: " << health << endl;
}

Monster::Monster(string newxp, int newHealth, int newDamage)
{
   xp=newxp;
   health=newHealth;
   damage=newDamage;
}  


#include "Character.h"

void Character::attack(Monster& target)
{
   target.health-=damage;
   cout << xp << " attacks the " << target.xp << " doing " << damage << " damage!" << endl;
   cout << xp << " health: " << health << endl;
}

void Character::rangedAttack(Monster& target)
{
   if (arrows == 0 )
       cout << xp << " is out of arrows!" << endl;
   else
   {
       short rangedDamaged=3;
       target.health-=rangedDamaged;
       arrows--;
       cout << xp << " shoots " << target.xp << " doing " << rangedDamaged << " damage!" << endl;
   }
}

Character::Character(string newxp)
{
   xp=newxp;
   health=100;
   damage=3;
   arrows=5;
}

void Character::display()
{
   cout << xp << " health: " << health << " arrows: " << arrows << endl;
}


#include "Non Player.h"
#include "Monster.h"
#include "Character.h"

Non Player::Non Player(Monster& newM) : M(newM)
{
}

void Non Player::Non PlayerChoice(Character& C)
{
  
   C.display();
   cout << "What do you do? 1 attack, 2 fire arrow" << endl;
   short choice;
   cin >> choice;
   switch (choice)
   {
       case 1:
           C.attack(M);
           break;
          
       case 2:
           C.rangedAttack(M);
           break;
   }
}


void Non Player::Non Player1(Character& C)
{
  
   while (M.health>0 && C.health>0 )
   {
      
       M->attack(C);
      
       Non PlayerChoice(C);
      
   }
   if (M.health<0) //use -> for pointer
       cout << "Congratulations!" << endl;
   if (C.health<0) //and use a dot for reference
       cout << "GAME OVER" << endl;;
  
}

#include <iostream>
#include <string>
using xpspace std;

#include "Monster.h"
#include "Character.h"
#include "Non Player.h"

Monster goblin("goblin",50,2);//declare Monster type outside of main


int main (int argc, char * const argv[]) {
  
   Character C("George"); //variable xp C doesn’t matter as we pass by ref
  
   Non Player Non Player(goblin);

   Non Player.Non Player1(C);
  
   cout << "thanks for playing!" << endl;

   return 0;
}