Last active
April 21, 2017 21:23
-
-
Save peschkaj/003827fff0504d39cfdd4198252ca100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Monster { | |
public: | |
Monster(); | |
Monster(int initial_hp); | |
~Monster(); | |
void attack() const; | |
private: | |
int hp; | |
}; | |
class Orc : public Monster { | |
public: | |
Orc(); | |
Orc(int initial_hp); | |
~Orc(); | |
void attack() const; // hides the base class version | |
void pillage() const; | |
private: | |
// idk let's pretend it has a weapon | |
}; | |
void Monster::attack() const | |
{ | |
cout << "The Monster attacks you! It's mildly upsetting!" << endl; | |
} | |
void Orc::attack() const | |
{ | |
cout << "The Orc attacks you! It hurt a lot because they're usually quite strong." << endl; | |
} | |
void Orc::pillage() const | |
{ | |
cout << "The Orc pillaged your village and stole your favorite newspaper!!!!" << endl; | |
} | |
int main() | |
{ | |
Monster m; | |
Orc o; | |
m.attack(); | |
o.attack(); | |
return 0; | |
} | |
Monster::Monster() | |
: hp(100) | |
{ | |
} | |
Monster::Monster(int initial_hp) | |
: hp(initial_hp) | |
{ | |
} | |
Monster::~Monster() | |
{ | |
} | |
Orc::Orc() | |
{ | |
} | |
Orc::Orc(int initial_hp) | |
: Monster(initial_hp) | |
{ | |
} | |
Orc::~Orc() | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Monster | |
{ | |
public: | |
Monster(); | |
Monster(int initial_hp); | |
virtual ~Monster(); | |
virtual void attack() const; | |
private: | |
int hp; | |
}; | |
class Orc : public Monster | |
{ | |
public: | |
Orc(); | |
Orc(int initial_hp); | |
~Orc(); | |
void attack() const; | |
void pillage() const; | |
private: | |
// idk let's pretend it has a weapon | |
}; | |
void Monster::attack() const | |
{ | |
cout << "The Monster attacks you! It's mildly upsetting!" << endl; | |
} | |
void Orc::attack() const | |
{ | |
cout << "The Orc attacks you! It hurt a lot because they're usually quite strong." << endl; | |
} | |
void Orc::pillage() const | |
{ | |
cout << "The Orc pillaged your village and stole your favorite newspaper!!!!" << endl; | |
} | |
/* | |
* Instead of an object by reference, this function could | |
* work just as well if we passed in a Monster* | |
* | |
* But we would of course need to check for NULL in that case | |
*/ | |
void attack_with_monster(const Monster& monster) | |
{ | |
monster.attack(); | |
monster.pillage(); | |
/* monster.pillage(); | |
* ^ is not okay to do... yet. | |
* the pillage() member function is not | |
* a part of the inheritance hierarchy. */ | |
} | |
int main() | |
{ | |
Monster m; | |
Orc o; | |
attack_with_monster(m); | |
attack_with_monster(o); | |
/* | |
* Alternatively, I could do: | |
* Monster* my_monster = &m; | |
* OR | |
* Monster* my_monster = &o; | |
* | |
* THEN | |
* my_monster->attack(); | |
*/ | |
return 0; | |
} | |
Monster::Monster() | |
: hp(100) | |
{ | |
} | |
Monster::Monster(int initial_hp) | |
: hp(initial_hp) | |
{ | |
} | |
Monster::~Monster() | |
{ | |
} | |
Orc::Orc() | |
{ | |
} | |
Orc::Orc(int initial_hp) | |
: Monster(initial_hp) | |
{ | |
} | |
Orc::~Orc() | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Monster | |
{ | |
public: | |
Monster(); | |
Monster(int initial_hp); | |
virtual ~Monster(); | |
virtual void attack() const; | |
private: | |
int hp; | |
}; | |
class Orc : public Monster | |
{ | |
public: | |
Orc(); | |
Orc(int initial_hp); | |
~Orc(); | |
void attack() const; | |
void pillage() const; | |
private: | |
// idk let's pretend it has a weapon | |
}; | |
class Gargoyle : public Monster { | |
public: | |
void attack() const | |
{ | |
cout << "He burps." << endl; | |
} | |
private: | |
}; | |
void Monster::attack() const | |
{ | |
cout << "The Monster attacks you! It's mildly upsetting!" << endl; | |
} | |
void Orc::attack() const | |
{ | |
cout << "The Orc attacks you! It hurt a lot because they're usually quite strong." << endl; | |
} | |
void Orc::pillage() const | |
{ | |
cout << "The Orc pillaged your village and stole your favorite newspaper!!!!" << endl; | |
} | |
/* | |
* Instead of an object by reference, this function could | |
* work just as well if we passed in a Monster* | |
*/ | |
void attack_with_monster(const Monster& monster) | |
{ | |
monster.attack(); | |
/* If the Monster is an Orc, let's have it pillage! We | |
* challenge our audience with unique enemies and also | |
* we never programmed a way for the player to defend themselves! | |
* muahahahahahahahahah | |
*/ | |
/* the fancy term for this is RTTI = Run Time Type Identification */ | |
const Orc* is_orc = dynamic_cast<const Orc*>(&monster); | |
if (is_orc) { | |
is_orc->pillage(); | |
} | |
} | |
int main() | |
{ | |
Monster m; | |
Orc o; | |
attack_with_monster(m); | |
attack_with_monster(o); | |
return 0; | |
} | |
Monster::Monster() | |
: hp(100) | |
{ | |
} | |
Monster::Monster(int initial_hp) | |
: hp(initial_hp) | |
{ | |
} | |
Monster::~Monster() | |
{ | |
} | |
Orc::Orc() | |
{ | |
} | |
Orc::Orc(int initial_hp) | |
: Monster(initial_hp) | |
{ | |
} | |
Orc::~Orc() | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Monster | |
{ | |
public: | |
Monster(); | |
Monster(int initial_hp); | |
virtual ~Monster(); | |
virtual void attack() const; | |
/* Turns Monster into an ABC... all derived classes | |
* must provide their own implementation of `final_words()` | |
*/ | |
virtual void final_words() const = 0; | |
private: | |
int hp; | |
}; | |
class Orc : public Monster | |
{ | |
public: | |
Orc(); | |
Orc(int initial_hp); | |
~Orc(); | |
void attack() const; | |
void pillage() const; | |
void final_words() const; | |
private: | |
// idk let's pretend it has a weapon | |
}; | |
class Gargoyle : public Monster { | |
public: | |
void attack() const; | |
void final_words() const; | |
private: | |
}; | |
void Monster::attack() const | |
{ | |
cout << "The Monster attacks you! It's mildly upsetting!" << endl; | |
} | |
void Gargoyle::attack() const | |
{ | |
cout << "He burps." << endl; | |
} | |
void Orc::attack() const | |
{ | |
cout << "The Orc attacks you! It hurt a lot because they're usually quite strong." << endl; | |
} | |
void Orc::pillage() const | |
{ | |
cout << "The Orc pillaged your village and stole your favorite newspaper!!!!" << endl; | |
} | |
void Orc::final_words() const | |
{ | |
cout << "O, I am slain!" << endl; | |
} | |
void Gargoyle::final_words() const | |
{ | |
cout << "I'll be back!! I swear it!" << endl; | |
} | |
/* | |
* Instead of an object by reference, this function could | |
* work just as well if we passed in a Monster* | |
*/ | |
void attack_with_monster(const Monster& monster) | |
{ | |
monster.attack(); | |
/* If the Monster is an Orc, let's have it pillage! We | |
* challenge our audience with unique enemies and also | |
* we never programmed a way for the player to defend themselves! | |
* muahahahahahahahahah | |
*/ | |
/* the fancy term for this is RTTI = Run Time Type Identification */ | |
const Orc* is_orc = dynamic_cast<const Orc*>(&monster); | |
if (is_orc) { | |
is_orc->pillage(); | |
} | |
} | |
int main() | |
{ | |
/* | |
Monster m; | |
Orc o; | |
attack_with_monster(m); | |
attack_with_monster(o); | |
*/ | |
Monster* shawns[3] = {0}; | |
shawns[0] = new Orc; | |
shawns[1] = new Gargoyle; | |
shawns[0]->final_words(); | |
shawns[1]->final_words(); | |
delete shawns[0]; | |
delete shawns[1]; | |
return 0; | |
} | |
Monster::Monster() | |
: hp(100) | |
{ | |
} | |
Monster::Monster(int initial_hp) | |
: hp(initial_hp) | |
{ | |
} | |
Monster::~Monster() | |
{ | |
} | |
Orc::Orc() | |
{ | |
} | |
Orc::Orc(int initial_hp) | |
: Monster(initial_hp) | |
{ | |
} | |
Orc::~Orc() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment