[C++] Fun Excercise
Posted: Wed Jan 12, 2011 8:51 pm
Here is your starting code.
Here is your target output.
I don't know why it won't render properly, but the second row onward (starting with "1 is born!") should be indented by one more space.
Write the code for main. Good luck.
Code: Select all
#include <iostream>
using namespace std;
class Blargh
{
public:
Blargh()
{
mIndex = index++;
cout << spaces << mIndex << " is born!\n";
*spaceUpdate++ = ' ';
}
~Blargh()
{
*--spaceUpdate = '\0';
cout << spaces << mIndex << " dies!\n";
}
private:
static size_t index;
static char spaces[128];
static char* spaceUpdate;
size_t mIndex;
};
size_t Blargh::index = 0;
char Blargh::spaces[128] = {};
char* Blargh::spaceUpdate = spaces;
int main(int argc, char** argv)
{
// ???
return 0;
}
Code: Select all
0 is born!
1 is born!
2 is born!
3 is born!
4 is born!
5 is born!
6 is born!
7 is born!
8 is born!
8 dies!
7 dies!
6 dies!
9 is born!
10 is born!
10 dies!
9 dies!
5 dies!
4 dies!
3 dies!
2 dies!
1 dies!
Write the code for main. Good luck.