vladkomarr

objects_array

Sep 25th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //objects.h
  2.  
  3. #ifndef OBJECTS_H
  4. #define OBJECTS_H
  5.  
  6. class objects {
  7. private:
  8.     double data;                    // сами данные
  9.     static objects* array[255];
  10.     static int count;
  11. public:
  12.     objects(int data);
  13.     ~objects();
  14.     static int get_quantity();
  15.     static objects* get_link(int i);
  16. };
  17. #endif // OBJECTS_H
  18.  
  19. //objects.cpp
  20.  
  21. #include "objects.h"
  22. #include <iostream>
  23.  
  24. int objects::count = 0;
  25. objects* objects::array[255];
  26.  
  27. objects::objects(int data) {
  28.     array[count] = this;
  29.     count++;
  30.     this->data = data;
  31.     std::cout << "Added new element with data = " << this->data << std::endl;
  32. }
  33.  
  34. objects::~objects() {
  35.     count--;
  36.     std::cout << "Element has been deleted.\n";
  37. }
  38.  
  39. int objects::get_quantity() {
  40.     return count;
  41. }
  42.  
  43. objects* objects::get_link(int i) {
  44.     return array[i];
  45. }
  46.  
  47.  
  48. //main.cpp
  49. #include <iostream>
  50. #include <objects.h>
  51.  
  52. int main() {
  53.     for(int i = 0; i < 8; i++)
  54.         objects *temp = new objects(i*i);
  55.     std::cout << "\nNumber of elements: " << objects::get_quantity()
  56.               << "\nList of links to the registered objects:\n[ ";
  57.  
  58.     for(int i = 0; i < objects::get_quantity(); i++)
  59.         std::cout << objects::get_link(i) << " ";
  60.     std::cout << "]\n\n";
  61.  
  62.     for(int i = 0; i < 8; i++)
  63.         delete objects::get_link(i);
  64.     std::cout << "\nNumber of elements: " << objects::get_quantity() << ".\n";
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment