// ----------------------------------------------------------------------- // Abstract Creature // Demonstrates abstract classes // ----------------------------------------------------------------------- #include using namespace std; // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- class Creature //abstract class { public: Creature(int health = 100); virtual void Greet() const = 0; //pure virtual member function virtual void DisplayHealth() const; protected: int m_Health; }; // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- Creature::Creature(int health): m_Health(health) { // nothing else to do } // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- void Creature::DisplayHealth() const { cout << "Health: " << m_Health << endl; } // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- class Orc : public Creature { public: Orc(int health = 120); virtual void Greet() const; }; // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- Orc::Orc(int health): Creature(health) { // nothing else to do } // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- void Orc::Greet() const { cout << "With a grunt the orc says hello.\n"; } // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- // main - it all begins here // ----------------------------------------------------------------------- int main() { Creature* pCreature = new Orc(); pCreature->Greet(); pCreature->DisplayHealth(); return 0; }