> cout << "hah!" << endl;
#include <iostream>
#include <string>
class Hah {
private:
std::string msg;
public:
Hah() {
this->msg = "Hah!";
}
std::string hah() {
return this->msg;
}
};
int main() {
Hah hah;
std::cout << hah.hah() << std::endl;
return 0;
}
There. Fixed that for you.
> "Alf P. Steinbach" <al...@start.no> writes:
>
> > cout << "hah!" << endl;
Flyttet til no.test
Thanks.
An idiomatic version of that program would be something like this:
<code>
#include <iostream>
#include <string>
class Hah
{
private:
std::string myMsg;
public:
Hah(): myMsg( "Hah!" ) {}
std::string hah() const { return myMsg; }
};
int main()
{
std::cout << Hah().hah() << std::endl;
}
</code>
Differences / best practice guidelines:
* Use initializer lists rather than assignment in constructor body.
* Add 'const' wherever practically possible.
* In C++ 'main' has a default result value, namely 0, and best practice
is to avoid redundant code that doesn't communicate significant
info to one reading the code (code is about communicating to programmers),
or has some other purpose such as supporting later modification.
In passing, if the text is changed from "Hah" to "Høh?" then the program will
still compile fine with MinGW g++ in Windows, and even yield the correct result
if the source code is written in Latin-1 and the program is run in the Windows
command interpreter with codepage 1252 (Windows ANSI Western, selected by e.g.
the chcp command), but this then is due to a *bug* in that compiler where it
doesn't detect that the source code contains invalid UTF-8 bytes.
Writing the source code in UTF-8 would be a workaround for that compiler bug,
but then the relevant Windows command interpreter codepage would be 65001, which
has not just one *bug* but a phletora of them; no output is guaranteed[1]...
In short, current C++ implementations for Windows fail the test of being able to
implement the Norwegian "Høh?" program without direct use of API functions, i.e.
using only standard core language and standard library functionality.
Unfortunately other programming languages do not fare very much better.
It's only 2009 -- we'll probably have to wait 30 years more for this basic
functionality to be in place...
Cheers & happy new year
- Alf
Notes:
[1] An interesting exercise in the Windows XP command interpreter:
C:\> chcp 65001
C:\> more