C++
15.15 Lab: Abstract base class
Base Class
The base Character class is located Character.h and cannot be changed.
enum HeroType {WARRIOR, ELF, WIZARD};
const double MAX_HEALTH = 100.0;
class Character {
protected:
HeroType type;
string name;
double health;
double attackStrength;
public:
Character(HeroType type, const string &name, double health, double attackStrength);
HeroType getType() const;
const string & getName() const;
/* Returns the whole number of the health value (static_cast to int). */
int getHealth() const;
void setHealth(double h);
/* Reduces health value by amount passed in. */
void damage(double d);
/* Returns true if getHealth() returns an integer greater than 0, otherwise false */
bool isAlive() const;
virtual void attack(Character &) = 0;
};
Derived Classes
Warrior
Stores the warrior's allegiance as a string.
The warrior does not attack warriors that have the same allegiance.
The damage done by the warrior is the percentage of the warrior's health remaining (health / MAX_HEALTH) multiplied by the warrior's attack strength.
Elf
Stores the elf's family name as a string.
The elf does not attack elf's from its own family.
The damage done by the elf is the percentage of the elf's health remaining (health / MAX_HEALTH) multiplied by the elf's attack strength.
Wizard
Stores the wizard's rank as an int.
When a wizard attacks another wizard, the damage done is the wizard's attack strength multiplied by the ratio of the attacking wizard's rank over the defending wizard's rank.
The damage done to non-wizards is just the attack strength. The wizard's health is not taken into consideration.
Dynamic casting type of Character in attack function
In order to access the Warrior data field allegiance using the Character reference passed in to the attack function, you will need to dynamic cast the Character reference to a Warrior reference.
Here's an example of dynamic casting a Character reference named opponent to a Warrior reference named opp:
Warrior &opp = dynamic_cast<Warrior &>(opponent);
You will need to do the same for the Wizard and Elf attack functions, only dynamic casting to Wizard or Elf reference instead.
HeroType
Notice the enum declaration above the Character class declaration. This creates a special type called HeroType that has the values, WARRIOR, ELF, and WIZARD. Those are the values you store in a variable of type HeroType. For example, you can initialize a variable of type HeroType and set it to the value of WARRIOR like this:
HeroType type = WARRIOR;
You can compare a variable named t of type HeroType to one of the HeroType values like this:
if (t == WARRIOR) {
// do something based on t being a warrior
}
Example main function
This shows you what your attack function should do in all situations. Look below this to find the main function you will need to pass the zyBook tests (this main() function has also been provided in the coding window for you).
#include <iostream>
using namespace std;
#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"
int main() {
Warrior w1("Arthur", 100, 5, "King George");
Warrior w2("Jane", 100, 6, "King George");
Warrior w3("Bob", 100, 4, "Queen Emily");
Elf e1("Raegron", 100, 4, "Sylvarian");
Elf e2("Cereasstar", 100, 3, "Sylvarian");
Elf e3("Melimion", 100, 4, "Valinorian");
Wizard wz1("Merlin", 100, 5, 10);
Wizard wz2("Adali", 100, 5, 8);
Wizard wz3("Vrydore", 100, 4, 6);
e1.attack(w1);
cout << endl;
e1.attack(e2);
cout << endl;
w2.attack(w1);
cout << endl;
w3.attack(w1);
cout << endl;
wz1.attack(wz2);
cout << endl;
wz1.attack(wz3);
cout << endl;
return 0;
}
The output of this main function will look like this:
Elf Raegron shoots an arrow at Arthur --- TWANG!!
Arthur takes 4 damage.
Elf Raegron does not attack Elf Cereasstar.
They are both members of the Sylvarian family.
Warrior Jane does not attack Warrior Arthur.
They share an allegiance with King George.
Warrior Bob attacks Arthur --- SLASH!!
Arthur takes 4 damage.
Wizard Merlin attacks Adali --- POOF!!
Adali takes 6.25 damage.
Wizard Merlin attacks Vrydore --- POOF!!
Vrydore takes 8.33333 damage.
main.cpp needed for zyBook tests
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "Character.h"
#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"
int main() {
int seed;
cout << "Enter seed value: ";
cin >> seed;
cout << endl;
srand(seed);
vector<Character *> adventurers;
adventurers.push_back(new Warrior("Arthur", MAX_HEALTH, 5, "King George"));
adventurers.push_back(new Warrior("Jane", MAX_HEALTH, 6, "King George"));
adventurers.push_back(new Warrior("Bob", MAX_HEALTH, 4, "Queen Emily"));
adventurers.push_back(new Elf("Raegron", MAX_HEALTH, 4, "Sylvarian"));
adventurers.push_back(new Elf("Cereasstar", MAX_HEALTH, 3, "Sylvarian"));
adventurers.push_back(new Elf("Melimion", MAX_HEALTH, 4, "Valinorian"));
adventurers.push_back(new Wizard("Merlin", MAX_HEALTH, 5, 10));
adventurers.push_back(new Wizard("Adali", MAX_HEALTH, 5, 8));
adventurers.push_back(new Wizard("Vrydore", MAX_HEALTH, 4, 6));
unsigned numAttacks = 10 + rand() % 11;
unsigned attacker, defender;
for (unsigned i = 0; i < numAttacks; ++i) {
attacker = rand() % adventurers.size();
do {
defender = rand() % adventurers.size();
} while (defender == attacker);
adventurers.at(attacker)->attack(*adventurers.at(defender));
cout << endl;
}
cout << "-----Health Remaining-----" << endl;
for (unsigned i = 0; i < adventurers.size(); ++i) {
cout << adventurers.at(i)->getName() << ": "
<< adventurers.at(i)->getHealth() << endl;
}
return 0;
}
Character.h
#include <string>
using namespace std;
#ifndef __CHARACTER_H__
#define __CHARACTER_H__
enum HeroType {WARRIOR, ELF, WIZARD};
const double MAX_HEALTH = 100.0;
class Character {
protected:
HeroType type;
string name;
double health;
double attackStrength;
public:
Character(HeroType, const string &, double, double);
HeroType getType() const;
const string & getName() const;
int getHealth() const;
void setHealth(double h);
void damage(double d);
bool isAlive() const;
virtual void attack(Character &) = 0;
};
#endif
Main.cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "Character.h"
#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"
int main() {
int seed;
cout << "Enter seed value: ";
cin >> seed;
cout << endl;
//Character.h
#ifndef CHARACTER_H
#define CHARACTER_H
#include<iostream>
#include<string>
using namespace std;
enum HeroType {WARRIOR, ELF, WIZARD};
const double MAX_HEALTH = 100.0;
class Character {
protected:
HeroType type;
string name;
double health;
double attackStrength;
public:
Character(HeroType type, const string &name, double health, double attackStrength);
HeroType getType() const;
const string & getName() const;
/* Returns the whole number of the health value (static_cast to int). */
int getHealth() const;
void setHealth(double h);
/* Reduces health value by amount passed in. */
void damage(double d);
/* Returns true if getHealth() returns an integer greater than 0, otherwise false */
bool isAlive() const;
virtual void attack(Character &) = 0;
};
#endif
//Character.cpp
#include<iostream>
#include "Character.h"
using namespace std;
Character::Character(HeroType type,const string &name, double health, double attackStrength){
this->type = type;
this->name.assign(name);
this->health = health;
this->attackStrength = attackStrength;
}
HeroType Character::getType() const{
return type;
}
const string & Character::getName() const{
return name;
}
/* Returns the whole number of the health value (static_cast to int). */
int Character::getHealth() const{
return health;
}
void Character::setHealth(double h){
this->health = h;
}
/* Reduces health value by amount passed in. */
void Character::damage(double d){
if(this->health - d > 0){
cout << this->getName() << " takes " << d << " damage" << endl;
this->health -= d;
}
else
{
this->health = 0;
}
}
/* Returns true if getHealth() returns an integer greater than 0, otherwise false */
bool Character::isAlive() const{
if(this->health > 0)
return true;
return false;
}
//Warrior.h
#ifndef WARRIOR_H
#define WARRIOR_H
#include "Character.h"
class Warrior : public Character
{
private:
string allegiance;
public:
Warrior(const string &name, double health, double attackStrength ,const string &allegiance);
void setAllegiance(string allegiance);
string getAllegiance();
void attack(Character &);
};
#endif
//Warrior.cpp
#include "Warrior.h"
using namespace std;
Warrior::Warrior(const string &name, double health, double attackStrength ,const string &allegiance):Character(WARRIOR, name,health, attackStrength ) {
this->allegiance.assign(allegiance);
}
void Warrior::setAllegiance(string allegiance){
this->allegiance.assign(allegiance);
}
string Warrior::getAllegiance(){
return this->allegiance;
}
void Warrior::attack(Character &ch){
if( ch.getType() == WARRIOR )
{
Warrior &opp = dynamic_cast<Warrior &>(ch);
if(this->allegiance.compare(opp.allegiance) == 0){
cout << "Warrior " << this->getName() << " does not attack Warrior " << ch.getName() <<"." << endl;
cout << "They share an allegiance with " << this->allegiance << ".";
return;
}
}
cout << "Warrior " << this->getName() << " attacks " << ch.getName() << " --- SLASH!!" << endl;
double damage = (this->health/ MAX_HEALTH)*this->attackStrength;
ch.damage(damage);
}
----------------------------------------------------------------
//Elf.h
#ifndef ELF_H
#define ELF_H
#include "Character.h"
class Elf : public Character
{
private:
string family_name;
public:
Elf(const string &name, double health, double attackStrength ,const string &family_name);
void setFamilyName(string fname);
string getFamilyName();
void attack(Character &);
};
#endif
//Elf.cpp
#include"Elf.h"
Elf::Elf(const string &name, double health, double attackStrength ,const string &family_name): Character(ELF, name,health, attackStrength ){
this->family_name.assign(family_name);
}
void Elf::setFamilyName(string fname){
this->family_name.assign(fname);
}
string Elf::getFamilyName(){
return this->family_name;
}
void Elf::attack(Character &ch){
cout << "Elf " << this->getName() << " shoots an Arrow at " << ch.getName() << " --- TWANG!!" << endl;
if(ch.getType() == ELF)
{
Elf &opp = dynamic_cast<Elf &>(ch);
if(this->family_name.compare(opp.family_name) == 0)
{
cout << "Elf " << this->getName() <<" does not attck Elf" << ch.getName() << endl;
cout << "They are both members of the " << this->family_name << endl;
return;
}
}
double damage = (this->health/ MAX_HEALTH)*this->attackStrength;
ch.damage(damage);
}
------------------------------------------------------------------------
//Wizard.h
#ifndef WIZARD_H
#define WIZARD_H
#include "Character.h"
class Wizard : public Character
{
private:
int rank;
public:
Wizard(const string &name, double health, double attackStrength , int rank);
void setRank(int );
int getRank();
void attack(Character &);
};
#endif
//Wizard.cpp
#include "Wizard.h"
Wizard::Wizard(const string &name, double health, double attackStrength , int rank): Character(WIZARD, name,health, attackStrength ){
this->rank = rank;
}
void Wizard::setRank(int r){
this->rank = r;
}
int Wizard::getRank(){
return this->rank;
}
void Wizard::attack(Character &ch){
double damage;
cout << "Wizard " << this->getName() << " attacks " << ch.getName() << " --- POOF!!" << endl;
if(ch.getType() == WIZARD){
Wizard &opp = dynamic_cast<Wizard &>(ch);
damage = opp.attackStrength * (this->rank/opp.rank);
}
else
{
damage = this->attackStrength;
}
ch.damage(damage);
}
...............................................................................---------------------------
//main.cpp
#include <iostream>
#include<string>
using namespace std;
#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"
int main()
{
Warrior w1("Arthur", 100, 5, "King George");
Warrior w2("Jane", 100, 6, "King George");
Warrior w3("Bob", 100, 4, "Queen Emily");
Elf e1("Raegron", 100, 4, "Sylvarian");
Elf e2("Cereasstar", 100, 3, "Sylvarian");
Elf e3("Melimion", 100, 4, "Valinorian");
Wizard wz1("Merlin", 100, 5, 10);
Wizard wz2("Adali", 100, 5, 8);
Wizard wz3("Vrydore", 100, 4, 6);
e1.attack(w1);
cout << endl;
e1.attack(e2);
cout << endl;
w2.attack(w1);
cout << endl;
w3.attack(w1);
cout << endl;
wz1.attack(wz2);
cout << endl;
wz1.attack(wz3);
cout << endl;
return 0;
}
//output:

C++ 15.15 Lab: Abstract base class Base Class The base Character class is located Character.h and...
Base Class enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected: HeroType type; string name; double health; double attackStrength; public: Character(HeroType type, const string &name, double health, double attackStrength); HeroType getType() const; const string & getName() const; /* Returns the whole number of the health value (static_cast to int). */ int getHealth() const; void setHealth(double h); /* Returns true if getHealth() returns an integer greater than 0, otherwise false */ bool isAlive() const; virtual void...
C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...
10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...
Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...
C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game we have four different types of Creatures: Humans, Cyberdemons, Balrogs, and elves. To represent one of these Creatures we might define a Creature class as follows: class Creature { private: int type; // 0 Human, 1 Cyberdemon, 2 Balrog, 3 elf int strength; // how much damage this Creature inflicts int hitpoints; // how much damage this Creature can sustain string getSpecies() const; //...
I am having an issue adding in the following strenght and defense criteria. *Mob, *charm, *Hogwarts. Can you help please? Requirements In this project, you will create a simple class hierarchy as the basis for a fantasy combat game. Your ‘universe’ contains Vampires, Barbarians, Blue Men, Medusa and Harry Potter. Each has characteristics for attack, defense, armor, and strength points as follows. Type Attack Defense Armor Strength Points Vampire1 1d12 1d6* Charm 1 18 Barbarian2 2d6 2d6 0 12 Blue...
USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds data about an item in a retail store. The class should have the following member variables: description: A string that holds a brief description of the item. unitsOnHand: An int that holds thw number of units currently in inventory. price: A double that holds that item's retail price. Write the following functions: -appropriate mutator functions -appropriate accessor functions - a default constructor that sets:...
How would I test this code thoroughly in a main.cpp file. Locomotive is an abstract base class. steam and dieselElectric are the derived classes. #ifndef COMMODITY_H #define COMMODITY_H #include <iostream> #include <string> using namespace std; class commodity { private: string name; int quantity; public: commodity(commodity *c); commodity(string name,int quantity); ~commodity(); string getName() const; int getQuantity() const; }; #endif #include "commodity.h" commodity::commodity(commodity *c) { this->name=c->getName(); this->quantity=c->getQuantity(); } commodity::commodity(string name,int quantity) { this->name=name; ...
Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public: static string weekDays[7]; void print() const; string nextDay() const; string prevDay() const; void addDay(int nDays); void setDay(string d); string getDay() const; dayType(); dayType(string d); private: string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...