Advertisement
Coriic

Untitled

Jun 21st, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Animal{
  4. public:
  5. virtual Animal* copy()const =0;
  6. };
  7.  
  8. class Dog:public Animal{
  9. public:
  10. Animal* copy()const{
  11. return new Dog(*this);
  12. }
  13. };
  14.  
  15. class Cat: public Animal{
  16. public:
  17. Animal* copy()const{
  18. return new Cat(*this);
  19. }
  20. };
  21.  
  22. class Frog: public Animal{
  23. public:
  24. Animal* copy() const{
  25. return new Frog(*this);
  26. }
  27. };
  28.  
  29. class AnimalWezel{
  30. public:
  31. AnimalWezel* next;
  32. Animal* wsk;
  33. };
  34.  
  35. class AnimalList{
  36. private:
  37. AnimalWezel* begin;
  38. AnimalWezel* end;
  39. public:
  40. AnimalList(): begin(0), end(0){}
  41. ~AnimalList();
  42. void add(Animal* a);
  43. AnimalList(const AnimalList&);
  44. AnimalList& operator=(const AnimalList&);
  45. void wypisz();
  46. };
  47.  
  48. AnimalList::~AnimalList() {
  49. AnimalWezel* it;
  50. while(begin!=0){
  51. it=begin;
  52. begin=begin->next;
  53. delete(it->wsk);
  54. delete(it);
  55. }
  56. begin=0;
  57. end=0;
  58. }
  59. void AnimalList::add(Animal* a){
  60. AnimalWezel* tmp=new AnimalWezel;
  61. if(begin==0){
  62. tmp->next=0;
  63. begin=tmp;
  64. end=tmp;
  65. }
  66. else{
  67. end->next=tmp;
  68. tmp->next=0;
  69. end=tmp;
  70. }
  71. tmp->wsk=a;
  72. }
  73.  
  74. AnimalList::AnimalList(const AnimalList& copy1) {
  75. AnimalWezel* it=copy1.begin;
  76. while(it!=0){
  77. this->add(it->wsk->copy());
  78. it=it->next;
  79. }
  80. }
  81.  
  82. AnimalList& AnimalList::operator=(const AnimalList& copy1){
  83. AnimalWezel* it;
  84. while(begin!=0){
  85. it=begin;
  86. begin=begin->next;
  87. delete(it->wsk);
  88. delete(it);
  89. }
  90. begin=0;
  91. end=0;
  92. it=copy1.begin;
  93. while(it!=0){
  94. this->add(it->wsk->copy());
  95. it=it->next;
  96. }
  97. return (*this);
  98. }
  99.  
  100. void AnimalList::wypisz() {
  101. AnimalWezel* it=this->begin;
  102. while(it!=0){
  103. std::cout << "Wezel listy" <<std::endl;
  104. it=it->next;
  105. }
  106. }
  107.  
  108. int main(){
  109. Dog* pies=new Dog;
  110. AnimalList zagroda;
  111. zagroda.add(pies);
  112. zagroda.wypisz();
  113. Cat* kot=new Cat;
  114. zagroda.add(kot);
  115. zagroda.wypisz();
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement