Please help, write a c++ program. Define your class in a header file and impleme
ID: 3744290 • Letter: P
Question
Please help, write a c++ program. Define your class in a header file and implement in a .cpp file also include the main file.
gladiator.h
#ifndef GLADIATOR_H
#define GLADIATOR_H
#include <iostream>
#include <string>
using namespace std;
class gladiator
{
public:
//constructor and destructor
gladiator();
virtual ~gladiator();
//generate random number by using seedR property
void setRandomSeed();
//getters for gladiator properties
int getSeedR() const;
int getHP() const;
double getAS() const;
double getDS() const;
int getAppeal() const;
int getDamage() const;
//setters for gladiator properties
void setSeedR(int s);
void setHP(int s);
void setAS(double s);
void setDS(double s);
void setAppeal(int s);
void setDamage(int s);
//function to attack other gladiator
int attack(gladiator & glad);
//virtual functions implemented by child classes
virtual string taunt() = 0;
virtual void print() = 0;
virtual void defenceBolster() = 0;
virtual void specialAction() = 0;
private:
int HP; //gladiator's health
double AS; //Attack skill probability of attack 0-1 by them
double DS; // Defense skill probability attack to them 0-1
int appeal; //represent charisma and crowd applause
int damage; //physical damage capacity
int seedR; // value used when generating randoms seed
};
#endif
--------------------------------------------------------------------------------------------------------------------------
gladiator.cpp
#include "gladiator.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <map>
using namespace std;
//constructor implementation
gladiator::gladiator()
{
}
//destructor
gladiator::~gladiator()
{
}
//generating random seed value
void gladiator::setRandomSeed()
{
srand(seedR);
}
//getter for seedR
int gladiator::getSeedR() const
{
return seedR;
}
//getter for HP
int gladiator::getHP() const
{
return HP;
}
//getter for AS
double gladiator::getAS() const
{
return AS;
}
//getter for DS
double gladiator::getDS() const
{
return DS;
}
//getter for Appeal
int gladiator::getAppeal() const
{
return appeal;
}
//getter for Damage
int gladiator::getDamage() const
{
return damage;
}
//setter for SeedR
void gladiator::setSeedR(int s)
{
seedR = s;
}
//setter for HP
void gladiator::setHP(int s)
{
HP = s;
}
//setter for As
void gladiator::setAS(double s)
{
AS = s;
}
//setter for Ds
void gladiator::setDS(double s)
{
DS = s;
}
//setter for appeal
void gladiator::setAppeal(int s)
{
appeal = s;
}
//setter for damage
void gladiator::setDamage(int s)
{
damage = s;
}
//implementation of attack
int gladiator::attack(gladiator& glad)
{
int renown = 0;
double random = 0 + (1-0)*rand()/((double) RAND_MAX);
if(random < AS)
{
renown += 10;
double random_2 = 0 + (1-0)*rand()/((double) RAND_MAX);
if (!(random_2 <= glad.getDS()))
{
glad.setHP(glad.getHP()-damage);
if(glad.getHP() <= 0)
{
renown += 30;
}
}
}else
{
renown -= 10;
}
return appeal+renown; //returning renown + appeal score of attacking glad
}
string gladiator::taunt()
{
}
void gladiator::print()
{
}
void gladiator::defenceBolster()
{
}
void gladiator::specialAction()
{
}
-----------------------------------------------------------------------------------------------------------------------------------------------
trax.h
#ifndef TRAX_H
#define TRAX_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class trax:public gladiator
{
int sprints;
public:
int getSprints();
void setSprints(int a);
trax();
~trax();
void print();
string taunt();
void defenceBolster();
void specialAction();
};
#endif
-----------------------------------------------------------------------------------------------------------------------------------
murmillo.h
#ifndef MURMILLO_H
#define MURMILLO_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class murmillo:public gladiator
{
public:
murmillo();
~murmillo();
string taunt();
void print();
void defenceBolster();
void specialAction();
};
#endif
------------------------------------------------------------------------------------------------------------------------------------------
retiarus.h
#ifndef RETIARIUS_H
#define RETIARIUS_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class retiarius:public gladiator
{
private:
int control;
public:
retiarius();
~retiarius();
int getControl();
void setControl(int);
string taunt();
void defenceBolster();
void specialAction();
void print();
};
#endif
----------------------------------------------------------------------------------------------------------------------------------
dimachaerus.h
#ifndef DIMACHAERUS_H
#define DIMACHAERUS_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class dimachaerus:public gladiator
{
public:
dimachaerus();
~dimachaerus();
string taunt();
void defenceBolster();
void specialAction();
void print();
};
#endif
team.h
#ifndef TEAM_H
#define TEAM_H
#include "gladiator.h"
#include "trax.h"
#include "dimachaerus.h"
#include "retiarius.h"
#include "murmillo.h"
#include <typeinfo>
#include <string>
using namespace std;
class team
{
public:
//constructor
team();
virtual ~team(); //destructor
//getting and setters for size
int getSize() const ;
void setSize(int s);
//overloading operations
gladiator& operator[](int i) const ; //to get gladiator at index i
team& operator+(int * add) ; //to add array of gladiators to existing team
team& operator-(int * sub) ; //removing array of gladiators to existing team
team operator--() ; //deleting entire team
//friend function overloading outstream
friend ostream& operator<<(ostream & output,const team & s1)
{
string data = "";
for(int i=0; i<s1.getSize(); i++)
{
data = data + "Gladiator "+to_string(i+1)+" : "+typeid(s1[i]).name()+" ";
}
output<<data<<endl;
return output;
}
protected:
private: //properties
gladiator ** fightingTeam; //list of gladiators dynamic array
int teamSize; //size of team
};
#endif // TEAM_H
team.h
#ifndef TEAM_H
#define TEAM_H
#include "gladiator.h"
#include "trax.h"
#include "dimachaerus.h"
#include "retiarius.h"
#include "murmillo.h"
#include <typeinfo>
#include <string>
using namespace std;
class team
{
public:
//constructor
team();
virtual ~team(); //destructor
//getting and setters for size
int getSize() const ;
void setSize(int s);
//overloading operations
gladiator& operator[](int i) const ; //to get gladiator at index i
team& operator+(int * add) ; //to add array of gladiators to existing team
team& operator-(int * sub) ; //removing array of gladiators to existing team
team operator--() ; //deleting entire team
//friend function overloading outstream
friend ostream& operator<<(ostream & output,const team & s1)
{
string data = "";
for(int i=0; i<s1.getSize(); i++)
{
data = data + "Gladiator "+to_string(i+1)+" : "+typeid(s1[i]).name()+" ";
}
output<<data<<endl;
return output;
}
protected:
private: //properties
gladiator ** fightingTeam; //list of gladiators dynamic array
int teamSize; //size of team
};
#endif // TEAM_H
----------------------------------------------------------------------------------------------------------------------------------
team.cpp
#include "team.h"
//impmlentation of construtor
team::team()
{
teamSize = 0;
}
//getters and setters for teamsize
int team::getSize() const
{
return teamSize;
}
void team::setSize(int s)
{
teamSize = s;
fightingTeam = new gladiator*[teamSize];
}
//overloaidng bracket operator returns gladiator at index i
gladiator& team::operator[](int i) const
{
if(i>getSize())
{
cout <<"ERROR! should be less than "<<getSize()<<endl;
}
return *fightingTeam[i];
}
//overloading + operator : adds new team to existing team
// based on integer code arrray
team& team::operator+(int* add)
{
//getting size of integer array
int n = 0;
for(;add[n];n++);
//creating new array of pointers with new size
int new_size = n+getSize();
gladiator ** newArr = new gladiator*[new_size];
//copying old array into new array
for (int i = 0; i < getSize(); i ++){
newArr[i] = fightingTeam[i];
}
//deleting old array
delete[] fightingTeam;
//assigning new array into old array
fightingTeam = newArr;
//getting current size
int current = getSize();
//setting size into new
setSize(new_size);
//loop till integer array
for(int i = 0; i<n; i++)
{
gladiator* member;
int code = add[i]; //getting integer code
//using switch to add member based on code given in matrix
switch (code)
{
case 1: //adding trax
printf("adding Trax to team... ");
member = new trax();
break;
case 2: //adding murmillo
printf("adding Murmillo to team... ");
member = new murmillo();
break;
case 3: //adding retiarius
printf("adding Retiarius to team... ");
member = new retiarius();
break;
case 4: //adding dimachaerue
printf("adding Dimachaerus to team... ");
member = new dimachaerus();
break;
default: //default handler
printf("ERROR! choice should be 1-4 ");
break;
}
// now adding this new member into our resized array
if(current < getSize())
{
fightingTeam[current] = member;
current++;
}
else
printf("ERROR! current size > actual size... ");
}
}
//overloading operator - : removes these integer matched mmebers array members from team
team& team::operator-(int* sub)
{
//getting size of integer array
int n = 0;
for(;sub[n];n++);
//loop till size of integer array
for(int i = 0; i<n; i++)
{
int code = sub[i]; //getting code
switch (code) //using switch cases for code
{
case 1:
printf("deleting Trax from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(trax)) //checking for this type
delete fightingTeam[j]; //if found delete the member from array
fightingTeam[j] = fightingTeam[getSize()-1]; //replacing this deleted with last member
setSize(getSize()-1); //reducing size by 1
}
break;
case 2:
printf("deleting Murmillo from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(murmillo))
delete fightingTeam[j];
fightingTeam[j] = fightingTeam[getSize()-1];
setSize(getSize()-1);
}
break;
case 3:
printf("deleting Retiarius from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(retiarius))
delete fightingTeam[j];
fightingTeam[j] = fightingTeam[getSize()-1];
setSize(getSize()-1);
}
break;
case 4:
printf("deleting Dimachaerus from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(dimachaerus))
delete fightingTeam[j];
fightingTeam[j] = fightingTeam[getSize()-1];
setSize(getSize()-1);
}
break;
default:
printf("ERROR! choice should be 1-4 ");
break;
}
}
}
//overloading -- operator : clears array
team team::operator--()
{
delete[] fightingTeam;
return *this;
}
//destructor
team::~team()
{
delete[] fightingTeam;
}
Explanation / Answer
#ifndef GLADIATOR_H
#define GLADIATOR_H
#include <iostream>
#include <string>
using namespace std;
class gladiator
{
public:
//constructor and destructor
gladiator();
virtual ~gladiator();
//generate random number by using seedR property
void setRandomSeed();
//getters for gladiator properties
int getSeedR() const;
int getHP() const;
double getAS() const;
double getDS() const;
int getAppeal() const;
int getDamage() const;
//setters for gladiator properties
void setSeedR(int s);
void setHP(int s);
void setAS(double s);
void setDS(double s);
void setAppeal(int s);
void setDamage(int s);
//function to attack other gladiator
int attack(gladiator & glad);
//virtual functions implemented by child classes
virtual string taunt() = 0;
virtual void print() = 0;
virtual void defenceBolster() = 0;
virtual void specialAction() = 0;
private:
int HP; //gladiator's health
double AS; //Attack skill probability of attack 0-1 by them
double DS; // Defense skill probability attack to them 0-1
int appeal; //represent charisma and crowd applause
int damage; //physical damage capacity
int seedR; // value used when generating randoms seed
};
#endif
--------------------------------------------------------------------------------------------------------------------------
gladiator.cpp
#include "gladiator.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <map>
using namespace std;
//constructor implementation
gladiator::gladiator()
{
}
//destructor
gladiator::~gladiator()
{
}
//generating random seed value
void gladiator::setRandomSeed()
{
srand(seedR);
}
//getter for seedR
int gladiator::getSeedR() const
{
return seedR;
}
//getter for HP
int gladiator::getHP() const
{
return HP;
}
//getter for AS
double gladiator::getAS() const
{
return AS;
}
//getter for DS
double gladiator::getDS() const
{
return DS;
}
//getter for Appeal
int gladiator::getAppeal() const
{
return appeal;
}
//getter for Damage
int gladiator::getDamage() const
{
return damage;
}
//setter for SeedR
void gladiator::setSeedR(int s)
{
seedR = s;
}
//setter for HP
void gladiator::setHP(int s)
{
HP = s;
}
//setter for As
void gladiator::setAS(double s)
{
AS = s;
}
//setter for Ds
void gladiator::setDS(double s)
{
DS = s;
}
//setter for appeal
void gladiator::setAppeal(int s)
{
appeal = s;
}
//setter for damage
void gladiator::setDamage(int s)
{
damage = s;
}
//implementation of attack
int gladiator::attack(gladiator& glad)
{
int renown = 0;
double random = 0 + (1-0)*rand()/((double) RAND_MAX);
if(random < AS)
{
renown += 10;
double random_2 = 0 + (1-0)*rand()/((double) RAND_MAX);
if (!(random_2 <= glad.getDS()))
{
glad.setHP(glad.getHP()-damage);
if(glad.getHP() <= 0)
{
renown += 30;
}
}
}else
{
renown -= 10;
}
return appeal+renown; //returning renown + appeal score of attacking glad
}
string gladiator::taunt()
{
}
void gladiator::print()
{
}
void gladiator::defenceBolster()
{
}
void gladiator::specialAction()
{
}
-----------------------------------------------------------------------------------------------------------------------------------------------
trax.h
#ifndef TRAX_H
#define TRAX_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class trax:public gladiator
{
int sprints;
public:
int getSprints();
void setSprints(int a);
trax();
~trax();
void print();
string taunt();
void defenceBolster();
void specialAction();
};
#endif
-----------------------------------------------------------------------------------------------------------------------------------
murmillo.h
#ifndef MURMILLO_H
#define MURMILLO_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class murmillo:public gladiator
{
public:
murmillo();
~murmillo();
string taunt();
void print();
void defenceBolster();
void specialAction();
};
#endif
------------------------------------------------------------------------------------------------------------------------------------------
retiarus.h
#ifndef RETIARIUS_H
#define RETIARIUS_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class retiarius:public gladiator
{
private:
int control;
public:
retiarius();
~retiarius();
int getControl();
void setControl(int);
string taunt();
void defenceBolster();
void specialAction();
void print();
};
#endif
----------------------------------------------------------------------------------------------------------------------------------
dimachaerus.h
#ifndef DIMACHAERUS_H
#define DIMACHAERUS_H
#include "gladiator.h"
#include <iostream>
#include <string>
using namespace std;
class dimachaerus:public gladiator
{
public:
dimachaerus();
~dimachaerus();
string taunt();
void defenceBolster();
void specialAction();
void print();
};
#endif
team.h
#ifndef TEAM_H
#define TEAM_H
#include "gladiator.h"
#include "trax.h"
#include "dimachaerus.h"
#include "retiarius.h"
#include "murmillo.h"
#include <typeinfo>
#include <string>
using namespace std;
class team
{
public:
//constructor
team();
virtual ~team(); //destructor
//getting and setters for size
int getSize() const ;
void setSize(int s);
//overloading operations
gladiator& operator[](int i) const ; //to get gladiator at index i
team& operator+(int * add) ; //to add array of gladiators to existing team
team& operator-(int * sub) ; //removing array of gladiators to existing team
team operator--() ; //deleting entire team
//friend function overloading outstream
friend ostream& operator<<(ostream & output,const team & s1)
{
string data = "";
for(int i=0; i<s1.getSize(); i++)
{
data = data + "Gladiator "+to_string(i+1)+" : "+typeid(s1[i]).name()+" ";
}
output<<data<<endl;
return output;
}
protected:
private: //properties
gladiator ** fightingTeam; //list of gladiators dynamic array
int teamSize; //size of team
};
#endif // TEAM_H
team.h
#ifndef TEAM_H
#define TEAM_H
#include "gladiator.h"
#include "trax.h"
#include "dimachaerus.h"
#include "retiarius.h"
#include "murmillo.h"
#include <typeinfo>
#include <string>
using namespace std;
class team
{
public:
//constructor
team();
virtual ~team(); //destructor
//getting and setters for size
int getSize() const ;
void setSize(int s);
//overloading operations
gladiator& operator[](int i) const ; //to get gladiator at index i
team& operator+(int * add) ; //to add array of gladiators to existing team
team& operator-(int * sub) ; //removing array of gladiators to existing team
team operator--() ; //deleting entire team
//friend function overloading outstream
friend ostream& operator<<(ostream & output,const team & s1)
{
string data = "";
for(int i=0; i<s1.getSize(); i++)
{
data = data + "Gladiator "+to_string(i+1)+" : "+typeid(s1[i]).name()+" ";
}
output<<data<<endl;
return output;
}
protected:
private: //properties
gladiator ** fightingTeam; //list of gladiators dynamic array
int teamSize; //size of team
};
#endif // TEAM_H
----------------------------------------------------------------------------------------------------------------------------------
team.cpp
#include "team.h"
//impmlentation of construtor
team::team()
{
teamSize = 0;
}
//getters and setters for teamsize
int team::getSize() const
{
return teamSize;
}
void team::setSize(int s)
{
teamSize = s;
fightingTeam = new gladiator*[teamSize];
}
//overloaidng bracket operator returns gladiator at index i
gladiator& team::operator[](int i) const
{
if(i>getSize())
{
cout <<"ERROR! should be less than "<<getSize()<<endl;
}
return *fightingTeam[i];
}
//overloading + operator : adds new team to existing team
// based on integer code arrray
team& team::operator+(int* add)
{
//getting size of integer array
int n = 0;
for(;add[n];n++);
//creating new array of pointers with new size
int new_size = n+getSize();
gladiator ** newArr = new gladiator*[new_size];
//copying old array into new array
for (int i = 0; i < getSize(); i ++){
newArr[i] = fightingTeam[i];
}
//deleting old array
delete[] fightingTeam;
//assigning new array into old array
fightingTeam = newArr;
//getting current size
int current = getSize();
//setting size into new
setSize(new_size);
//loop till integer array
for(int i = 0; i<n; i++)
{
gladiator* member;
int code = add[i]; //getting integer code
//using switch to add member based on code given in matrix
switch (code)
{
case 1: //adding trax
printf("adding Trax to team... ");
member = new trax();
break;
case 2: //adding murmillo
printf("adding Murmillo to team... ");
member = new murmillo();
break;
case 3: //adding retiarius
printf("adding Retiarius to team... ");
member = new retiarius();
break;
case 4: //adding dimachaerue
printf("adding Dimachaerus to team... ");
member = new dimachaerus();
break;
default: //default handler
printf("ERROR! choice should be 1-4 ");
break;
}
// now adding this new member into our resized array
if(current < getSize())
{
fightingTeam[current] = member;
current++;
}
else
printf("ERROR! current size > actual size... ");
}
}
//overloading operator - : removes these integer matched mmebers array members from team
team& team::operator-(int* sub)
{
//getting size of integer array
int n = 0;
for(;sub[n];n++);
//loop till size of integer array
for(int i = 0; i<n; i++)
{
int code = sub[i]; //getting code
switch (code) //using switch cases for code
{
case 1:
printf("deleting Trax from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(trax)) //checking for this type
delete fightingTeam[j]; //if found delete the member from array
fightingTeam[j] = fightingTeam[getSize()-1]; //replacing this deleted with last member
setSize(getSize()-1); //reducing size by 1
}
break;
case 2:
printf("deleting Murmillo from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(murmillo))
delete fightingTeam[j];
fightingTeam[j] = fightingTeam[getSize()-1];
setSize(getSize()-1);
}
break;
case 3:
printf("deleting Retiarius from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(retiarius))
delete fightingTeam[j];
fightingTeam[j] = fightingTeam[getSize()-1];
setSize(getSize()-1);
}
break;
case 4:
printf("deleting Dimachaerus from team... ");
for(int j = 0; j< getSize() ; j++)
{
if(typeid(fightingTeam[j]) == typeid(dimachaerus))
delete fightingTeam[j];
fightingTeam[j] = fightingTeam[getSize()-1];
setSize(getSize()-1);
}
break;
default:
printf("ERROR! choice should be 1-4 ");
break;
}
}
}
//overloading -- operator : clears array
team team::operator--()
{
delete[] fightingTeam;
return *this;
}
//destructor
team::~team()
{
delete[] fightingTeam;
}