Gillius's Programming

Addendum to Learning OOP in C++

I wrote most of my tutorials around 3 years ago I did not know as much as I know now, and some of the more experienced programmers may have noticed some areas of my tutorials that are now out-of-date or aren't quite 100% correct.

Unfortunately I have not had the interest needed to go through and rewrite the tutorials -- I enjoy working on GNE and Itana and my other projects much more than writing tutorials now. When chances arise I will attempt to correct problems as I see them.

But I write this addendum to the OOP tutorial to clear up the largest hole in that tutorial. Technically most of the code there is not valid C++ code anymore, now that the new (well... new since I wrote the tutorials) ISO C++ 98 standard has been implemented in all of the current major compilers. I have personally used GCC 3.x, MSVC6, and the Sun CC compilers that comply with the new standard, and I would be surprised if Borland's latest compiler does not support it now.

The ISO C++ 98 Standard -- What's Different?

There are a few major things the new standard does:

Back to Tutorials Page


New C++ programs now all use the concept of namespaces. In C and old C++, there were potential issues with namespace collisions. For example many functions, classes, and variables are declared in C++, and these could cause namespace collisions.

With just the old C++ library, it was difficult to create a new variable called cout and then try to use that with the cout iostream. What if you wanted to create a class called ios, but forgot ios already was a class in the standard C++ library?

Also, if you wanted to use other add-on libraries -- they define their own functions, classes, and variables, and these names might conflict with yours, with no easy way to distinguish between the two competing definitions. C programs with macros espically had this nasty problem and thus many libraries have prefixed their function and variable names with a consistant prefix (for example, a function from a networking library might be called net_connect). This solution isn't the best.

Namespaces are a way for telling the compiler which "context" you want to evaluate the name in. In a way they almost act like C++ classes with all static members. For example you create a new class:

class MyClass {
  static int myVar;
  static void myFunc() {
    myVar = 5;

void main() {
  MyClass::myVar = 10;
namespace MyNamespace {
  int myVar;
  void myFunc() {
    myVar = 5;

void main() {
  MyNamespace::myVar = 10;

As you can see, in order to use myVar and myFunc outside of MyClass, you needed to qualify them using the scope operator, ::. For those with strange fonts, the scope operator is two colons. An equivalent example using namespaces:

The static qualifiers are not needed, and the namespace keyword is used instead of class. There are two very important points not illustrated here that warrant the use of a namespace over a class with only static members:

//An example given the code above:
using namespace MyNamespace;

void myFunc();

void func();
  myVar = 10;

When you import a namespace, you don't need to use the scope qualifier to qualify the name, unless there is a ambiguity. Since myFunc is defined in both the global namespace and in MyNamespace, when we use myFunc we must explicitly pick which one we want to call, but since we didn't define myVar, there is no need to qualify it. Since this is usually the case, using namespaces can save a lot of typing while removing namespace collisions.

Back to top

The New Headers

All of the original C functions and C++ classes are now in the std (short for standard) namespace. The new include files do not have an .h on the end, for example <iostream.h> becomes <iostream>. The include files that are part of the Standard C library also do not have a .h on the end and start with c, for example <string.h> becomes <cstring>. The old header names are deprecated and should not be used anymore, but most compilers keep them for backwards compatability.

Note that your header files could be named anything. They could be with or without the .h, or with any extension you want, but .h and .hpp are the most common.

Compare a pre ISO C++ 98 program to one compling with the latest standard:

#include <iostream.h>
#include <string.h>

void main() {
  cout << "Hello World!" << endl;
  int x = strlen("Hello World!");
#include <iostream>
#include <cstring>

void main() {
  std::cout << "Hello World!" << std::endl;
  int x = std::strlen("Hello World!");

Note that we could have added the statement "using namespace std;" and then we would not have to qualify cout or endl. So your old programs can be converted to the new standard simply by changing the include names and adding in "using namespace std;".

Back to top

Templated iostreams

There have been many changes to the iostreams library. On the basic level like the examples using cout and cin, no changes have been made that are directly visible to the coder, but in fact the iostream classes have greatly changed underneath.

My new tutorial I will be writing shortly uses the new iostream classes and will not work on older compilers like GCC 2.9x. Here is a partial list of changes:

An entire book could be written on using iostreams and what you can do with the new iostreams, so it is best to consult your documentation or freely available online documentation.

Back to top

The Standard Template Library

The Standard Template Library used to be standalone from C++, but it became a de facto standard of doing many operations in C++, and has become part of the Standard C++ Library. Although not offically called the STL anymore, most programmers still reference to it by this name as it is familar to them and it specifically refers to this portion of the Standard C++ Library.

The STL consists of two main parts. A part for storing and manipulating data structures which is similar in functionality to the Java Collections API. The second part consists of very commonly used algoritms such as iteration, sorting, and performing operations on data. There are a few other cool things in the STL such as functors, but I won't get into all of the concepts here. (as for the iostreams library, you could write a whole book just on the STL).

I learned the STL mainly from SGI's site. A view at the table contents gives you a good look at all of the capabilities of this library. I've seen coders do things easily with the STL that I've not seen done in any other language, including Java. It certainly is one VERY impressive set of code.

Back to top