Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <stdlib.h>
- #include <iostream>
- #include <vector>
- #include <list>
- #define for_each(type, list, itName) for(type::const_iterator itName = list.begin(); itName != list.end(); itName++)
- using namespace std;
- /*
- char* copiazaSirDeCaractere(const char* valoare) {
- char* copie = new char[strlen(valoare)];
- strcpy(copie, valoare);
- return copie;
- }
- */
- class Animal {
- private:
- const string _nume;
- Animal& operator=(const Animal& other);
- protected:
- virtual const char* interjectie() const = 0;
- const string& getNume() const {
- return _nume;
- }
- public:
- Animal(const char* nume) :_nume(nume){
- }
- Animal(const string& nume) :_nume(nume){
- }
- virtual ~Animal() {
- //delete []_nume;
- }
- Animal(const Animal& other) :_nume(other._nume) {
- }
- void canta() const {
- cout << interjectie();
- cout << ", ma numesc " << _nume << endl;
- }
- virtual Animal* clone() = 0;
- };
- class Oaie: public Animal {
- public:
- Oaie(const char* nume) : Animal(nume) {
- }
- Oaie(const string& nume) : Animal(nume) {
- }
- virtual const char* interjectie() const {
- return "bee";
- }
- Animal* clone() {
- return new Oaie(getNume());
- }
- };
- class Caine: public Animal {
- public:
- Caine(const char* nume) : Animal(nume) {
- }
- Caine(const string& nume) : Animal(nume) {
- }
- virtual const char* interjectie() const {
- return "ham";
- }
- Animal* clone() {
- return new Caine(getNume());
- }
- };
- class Vaca: public Animal {
- public:
- Vaca(const char* nume) : Animal(nume) {
- }
- Vaca(const string& nume) : Animal(nume) {
- }
- virtual const char* interjectie() const {
- return "muu";
- }
- Animal* clone() {
- return new Vaca(getNume());
- }
- };
- class Rata: public Animal {
- public:
- Rata(const char* nume) : Animal(nume) {
- }
- Rata(const string& nume) : Animal(nume) {
- }
- virtual const char* interjectie() const {
- return "mac mac";
- }
- Animal* clone() {
- return new Rata(getNume());
- }
- };
- class Ferma {
- private:
- //Animal** animale;
- list<Animal*> animale;
- unsigned int capacitate;
- //unsigned int dimensiuneaActuala;
- public:
- class PreaMulteAnimale {};
- Ferma(unsigned int numarMaximAnimale) {
- capacitate = numarMaximAnimale;
- }
- Ferma(const Ferma& other) {
- capacitate = other.capacitate;
- for(list<Animal*>::const_iterator it = other.animale.begin(); it != other.animale.end(); it++) {
- animale.push_back((*it)->clone());
- }
- }
- ~Ferma() {
- for(list<Animal*>::iterator it = animale.begin(); it != animale.end(); it++) {
- delete (*it);
- }
- }
- Ferma& operator=(const Ferma& other) {
- if(this == &other) {
- return *this;
- }
- for(list<Animal*>::iterator it = animale.begin(); it != animale.end(); it++) {
- delete (*it);
- }
- animale.clear();
- capacitate = other.capacitate;
- for(list<Animal*>::const_iterator it = other.animale.begin(); it != other.animale.end(); it++) {
- animale.push_back((*it)->clone());
- }
- return *this;
- }
- void gazduieste(Animal* animal) {
- if (animale.size() >= capacitate) {
- throw PreaMulteAnimale();
- }
- animale.push_back(animal);
- }
- void concert() const {
- /*
- for(list<Animal*>::const_iterator it = animale.begin(); it != animale.end(); it++) {
- (*it)->canta();
- }*/
- for_each(list<Animal*>, animale)
- {
- (*it)->canta();
- }
- }
- };
- void populeazaFerma(Ferma& ferma) {
- ferma.gazduieste(new Caine("Pluto" ));
- ferma.gazduieste(new Oaie("Bisisica"));
- ferma.gazduieste(new Oaie("Rozi" ));
- ferma.gazduieste(new Vaca("Milka" ));
- ferma.gazduieste(new Rata("Donald" ));
- ferma.gazduieste(new Rata("Daffy" ));
- ferma.gazduieste(new Rata("Alba" ));
- }
- int main() {
- const unsigned int CapacitateFerma = 10;
- Ferma f(CapacitateFerma);
- populeazaFerma(f);
- f.concert();
- }
Advertisement
Add Comment
Please, Sign In to add comment