I have just finished teaching "Programming for Engineers" at
Northwestern, my first time teaching a course on the C++ programming
language and pushing me to learn the the many fine points of the
language. Yes, I know this is a theory blog but I'm giving you my
impressions of the language anyway.
C++ was a language written for its time, the 80's, when computers were
slow, memory was expensive and machines, for the most part, didn't
talk to each other that much. The language, an object oriented upgrade
to C, allowed one to write code just above the assembly language
code. It kept memory limited to the point of not storing the sizes of
arrays and allowed users to have pointers directly to memory. It is a
language that, with objects, lets you create whatever you want and
overload operators, allowing you to redefine "+" or
"=" to do whatever you want. It is also a powerful
language, allowing you to build classes based on other classes and,
with the Standard Template Library, gives you access to a set of
highly optimized data structures.
C++ does create incredibly fast code especially when run on today's
processors. On my laptop, looping through a trillion operations
takes only a couple of seconds. Because of the popularity of C++,
there are C++ compilers for every platform, many of which produced
incredibly optimized code. And of course there is a wealth of
information and code libraries written in C++.
That's the good. Now the bad: C++ is a complicated language—I
spent most of the ten week course teaching syntax and still didn't
cover close to the entire language. C++ allows obviously bad
statements, for example if you replace the "==" in
"
if (a == b) d++;
"
with a single "=" then it still compiles and runs but
doesn't do what you wanted. The expression "3^5" outputs 6 and
not 243. To make a class abstract you don't say "abstract"
but just make sure you have at least one pure virtual function. To
make a virtual function pure you add "=0" at the end of its
header definition. There is something very mystical about purifying
something by setting it equal to nothing.
But those are minor complaints. We don't live in the 80's anymore but
in the Internet age which causes two major problems: compatibility and
security. C++ has not standard way to access Internet objects like
XML. In class I showed how Microsoft's XMLTextReader worked but it is
tricky to set up and doesn't port to other systems. Too bad as a
computer that doesn't access the Net hardly seems like a computer
anymore.
Even worse, C++ is a very trusting language, not having many safety
checks and allowing direct access to low-level operations and
memory. The Internet is full of non-trusting people. One can write
safe code in C++, say that doesn't allow buffer overruns, but it is
tricky and a programmer even a little lazy can leave a gaping hole for
hackers to climb through.
Legacy code will keep C++ programmers employed for many years,
especially as we close in to 2038 and we hit the time
limit. But a programmer starting a new project today would do
better in a safer language. Python anyone?