99 bottles of beer: Difference between revisions

Content added Content deleted
m (→‎{{header|C++}}: Changed to cpp tag instead of c)
Line 55: Line 55:
=={{header|C++}}==
=={{header|C++}}==
=== The simple solution ===
=== The simple solution ===
<c>#include<iostream>
<cpp>#include<iostream>
using namespace std;
using namespace std;
int main() {
int main() {
Line 64: Line 64:
cout << x-1 << " bottles of beer on the wall" << endl << endl;
cout << x-1 << " bottles of beer on the wall" << endl << endl;
}
}
}</c>
}</cpp>


=== An object-oriented solution ===
=== An object-oriented solution ===
Line 71: Line 71:
{{works with|GCC|4.1.2 20061115 (prerelease) (SUSE Linux)}}
{{works with|GCC|4.1.2 20061115 (prerelease) (SUSE Linux)}}


<c>#include <iostream>
<cpp>#include <iostream>
#include <ostream>
#include <ostream>
#include <string>
#include <string>
Line 319: Line 319:
bottle_song::song song(100);
bottle_song::song song(100);
song.sing(std::cout);
song.sing(std::cout);
}</c>
}</cpp>


=== A template metaprogramming solution ===
=== A template metaprogramming solution ===
Of course, the output of the program always looks the same. One may therefore question why the program has to do all that tedious subtracting during runtime. Couldn't the compiler just generate the code to output the text, with ready-calculated constants? Indeed, it can, and the technique is called template metaprogramming. The following short code gives the text without containing a single variable, let alone a loop:
Of course, the output of the program always looks the same. One may therefore question why the program has to do all that tedious subtracting during runtime. Couldn't the compiler just generate the code to output the text, with ready-calculated constants? Indeed, it can, and the technique is called template metaprogramming. The following short code gives the text without containing a single variable, let alone a loop:


<c>#include <iostream>
<cpp>#include <iostream>
#include <ostream>
#include <ostream>


Line 351: Line 351:
{
{
bottle_countdown<100, 1>::print();
bottle_countdown<100, 1>::print();
}</c>
}</cpp>


=== A preprocessor solution ===
=== A preprocessor solution ===
Of course, with the template metaprogramming solution, the program has still do the conversion of numbers to strings at runtime, and those function calls also cost unnecessary time. Couldn't we just compose the complete text at compile time, and just output it at run time? Well, with the preprocessor, that's indeed possible:
Of course, with the template metaprogramming solution, the program has still do the conversion of numbers to strings at runtime, and those function calls also cost unnecessary time. Couldn't we just compose the complete text at compile time, and just output it at run time? Well, with the preprocessor, that's indeed possible:


<c>#include <iostream>
<cpp>#include <iostream>
#include <ostream>
#include <ostream>


Line 384: Line 384:
{
{
std::cout << SONG;
std::cout << SONG;
}</c>
}</cpp>


An inspection of the generated executable proves that it indeed contains the complete text of the song in one block.
An inspection of the generated executable proves that it indeed contains the complete text of the song in one block.