Constructors and Destructors - C ++

I need to write a program that displays 100 stars on the screen (in random places), and then the stars slowly disappear - one after another. I am not allowed to use loops or recursions. I tried to play with constructors and destructors, but I cannot make stars disappear one by one (and not all together). Any ideas?

Thank you Li

Sorry - forgot to mention that I use C ++

My current access violates useless code:

class star {
    int x;
    int y;
public:
    star(){
        x = rand()%80;
        y = rand()%80;
        PaintcharOnRandomLocation('*',x,y);
    };
    ~star(){
        PaintcharOnRandomLocation(' ',x,y);
    };

};

class printAll{
    star* arr;
public:
    printAll(){
    arr = new star[100];
    };


    ~printAll(){
        delete[] arr;
    };


};
void doNothing(printAll L){
};

void main()
{
    srand ( time(NULL) );   
    doNothing(printAll());

     getch();
};
+3
source share
4 answers

It seems the only way possible without loops / recursion is something like this:

class Star
{
  Star() 
  { 
     //constructor shows star in a a random place
  }
  ~Star()
  {
    //destructor removes star and sleeps for a random amount of time
  }
};

int main() 
{
   Star S[100];
}

, , , , .

, , , . , , .

+15

:

template<int N>
struct Star
{
   Star() { DrawAtRandomPlace(); }
   ~Star() { RemoveSlowly(); }
   Star<N-1> star;
};

template<> struct Star<0> {};

int main()
{
  Star<100> stars;
}

100 Star. RAII .

+8

, , star ? . sleep usleep.

+1

/ - , , , . , / , :

, , , .

, , .

, (!) :

main() , , .

0
source

Source: https://habr.com/ru/post/1761322/


All Articles