home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 1.ddi / OOPWLD.ZIP / POLY / STARS.CPP < prev   
Encoding:
C/C++ Source or Header  |  1990-07-28  |  959 b   |  40 lines

  1. // STARS.CPP: demonstration of new and delete
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. const scrwidth = 79;
  5. const scrheight = 24;
  6.  
  7. class star {  // a single star
  8.   int x, y;  // positions on the screen
  9. public:
  10.   star(int xpos, int ypos) : x(xpos), y(ypos) {
  11.     gotoxy(x,y);
  12.     cputs("*");
  13.   }
  14.   ~star() {
  15.     gotoxy(x,y);
  16.     cputs(" ");  // blank out the space
  17.   }
  18. };
  19.  
  20. class starbuffer {
  21.   enum { size = 128 };  // untyped enum acts as local const
  22.   star* ringbuffer[size];  // place to put pointers to stars
  23.   int index;
  24. public:
  25.   starbuffer() : index(0) {
  26.     for(int i = 0; i < size; i++)
  27.       ringbuffer[i] = (star*)0;
  28.   }
  29.   void add(star * s) { ringbuffer[index] = s; }
  30.   star * next() { return ringbuffer[ index = (++index % size) ]; }
  31. };
  32.  
  33. main() {
  34.   starbuffer stars;
  35.   while(!kbhit()) {
  36.     delete stars.next();  // deleting 0 does nothing
  37.     stars.add(new star(rand() % scrwidth, rand() % scrheight));
  38.   }
  39. }
  40.