Advertisement
TermSpar

Class Farm

May 20th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. //C++ Vector Class Iterations and Dynamic Memory Allocation: By Ben Bollinger
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. //Animal Class
  10. class Animals{
  11.    
  12.     public:
  13.         Animals(string name);
  14.        
  15.         void setHealth(int hp);
  16.        
  17.         string getName();
  18.        
  19.         int getHealth();
  20.        
  21.         void giveHealth();
  22.        
  23.     private:
  24.         string newName;
  25.         int newHealth;
  26.    
  27. };
  28.  
  29. Animals::Animals(string name){
  30.     newName = name;
  31. }
  32.  
  33. void Animals::setHealth(int hp){
  34.     newHealth = hp;
  35. }
  36.  
  37. inline string Animals::getName(){
  38.     return newName;
  39. }
  40.  
  41. int Animals::getHealth(){
  42.     return newHealth;
  43. }
  44.  
  45. //Farm Class
  46. class Farm{
  47.    
  48.     public:
  49.         Farm(int spaces);
  50.        
  51.         void AddAnimal(Animals);
  52.        
  53.         void displayFarm();
  54.        
  55.         void showHeath();
  56.        
  57.         void setAllHP();
  58.        
  59.         void displayMessage(string message);
  60.  
  61.         int getSize();
  62.        
  63.         Animals getAnimal(int indexPos);
  64.        
  65.     private:
  66.         vector<Animals> m_Farm;
  67. };
  68.  
  69. //Create a new farm
  70. Farm::Farm(int spaces){
  71.     m_Farm.reserve(spaces);
  72. }
  73.  
  74. //Add animal to farm
  75. void Farm::AddAnimal(Animals newAnimal){
  76.     m_Farm.push_back(newAnimal);
  77. }
  78.  
  79. //Display all animals in farm
  80. void Farm::displayFarm(){
  81.     int i = 0;
  82.     for(vector<Animals>::iterator iter = m_Farm.begin(); iter != m_Farm.end(); ++iter){
  83.         i++;
  84.         cout << "Animal #" << i << ": " << iter->getName() << "\n";
  85.     }
  86. }
  87.  
  88. //Allow user to see health of all animals
  89. void Farm::showHeath(){
  90.     cout << "\n";
  91.     for(vector<Animals>::iterator iter = m_Farm.begin(); iter != m_Farm.end(); ++iter){
  92.         cout << iter->getName() << "'s Health is " << iter->getHealth() << "\n";
  93.     }
  94. }
  95.  
  96. //Allow user to set health for all animals
  97. void Farm::setAllHP(){
  98.     for(vector<Animals>::iterator iter = m_Farm.begin(); iter != m_Farm.end(); ++iter){
  99.         int newHP;
  100.         cout << "Set Health For " << iter->getName() << ": ";
  101.         cin >> newHP;
  102.         iter->setHealth(newHP);
  103.     }
  104. }
  105.  
  106. void Farm::displayMessage(string message){
  107.     cout << "\n---------------";
  108.     cout << "\nFARM BROADCAST: '" << message << "'\n";
  109.     cout << "---------------\n";
  110. }
  111.  
  112. Animals Farm::getAnimal(int indexPos){
  113.     vector<Animals>::iterator iter = m_Farm.begin() + indexPos - 1;
  114.     Animals returnAni = *iter;
  115.  
  116.     return returnAni;
  117. }
  118.  
  119. int Farm::getSize(){
  120.     int farmSize;
  121.     farmSize = m_Farm.size();
  122.  
  123.     return farmSize;
  124. }
  125.  
  126. int main(){
  127.    
  128.     cout << "-------------------------------------------\n";
  129.     cout << "Welcome to C++ Class Farm: By Ben Bollinger\n";
  130.     cout << "-------------------------------------------\n";
  131.    
  132.     int fSize;
  133.     cout << "Enter How many Animals You Want: ";
  134.    
  135.     cin >> fSize;
  136.     Farm newFarm(fSize);
  137.    
  138.     //User enters animals to farm
  139.     for(int i = 0; i < fSize; i++){
  140.         string uInput;
  141.         cout << "Enter Name For Animal " << i + 1 << ": ";
  142.         cin >> uInput;
  143.         Animals newAni(uInput);
  144.         newFarm.AddAnimal(newAni);
  145.     }
  146.    
  147.     newFarm.displayMessage("Showing Animals");
  148.     newFarm.displayFarm();
  149.     cout << "\n";
  150.  
  151.     cout << "There are " << newFarm.getSize() << " animals in your farm\n\n";
  152.    
  153.     //Set and display Farm's health:
  154.     newFarm.setAllHP();
  155.     newFarm.showHeath();
  156.    
  157.     string yesOrNo;
  158.     newFarm.displayMessage("Change health of an animal? (y/n)");
  159.     cin >> yesOrNo;
  160.     if(yesOrNo == "y"){
  161.         int ani;
  162.         newFarm.displayMessage("Which Animal? (Animal Number)");
  163.         cin >> ani;
  164.         Animals setAni = newFarm.getAnimal(ani);
  165.         int hp;
  166.         newFarm.displayMessage("Set Health to?");
  167.         cin >> hp;
  168.         setAni.setHealth(hp);
  169.  
  170.         cout << "\n" << setAni.getName() << "'s new health is " << setAni.getHealth() << "\n\n";
  171.        
  172.     }else{
  173.         newFarm.displayMessage("Game Over");
  174.     }
  175.  
  176.     system("pause");
  177.    
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement