© Conrad Weisert, Information Disciplines, Inc., June 25, 2009
NOTE: This document may be circulated or quoted from freely, as long as the copyright credit is included..
In introducing C++ Bjarne Stroustrup pointed out that some of the new features would be useful even to programmers who didn't exploit any object-oriented techniques.
At that time, we could still buy a pure C compiler from a mainstream vendor. It cost less and would run on a much smaller machine than the new C++ monsters. A programmer might well prefer some new C++ feature, but had to forgo it to conform to budget or configuration constraints.
All that has changed today. We all have access to affordable C++ compilers and sophisticated development platforms. We all have generous machine configurations that easily run them. So, with all that object-oriented power available, why would anyone want to write a program using only the procedural paradigm?
Integrating code with a legacy application, stuffing it into a tiny embedded processor, or complying with some strange contract are possible, but unlikely reasons. I have a more persuasive and more common reason: to teach introductory programming.
We want the beginning programmer to master procedural programming, but we don't want to burden him or her with unnecessary rules and restrictions. So, the beginning student will use a C++ compiler, but will ignore both
Here are some elements of C1 that I wouldn't mention to the student, along with corresponding features of C++ that require no OOP awareness:
Ugly error-prone C facility | Better C++ facility |
malloc,
free,
and sizeof functions that require
otherwise unnecessary knowledge of internal data representations and invite
cheating.
| new and
delete
operators.
|
| Passing a pointer to allow a function to modify its argument. | Use reference parameter. |
Manipulation of null-terminated string
(array of char) either directly or through
library routines strcpy and
strcat
| See Coping with Character Strings in C (not a C++ facility, just good practice.2) |
printf and
for stream
output, with associated format codes.
| Output-stream insertion operator
<<.3
|
Similarly, scanf and
for stream
input, except where an extremely unusual format must be handled.
| Input-stream extraction
operator >>3.
|
| Preprocessor (macro) definition of a pseudo-generic function | Define an inline function or function template. |
Repeating the word struct
when delaring an instance.
| Just use the name of the
struct.2
|
| Unique names for each type of non-macro generic function | Function name overloading |
With those simplifications, C++ turns out to be a fine language for procedural programming, and is well suited to teaching programming to beginners. Most compilers give excellent error diagnostics, and with suitable care run-time loss of control should be rare.
2—These techniques will also help prepare the student for OOP.
3—Yes, these IO stream facilities depend on classes and objects, but the programmer needn't be aware of them. We can simply present the syntax as another kind of expression statement.
Return to IDI home page
technical articles
C/C++ articles
Last modified July 9, 2009