dburner

asd

Nov 7th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <string>
  2. #include <stdlib.h>
  3. #include <iostream>
  4.  
  5. #include <vector>
  6. #include <list>
  7.  
  8. #define for_each(type, list, itName) for(type::const_iterator itName = list.begin(); itName != list.end(); itName++)
  9.  
  10. using namespace std;
  11.  
  12. /*
  13. char* copiazaSirDeCaractere(const char* valoare) {
  14.     char* copie = new char[strlen(valoare)];
  15.     strcpy(copie, valoare);
  16.     return copie;
  17. }
  18. */
  19.  
  20. class Animal {
  21. private:
  22.     const string _nume;
  23.     Animal& operator=(const Animal& other);
  24. protected:
  25.     virtual const char* interjectie() const = 0;
  26.     const string& getNume() const {
  27.         return _nume;
  28.     }
  29. public:
  30.     Animal(const char* nume) :_nume(nume){
  31.     }
  32.  
  33.     Animal(const string& nume) :_nume(nume){
  34.     }
  35.  
  36.     virtual ~Animal() {
  37.         //delete []_nume;
  38.     }
  39.  
  40.     Animal(const Animal& other) :_nume(other._nume) {
  41.     }
  42.  
  43.     void canta() const {
  44.         cout << interjectie();
  45.         cout << ", ma numesc " << _nume << endl;
  46.     }
  47.  
  48.     virtual Animal* clone() = 0;
  49. };
  50.  
  51. class Oaie: public Animal {
  52. public:
  53.     Oaie(const char* nume) : Animal(nume) {
  54.     }
  55.  
  56.     Oaie(const string& nume) : Animal(nume) {
  57.     }
  58.  
  59.     virtual const char* interjectie() const {
  60.         return "bee";
  61.     }
  62.     Animal* clone() {
  63.         return new Oaie(getNume());
  64.     }
  65. };
  66.  
  67.  
  68. class Caine: public Animal {
  69. public:
  70.     Caine(const char* nume) : Animal(nume) {
  71.     }
  72.  
  73.     Caine(const string& nume) : Animal(nume) {
  74.     }
  75.  
  76.     virtual const char* interjectie() const {
  77.         return "ham";
  78.     }
  79.  
  80.     Animal* clone() {
  81.         return new Caine(getNume());
  82.     }
  83. };
  84.  
  85.  
  86. class Vaca: public Animal {
  87. public:
  88.     Vaca(const char* nume) : Animal(nume) {
  89.     }
  90.  
  91.     Vaca(const string& nume) : Animal(nume) {
  92.     }
  93.  
  94.     virtual const char* interjectie() const {
  95.         return "muu";
  96.     }
  97.     Animal* clone() {
  98.         return new Vaca(getNume());
  99.     }
  100. };
  101.  
  102. class Rata: public Animal {
  103. public:
  104.     Rata(const char* nume) : Animal(nume) {
  105.     }
  106.  
  107.     Rata(const string& nume) : Animal(nume) {
  108.     }
  109.  
  110.     virtual const char* interjectie() const {
  111.         return "mac mac";
  112.     }
  113.     Animal* clone() {
  114.         return new Rata(getNume());
  115.     }
  116. };
  117.  
  118.  
  119. class Ferma {
  120. private:
  121.     //Animal** animale;
  122.     list<Animal*> animale;
  123.     unsigned int capacitate;
  124.     //unsigned int dimensiuneaActuala;
  125. public:
  126.  
  127.     class PreaMulteAnimale {};
  128.  
  129.     Ferma(unsigned int numarMaximAnimale) {
  130.         capacitate = numarMaximAnimale;
  131.     }
  132.  
  133.     Ferma(const Ferma& other) {
  134.         capacitate = other.capacitate;
  135.  
  136.         for(list<Animal*>::const_iterator it = other.animale.begin(); it != other.animale.end(); it++) {
  137.             animale.push_back((*it)->clone());
  138.         }
  139.     }
  140.  
  141.     ~Ferma() {
  142.         for(list<Animal*>::iterator it = animale.begin(); it != animale.end(); it++) {
  143.             delete (*it);
  144.         }
  145.     }
  146.  
  147.     Ferma& operator=(const Ferma& other) {
  148.         if(this == &other) {
  149.             return *this;
  150.         }
  151.  
  152.         for(list<Animal*>::iterator it = animale.begin(); it != animale.end(); it++) {
  153.             delete (*it);
  154.         }
  155.         animale.clear();
  156.         capacitate = other.capacitate;
  157.  
  158.         for(list<Animal*>::const_iterator it = other.animale.begin(); it != other.animale.end(); it++) {
  159.             animale.push_back((*it)->clone());
  160.         }
  161.  
  162.         return *this;
  163.     }
  164.  
  165.  
  166.  
  167.     void gazduieste(Animal* animal) {
  168.         if (animale.size() >= capacitate) {
  169.             throw PreaMulteAnimale();
  170.         }
  171.         animale.push_back(animal);
  172.     }
  173.  
  174.     void concert() const {
  175.         /*
  176.         for(list<Animal*>::const_iterator it = animale.begin(); it != animale.end(); it++) {
  177.             (*it)->canta();
  178.         }*/
  179.  
  180.         for_each(list<Animal*>, animale)
  181.         {
  182.             (*it)->canta();
  183.         }
  184.     }
  185. };
  186.  
  187. void populeazaFerma(Ferma& ferma) {
  188.     ferma.gazduieste(new Caine("Pluto"   ));
  189.     ferma.gazduieste(new  Oaie("Bisisica"));
  190.     ferma.gazduieste(new  Oaie("Rozi"    ));
  191.     ferma.gazduieste(new  Vaca("Milka"   ));
  192.     ferma.gazduieste(new  Rata("Donald"  ));
  193.     ferma.gazduieste(new  Rata("Daffy"   ));
  194.     ferma.gazduieste(new  Rata("Alba"    ));
  195. }
  196.  
  197.  
  198. int main() {
  199.     const unsigned int CapacitateFerma = 10;
  200.     Ferma f(CapacitateFerma);
  201.     populeazaFerma(f);
  202.     f.concert();
  203. }
Advertisement
Add Comment
Please, Sign In to add comment