Language C++
(object-oriented version)
Date: | 04/20/05 |
Author: | Tim Robinson |
URL: | n/a |
Comments: | 4 |
Info: | n/a |
Score: | (3.00 in 28 votes) |
// C++ version of 99 Bottles of Beer, object oriented paradigm // programmer: Tim Robinson timtroyr@ionet.net #include <fstream.h> enum Bottle { BeerBottle }; class Shelf { unsigned BottlesLeft; public: Shelf( unsigned bottlesbought ) : BottlesLeft( bottlesbought ) {} void TakeOneDown() { if (!BottlesLeft) throw BeerBottle; BottlesLeft--; } operator int () { return BottlesLeft; } }; int main( int, char ** ) { Shelf Beer(99); try { for (;;) { char *plural = (int)Beer !=1 ? "s" : ""; cout << (int)Beer << " bottle" << plural << " of beer on the wall," << endl; cout << (int)Beer << " bottle" << plural << " of beer," << endl; Beer.TakeOneDown(); cout << "Take one down, pass it around," << endl; plural = (int)Beer !=1 ? "s":""; cout << (int)Beer << " bottle" << plural << " of beer on the wall." << endl; } } catch ( Bottle ) { cout << "Go to the store and buy some more," << endl; cout << "99 bottles of beer on the wall." << endl; } return 0; }
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
hacking style | Tim Robinson | 04/20/05 | 14 | |
extreme template metaprogramming | Richard Wolf | 04/20/05 | 5 | |
meta programming | Arion Lei | 04/20/05 | 5 | |
Preprocessor & self-include recursion | Chocapic | 02/27/07 | 6 | |
feature creep | Jono | 04/27/09 | 1 | |
most extreme template metaprogramming | Henrik Theiling | 12/04/11 | 0 | |
7 | Jono | 04/15/09 | 0 | |
ob fuscated | Tapi (Paddy O'Brien) | 08/11/05 | 4 | |
GUI version | Martyn Davies | 05/28/05 | 1 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
Thomas said on 07/25/05 03:54:26
Why didn't you just overload the insertion operator to accept an instance of shelf?
Linkku said on 08/01/05 10:14:35
#include <iostream>
using namespace std;
enum Bottle { BeerBottle };
class Shelf {
unsigned BottlesLeft;
public:
Shelf( unsigned bottlesbought )
: BottlesLeft( bottlesbought )
{}
void TakeOneDown()
{
if (!BottlesLeft)
throw BeerBottle;
BottlesLeft--;
}
operator int () { return BottlesLeft; }
};
int main( int, char ** )
{
Shelf Beer(99);
try {
for (; {
const char *plural = (int)Beer!=1?"s":"";
cout << (int)Beer << " bottle" << plural
<< " of beer on the wall," << endl;
cout << (int)Beer << " bottle" << plural
<< " of beer," << endl;
Beer.TakeOneDown();
cout << "Take one down, pass it around," << endl;
cout << (int)Beer << " bottle" << ((int)Beer!=1?"s":""
<< " of beer on the wall." << endl;
}
}
catch ( Bottle ) {
cout << "Go to the store and buy some more," << endl;
cout << "99 bottles of beer on the wall." << endl;
}
return 0;
}
Frederik said on 04/17/10 13:32:32
Nice -- clever and playful.
Lurveleven said on 05/31/10 12:02:21
The output is not correct.