Voting
Category
real language
Language C++
(object-oriented version)
| Date: | 04/20/05 |
| Author: | Tim Robinson |
| URL: | n/a |
| Comments: | 2 |
| Info: | n/a |
| Score: |
// 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 |
|---|---|---|---|---|
| extreme template metaprogramming | Richard Wolf | 04/20/05 | 5 | |
| meta programming | Arion Lei | 04/20/05 | 2 | |
| hacking style | Tim Robinson | 04/20/05 | 5 | |
| ob fuscated | Tapi (Paddy O'Brien) | 08/11/05 | 2 | |
| GUI version | Martyn Davies | 05/28/05 | 0 |
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 use the form to submit new examples!
Comments
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;
}