Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <time.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class tree {
  8. public:
  9.     int size,x,y;
  10.     tree() {
  11.         size = 10 + rand() % 50;
  12.         x = 10 + rand() % 500;
  13.         y = 10 + rand() % 500;
  14.         //printf("tree size = %d",size);
  15.     }
  16.     ~tree() {
  17.         printf("tree delete");
  18.     }
  19.     virtual void prt() {};
  20.     virtual void set_xy() {};
  21. };
  22.  
  23. class oak : public tree {
  24. public:
  25.     string name;
  26.     oak():tree() {
  27.         name = "oak";
  28.     }
  29.  
  30.     void prt() {
  31.         cout << "tree size = " << size << " meter name = " << name <<endl;
  32.     }
  33.     void set_xy(int x, int y) {
  34.         this->x = x;
  35.         this->y = y;
  36.     }
  37.     };
  38.  
  39. class birch : public tree {
  40. public:
  41.     string name;
  42.     birch() :tree() {
  43.         name = "birch";
  44.     }
  45.  
  46.     void prt() {
  47.         cout << "tree size = " << size << " meter name = " << name << endl;
  48.     }
  49.     void set_xy(int x, int y) {
  50.         this->x = x;
  51.         this->y = y;
  52.     }
  53. };
  54.  
  55. class maple : public tree {
  56. public:
  57.     string name;
  58.     maple() :tree() {
  59.         name = "maple";
  60.     }
  61.  
  62.     void prt() {
  63.         cout << "tree size = " << size << " meter name = " << name <<  endl;
  64.     }
  65.     void set_xy(int x, int y) {
  66.         this->x = x;
  67.         this->y = y;
  68.     }
  69. };
  70.  
  71. class MyStorage {
  72. private:
  73.     int _size;
  74.     tree **obj,**obj2;
  75. public:
  76.     MyStorage(int size) {
  77.         _size = size;
  78.         obj = new tree*[_size];
  79.     }
  80.     int getCount() {
  81.         return _size;
  82.     }
  83.     tree &getObject(int index) {
  84.         return *obj[index];
  85.     }
  86.     void setObject(int index, tree *tr) {
  87.         obj[index] = tr;
  88.     }
  89.     void add(int index, tree* tr) {
  90.         _size++;
  91.         obj2 = new tree * [_size];
  92.         obj2[index] = tr;
  93.         for (int i = 0; i < index; i++)
  94.             obj2[i] = obj[i];
  95.         for (int i = index+1; i < _size; i++)
  96.             obj2[i] = obj[i-1];
  97.         delete(obj);
  98.         obj = new tree * [_size];
  99.         for (int i = 0; i < _size; i++)
  100.             obj[i] = obj2[i];
  101.     }
  102.     void del(int index, tree* tr) {
  103.  
  104.     }
  105.  
  106. };
  107. int main()
  108. {
  109.     srand(time(NULL));
  110.     //MyStorage storage(12);
  111.     //cout << storage.getcount() << endl;
  112.     //scanf("%d", &a);
  113.     //cout << storage.getValue(a) << endl;
  114.     /*storage.setValue(3, new oak());
  115.     storage.getValue(3).prt();
  116.     /*oak* a = new oak();
  117.     a.*/
  118.     // ٌîçنàهى ًُàيèëèùه
  119.     MyStorage storage(10);
  120.     // نîلàâëےهى â يهمî îلْهêٍû
  121.     for (int i = 0; i < storage.getCount(); i++)
  122.         storage.setObject(i, new maple());
  123.     // îلًàùàهىٌے ïîî÷هًهنيî êî âٌهى
  124.     for (int i = 0; i < storage.getCount(); i++)
  125.         storage.getObject(i).prt();
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement