Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Creature
- {
- public:
- Creature();
- void SetName(string name);
- string GetName();
- float GetHealth();
- void TakeDamage(float damage);
- protected:
- int NumberOfLimbs;
- private:
- string Name;
- float Health;
- };
- class Goblin : public Creature
- {
- public:
- Goblin();
- int GetNumberOfLimbs();
- };
- int main()
- {
- Creature Igor;
- // Even though Name is a private variable,
- // we were able to Set and Get the Name property
- Igor.SetName("Igor");
- cout << "Name: " << Igor.GetName() << endl;
- cout << "Health: " << Igor.GetHealth() << endl;
- cout << "Igor will not take 35 damage!" << endl;
- Igor.TakeDamage(35.0);
- //
- Goblin Gobby;
- cout << Gobby.GetName() << endl;
- cout << Gobby.GetNumberOfLimbs() << endl;
- system("pause");
- }
- Creature::Creature()
- {
- Health = 100.f;
- NumberOfLimbs = 4;
- cout << "A creature has been created! \n";
- }
- void Creature::SetName(string name)
- {
- Name = name;
- }
- string Creature::GetName()
- {
- return Name;
- }
- float Creature::GetHealth()
- {
- return Health;
- }
- void Creature::TakeDamage(float damage)
- {
- float Total;
- Total = Health - damage;
- if (Total <= 0.f)
- {
- cout << GetName() << " has died!\n";
- }
- else
- {
- Health -= damage;
- }
- cout << "Health: " << Health << endl;
- }
- Goblin::Goblin()
- {
- // No errors since NumberOfLimbs is in the
- // protected section of the Creature class
- // from which Goblin is derived.
- NumberOfLimbs = 5;
- SetName("Gobby");
- }
- int Goblin::GetNumberOfLimbs()
- {
- return NumberOfLimbs;
- }
Advertisement
Add Comment
Please, Sign In to add comment