Advertisement
TermSpar

Vector Iterations Tutorial

May 20th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Animals{
  8. public:
  9.     //Overloaded Constructor:
  10.     Animals(string);
  11.  
  12.     string getName();
  13.  
  14. private:
  15.     //Member Variables:
  16.     string newName;
  17. };
  18.  
  19. Animals::Animals(string name){
  20.     newName = name;
  21. }
  22.  
  23. string Animals::getName(){
  24.     return newName;
  25. }
  26.  
  27. class Zoo{
  28. public:
  29.     //Overloaded Constructor:
  30.     Zoo(int spaces);
  31.  
  32.     void AddAnimal(Animals);
  33.  
  34.     void displayZoo();
  35.  
  36. private:
  37.     //Member Variables:
  38.     vector<Animals> newZoo;
  39. };
  40.  
  41. Zoo::Zoo(int spaces){
  42.     newZoo.reserve(spaces);
  43. }
  44.  
  45. void Zoo::AddAnimal(Animals newAni){
  46.     newZoo.push_back(newAni);
  47. }
  48.  
  49. void Zoo::displayZoo(){
  50.     int aniCount = 0;
  51.     for(vector<Animals>::iterator iter = newZoo.begin(); iter != newZoo.end(); ++iter){
  52.         aniCount++;
  53.         cout << "Animal #" << aniCount << ": " << iter->getName() << "\n";
  54.     }
  55. }
  56.  
  57. int main(){
  58.     int zSize;
  59.     cout << "Enter Number of Animals: ";
  60.     cin >> zSize;
  61.  
  62.     Zoo myZoo(zSize);
  63.  
  64.     for(int i = 0; i < zSize; i++){
  65.         string uInput;
  66.         cout << "Enter Animal " << i + 1 << ": ";
  67.         cin >> uInput;
  68.  
  69.         Animals myAni(uInput);
  70.         myZoo.AddAnimal(myAni);
  71.     }
  72.  
  73.     myZoo.displayZoo();
  74.  
  75.     system("pause");
  76.     return 0;
  77.    
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement