1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream>
class Actor { // abstract base class
public:
Actor(double health_arg = 100.0, double attack_arg = 0.0,
std::string whatis_arg = "Dinosaur");
std::string getName() const { return whatis; }
double getHealth() const { return health; }
double getAttack() const { return attack; }
protected:
double health {},
attack {};
std::string whatis;
virtual void calculateAttack() = 0;
friend std::ostream& operator<<(std::ostream& os, const Actor* rhs)
{
return os << rhs->whatis << ": health: " << rhs->health
<< "; attack: " << rhs->attack;
}
};
Actor::Actor(double health_arg, double attack_arg, std::string whatis_arg)
: health { health_arg }, attack { attack_arg }, whatis { whatis_arg }
{}
class Velociraptor : public Actor {
public:
Velociraptor(int teeth_arg = 75, int pack_arg = 0, double health_arg = 80.0,
double attack_arg = 0, std::string whatis_arg = "Velociraptor");
protected:
void calculateAttack() override;
private:
int teeth,
pack;
friend std::ostream& operator<<(std::ostream& os, const Velociraptor* rhs)
{
return os << reinterpret_cast<const Actor*>(rhs)
<< " (further details: teeth: " << rhs->teeth
<< "; member of a pack of " << rhs->pack << ')';
}
};
Velociraptor::Velociraptor(int teeth_arg, int pack_arg, double health_arg,
double attack_arg, std::string whatis_arg)
: Actor(health_arg, attack_arg, whatis_arg)
{
teeth = teeth_arg;
pack = pack_arg;
calculateAttack();
}
void Velociraptor::calculateAttack()
{
// attack --> given value + teeth + number of element in the pack * 2
// (the more they are, the more brave each one is)
attack += teeth + pack * 2;
}
class TRex : public Actor {
public:
TRex(double weight_arg = 1000.00, int hunger_arg = 0, double health_arg = 200.00,
double attack_arg = 0.0, std::string whatis_arg = "Tyrannosaurus Rex");
protected:
void calculateAttack() override;
private:
double weight;
int hunger;
friend std::ostream& operator<<(std::ostream& os, const TRex* rhs)
{
return os << reinterpret_cast<const Actor*>(rhs)
<< " (further details: weight: " << rhs->weight
<< "; it hasn't eaten for " << rhs->hunger << " days)";
}
};
TRex::TRex(double weight_arg, int hunger_arg, double health_arg,
double attack_arg, std::string whatis_arg)
: Actor(health_arg, attack_arg, whatis_arg)
{
weight = weight_arg;
hunger = hunger_arg;
calculateAttack();
}
void TRex::calculateAttack()
{
// attack -> given value + 10% * weight + hunger
// (it's big, it needs to eat everyday)
attack += weight / 10 + hunger;
}
class Player : public Actor {
public:
Player(int ammunition_arg = 0, double intelligence_arg = 100.0,
double health_arg = 20.0, double attack_arg = 0.0,
std::string whatis_arg = "Morituro");
protected:
void calculateAttack() override;
private:
int ammunition;
double intelligence;
friend std::ostream& operator<<(std::ostream& os, const Player* rhs)
{
return os << reinterpret_cast<const Actor*>(rhs)
<< " (further details: ammunition: " << rhs->ammunition
<< "; intelligence " << rhs->intelligence;
}
};
Player::Player(int ammunition_arg, double intelligence_arg, double health_arg,
double attack_arg, std::string whatis_arg)
: Actor(health_arg, attack_arg, whatis_arg)
{
ammunition = ammunition_arg;
intelligence = intelligence_arg;
calculateAttack();
}
void Player::calculateAttack()
{
// attack -> given value + 50% * intelligence + ammunition * 3
attack += intelligence / 2 + ammunition * 3;
}
void waitForEnter();
int main()
{
// Actor actor; <-- error: you can't have an instance of a abstract base class
Actor* vel = new Velociraptor; // but you can have a pointer of type Actor*
Actor* tyr = new TRex(1212.37, 5, 160, 13, "Fat Rex");
Actor* play = new Player;
std::cout << "Characteristic are:\n" << vel << '\n' << tyr << '\n' << play << '\n';
// Anyway, if you use the proper pointers, the compiler can understand better
// what you want:
Velociraptor* vel2 = reinterpret_cast<Velociraptor*>(vel);
TRex* tyr2 = reinterpret_cast<TRex*>(tyr);
Player* play2 = reinterpret_cast<Player*>(play);
std::cout << "\nCharacteristic are:\n" << vel2 << '\n' << tyr2 << '\n' << play2 << '\n';
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
|