Subscribe to 10 GOTO 10        RSS Feed
***-- 1 Votes

C++ Hello World The OOP Way

Icon 4 Comments
I was browsing though some old computer books when I ran across, "C++ In Action: Industrial-Strength Programming Techniques" by Bartosz Milewski and I was intrigued at the approach taken. The classic "Hello World" example was given roughly as:

class World {
    World() {
        std::cout << "Hello World!" << std::endl;
    ~World() {
        std::cout << "Good Bye!" << std::endl;

World TheWorld;

int main() { return 0; }

(note: I changed things up just a bit to fit my sensibilities).

As I looked at the example I thought it was kind of neat. It introduces a host of talking points right off the bat.

First off it shows the class #include which gives you access to functions and objects in a library by telling the compiler you might use them.

Then there is the class that forms a blue-print of something called an object. Object are after all what Object Oriented Programming is oriented to. So the most basic example program seems like a good place to raise the issue. Here we create a blue-print for an object called "World" and our would must have a Genesis and thus the constructor, and all good things must end and thus the deconstructor. For the general public to be able to see these wonders, the constructor and deconstructor must be public. So we can spend a couple of lines talking about the that.

Next there is the instantiation of the object TheWorld.

Then there is the int main()... in the book he uses void main() which may remove a bit of complexity from the discussion but tickled my tender sensibilities, so I replaced it with an int main() and while I could have left off the return statement I figured that we might as well have all our cards on the table.

The book then spends the rest of the chapter evolving the class World. While the author often deviates from the C++ standards, I really enjoyed this approach to C++ and I think I prefer it over the classic C based model.

There *are* some things I don't like about the example. For example I don't like introducing the global namespace, BUT, I think a good argument can be made for the importance of talking about it first. I also don't like the fact that this example actually crashes on my Borland compiler (not sure why it would but it does). But I find this to be a fascinating take on introducing C++ without teaching C first.

4 Comments On This Entry

Page 1 of 1


27 February 2010 - 11:39 AM
This is an interesting approach! I know that when I first started OOP, I was utterly confused coming from C-style C++ and even C-style Java (somewhat). Sure, this model can be confusing, but if one can explain how it works in the beginning, then I can see this defiantly introducing OOP well.


28 February 2010 - 12:19 PM
I think it's interesting specifically because it shows a C-programmer how a program can execute code that isn't referenced in main(). Once you get your head round the flow of execution, light bulbs of OO start going off.


28 February 2010 - 11:06 PM
C++ actually. Classes are not available in C.


01 March 2010 - 06:19 AM
In C code execution begins almost exclusively in main(), so a C-Programmer would not be used to seeing anything happen if main() contained no code. So it is indeed an interesting example to a C-programmer new to C++. I think moopet's point makes me re-examine my position on using the global variable in a beginner example. Not only does it open the discussion of scope but it also demonstrates some very fundamental differences between C and C++.
Page 1 of 1

May 2019

19 20 2122232425

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)