As Stroustrup said, there’s no such programming language as C/C++. C and C++ are two different beasts.

I’m not going to argue here about which is better. For small or embedded projects, the complexities of C++ can get in the way. For larger projects, some of the features of C++ are very useful.

I’m simply going to list the features C++ adds to C, from an ex-C programmer’s point of view (yes, I was one of those people who believed that C++ was just “C with classes”). The idea is to follow the links to further information if you want to know more about any of them.

This is part 1 of 2, the non-OO features:

Default arguments to functions: defining int read(int bufsize =512) means that if you call read() without arguments it’s like you called read(512) .

means that if you call without arguments it’s like you called . Somewhat related is function overloading. You can define a function twice, with two different argument lists. The compiler will use the “closest match” when you call it. For example, defining int print(int n) {...} and int print(const char* s) {...} means you can print() both ints and char strings.

and means you can both ints and char strings. The compiler uses ambiguity resolution to resolve all these overloaded functions.

You can declare local variables wherever in the code you need them, not just at the start of a function (also a C99 feature). This means variables can be declared closer to where you use them. The variable is scoped to the {...} block you put it in.

block you put it in. References, as in int& x . This isn’t some weird Microsoft-only syntax for smart pointers. I started to think of these as pointers without all the * ‘s, but they’re quite different. An alias is another name for the referred-to object, usually used for pass-by-reference — and unlike pointers, you can call say swap(x, y) instead of swap(&x, &y) . A const reference means you’re not allowed to change the object it refers to.

. This isn’t some weird Microsoft-only syntax for smart pointers. I started to think of these as pointers without all the ‘s, but they’re quite different. An alias is another name for the referred-to object, usually used for pass-by-reference — and unlike pointers, you can call say instead of . A reference means you’re not allowed to change the object it refers to. Const-correctness in general. This is not just a C++ thing, but C++ folks tend to be stricter about it (usually for good reason).

A namespace just groups a bunch of names together (functions or variables). C should have had them all along, to avoid everyone having to use their own brand of mylib_myfunc() pseudo-namespaces. See also the using keyword in its various forms.

pseudo-namespaces. See also the keyword in its various forms. The built-in boolean type, bool , with true and false keywords (also in C99 via stdbool.h ).

, with and keywords (also in C99 via ). The inline keyword and associated quirks, including implicit inlining and auto-inlining.

keyword and associated quirks, including implicit inlining and auto-inlining. Most people don’t realise it, but (not f or n == 5) is both valid Python and valid C++, thanks to ISO646-style operator keywords.

is both valid Python and valid C++, thanks to ISO646-style operator keywords. A much larger standard library than C. The Standard Template Library (STL) is definitely worth knowing, especially the container datatypes like vector (growable array) and map (associative array), and the STL algorithms such as find() and sort() . C++’s iostreams are just plain weird (I mean, using the bit-shift operators to print stuff out?), but they’re here to stay. Of course, you can still use all of C’s standard library, and if you #include <cstdio> instead of <stdio.h> , you’ll get the names in the std namespace, as in std::printf() .

(growable array) and (associative array), and the STL algorithms such as and . C++’s iostreams are just plain weird (I mean, using the bit-shift operators to print stuff out?), but they’re here to stay. Of course, you can still use all of C’s standard library, and if you instead of , you’ll get the names in the namespace, as in . Linking C++ code to C code has several gotchas.

Please send your feedback, and let me know if I’ve missed any non-OO features. The following week’s entry contains the second part describing the object-oriented features C++ has added.

3 May 2010 by Ben 14 comments