Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Open Source

A Better C?

The C++ language is a general-purpose programming language that is, except for minor details, a superset of C. It improves on C through its support of data abstraction and object-oriented programming. The main influences on its design, in addition to C, were Simula57 and Algol68 (see references 1 and 2).

C++ was first installed 5 years ago. Today, it has several independent implementations and many thousands of installations. It is being used or major university research projects and for large-scale software development in companies such as Apple, Apollo, AT&T, and Sun.

It has been applied to most branches of programming, including banking, CAD, compiler construction, database management, image processing, graphics, music synthesis, networking, programming environments, robotics, simulation, scientific computation, switching, and very-large-scale-integration design.

A Better C

C++ improves the notational convenience of C and provides greater type safety. It compensates for C's weaknesses without compromising C's strengths. In particular, there is no program that can be written in C but not in C++ , nor is there a program that can be written in C so that it achieves greater run-time efficiency than it does in C++ (see reference 3).

C is clearly not the cleanest language ever designed nor the easiest to use, but it owes its current pervasiveness to several key strengths:

  • Flexibility: You can apply C to almost every application area and use almost every programming technique with it. The language has no inherent limitations that preclude writing particular kinds of programs.
  • Efficiency: C's semantics are "low-level. " That is, its fundamental concepts mirror those of a traditional computer. Consequently, it's relatively easy, both for you and for a compiler, to efficiently use hardware resources for a C program.
  • Availability: Given any computer, from the tiniest microcomputer to the largest supercomputer, chances are that there's an acceptable-quality C compiler available for it, and that such a compiler supports an acceptably complete and standard C language and library. There are also libraries and support tools available, so you rarely need to design a new system from scratch.
  • Portability: While a C program may not be easily or automatically portable from one machine (or operating system) to another, such a port is usually possible. The level of difficulty is also usually low enough that even porting software that contains inherent machine dependencies is both technically and economically feasible.

C++ preserves these strengths and remedies some of C's most obvious problems. For example, function arguments are type-checked in C++, and coercions are applied where they are found to be appropriate:

extern double sqrt (doub1t);
// declare square-root function
double d1 = sqrt (2);
// fine: 2 is converted to 
// a double
double d2 = sqrt ("two");
// error: sqrt*() does not
// accept a string

The // notation was introduced into C++ from BCPL (see reference 4) for comments starting at the // and ending at the end of the line.

As shown, C++ makes you specify a function's argument types in a function declaration so that the standard type conversions (such as int to double) can be implicitly applied, and type errors (such as calling a function requiring a double with a char* argument) can be caught at compile time. With minor restrictions, the draft ANSI C standard accepts the C++ function-calling rules and the syntax for function declarations and function definitions (see reference 5).

C++ provides in-line substitution of functions:

inline int max(int a, int b)
   { return a>b?a:b; }
int x = 7;
int y = 9;
// generates: x>y?x:y
max(f(x),x); // generates:
// temp=f(x); temp>x?temp:x

Unlike the macros commonly used in C, in-line functions obey the usual type and scope rules. Using in-line functions can lead to apparent run-time improvements over C. In-line substitution of functions is especially important in the context of data abstraction and object-oriented programming. With these styles of programming, very small functions are so common that function-call overhead can become a performance bottleneck.

In addition, C++ provides typed and scoped constants, operators for free store (dynamic store) manipulation, and many other features.

When the ANSI C committee finishes its work, the definition of C++ will be reviewed to remove gratuitous incompatibilities. This will not be a major task, though, because C++ and ANSI C have already absorbed most of the "new ANSI C" features from each other.

For example, the notion of a pointer to "raw storage," void*, was incorporated into C++ from ANSI C, as were notational conveniences such as the suffix u indicating an unsigned literal (e.g. , 12u) and hexadecimal character constants (e.g., '\x fa'). However, the most important features of C++ relate to the support of data abstraction and object-oriented programming and are thus outside the scope of ANSI C and unaffected by changes in the draft ANSI C standard.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.