GregLeck

Untitled

Feb 16th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Creature
  6. {
  7. public:
  8.     Creature();
  9.  
  10.     void SetName(string name);
  11.     string GetName();
  12.  
  13.     float GetHealth();
  14.  
  15.     void TakeDamage(float damage);
  16.  
  17. protected:
  18.     int NumberOfLimbs;
  19.  
  20. private:
  21.     string Name;
  22.     float Health;
  23. };
  24.  
  25. class Goblin : public Creature
  26. {
  27. public:
  28.     Goblin();
  29.  
  30.     int GetNumberOfLimbs();
  31. };
  32.  
  33. int main()
  34. {
  35.     Creature Igor;
  36.     // Even though Name is a private variable,
  37.     // we were able to Set and Get the Name property
  38.     Igor.SetName("Igor");
  39.     cout << "Name: " << Igor.GetName() << endl;
  40.     cout << "Health: " << Igor.GetHealth() << endl;
  41.  
  42.     cout << "Igor will not take 35 damage!" << endl;
  43.     Igor.TakeDamage(35.0);
  44.  
  45.     //
  46.  
  47.     Goblin Gobby;
  48.     cout << Gobby.GetName() << endl;
  49.     cout << Gobby.GetNumberOfLimbs() << endl;
  50.  
  51.     system("pause");
  52. }
  53.  
  54. Creature::Creature()
  55. {
  56.     Health = 100.f;
  57.     NumberOfLimbs = 4;
  58.     cout << "A creature has been created! \n";
  59. }
  60.  
  61. void Creature::SetName(string name)
  62. {
  63.     Name = name;
  64. }
  65.  
  66. string Creature::GetName()
  67. {
  68.     return Name;
  69. }
  70.  
  71. float Creature::GetHealth()
  72. {
  73.     return Health;
  74. }
  75.  
  76. void Creature::TakeDamage(float damage)
  77. {
  78.     float Total;
  79.     Total = Health - damage;
  80.  
  81.     if (Total <= 0.f)
  82.     {
  83.         cout << GetName() << " has died!\n";
  84.     }
  85.     else
  86.     {
  87.         Health -= damage;
  88.     }
  89.  
  90.     cout << "Health: " << Health << endl;
  91. }
  92.  
  93. Goblin::Goblin()
  94. {
  95.     // No errors since NumberOfLimbs is in the
  96.     // protected section of the Creature class
  97.     // from which Goblin is derived.
  98.     NumberOfLimbs = 5;
  99.  
  100.     SetName("Gobby");
  101. }
  102.  
  103. int Goblin::GetNumberOfLimbs()
  104. {
  105.     return NumberOfLimbs;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment