Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C++

(object-oriented version)

Date:04/20/05
Author:Tim Robinson
URL:n/a
Comments:4
Info:n/a
Score: (2.95 in 20 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

VersionAuthorDateCommentsRate
extreme template metaprogrammingRichard Wolf04/20/055
hacking styleTim Robinson04/20/0514
meta programmingArion Lei04/20/055
Preprocessor & self-include recursionChocapic02/27/076
feature creepJono04/27/091
most extreme template metaprogrammingHenrik Theiling12/04/110
7Jono04/15/090
ob fuscatedTapi (Paddy O'Brien)08/11/054
GUI versionMartyn Davies05/28/051

Comments

>>  Thomas said on 07/25/05 03:54:26

Thomas Why didn't you just overload the insertion operator to accept an instance of shelf?

>>  Linkku said on 08/01/05 10:14:35

Linkku #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

Frederik Nice -- clever and playful.

>>  Lurveleven said on 05/31/10 12:02:21

Lurveleven The output is not correct.

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!

Name:

eMail:

URL:

Security Code:
  
Comment: