We're always interested in getting feedback. E-mail us if you like
this guide, if you think that important material is omitted, if you
encounter errors in the code examples or in the documentation, if you
find any typos, or generally just if you feel like e-mailing. Mail to
Frank Brokken
or use an
e-mail form.
Please state the concerned document version, found in
the title. If you're interested in a printable
PostScript copy, use the
form, or
better yet,
pick up your own copy by ftp
from ftp.icce.rug.nl/pub/http.
This document is published by the ICCE, State University of Groningen, the Netherlands. This document was typeset using the yodl formatting system.
All rights reserved. No part of this document may be published or changed without prior consent of the authors. Direct all correspondence concerning suggestions, additions, improvements or changes in this document to the first author:
In this chapter a first impression of C++ is presented. A few extensions to C are reviewed and a tip of the mysterious veil surrounding object oriented programming (OOP) is lifted.
C++ was originally a `pre-compiler', similar to the preprocessor of
C, which converted special constructions in its source code to plain
C. This code was then compiled by a normal C compiler. The
`pre-code', which was read by the C++ pre-compiler, was usually located
in a file with the extension .cc
, .C
or .cpp
. This file
would then be converted to a C source file with the extension .c
, which
was compiled and linked.
The nomenclature of C++ source files remains: the extensions .cc
and
.cpp
are usually still used. However, the preliminary work of a C++
pre-compiler is in modern compilers usually included in the actual compilation
process. Often compilers will determine the type of a source file by the
extension. This holds true for Borland's and Microsoft's C++ compilers,
which assume a C++ source for an extension .cpp
. The GNU compiler
gcc
, which is available on many Unix platforms, assumes for C++ the
extension .cc
.
The fact that C++ used to be compiled into C code is also visible
from the fact that C++ is a superset of C: C++ offers all
possibilities of C, and more. This makes the transition from C to
C++ quite easy. Programmers who are familiar with C may start
`programming in C++' by using source files with an extension .cc
or
.cpp
instead of .c
, and can then just comfortably slide into all the
possibilities that C++ offers. No abrupt change of habits is required.
.cc
and
run it through a C++ compiler:
sizeof('c')
equals sizeof(int)
,
'c'
being any ASCII character. The underlying philosophy is
probably that char
's, when passed as arguments to functions, are
passed as integers anyway. Furthermore, the C compiler handles a
character constant like 'c'
as an integer constant. Hence, in
C, the function calls
and
are synonyms.
In contrast, in C++, sizeof('c')
is always 1, while
an int
is still an int
. As we shall see later (see
section 2.4.9), two function calls
and
are quite separate functions: C++ discriminates functions by
their arguments, which are different in these two calls: one function
requires an int
while the other one requires a char
.
means in C that a function func()
exists, which returns
no value. However, in C, the declaration doesn't specify which
arguments (if any) the function takes.
In contrast, such a declaration in C++ means that the
function func()
takes no arguments at all.
Often it is said that programming in C++ leads to `better' programs. Some of the claimed advantages of C++ are:
Which of these allegations are true? In our opinion, C++ is a little overrated; in general this holds true for the entire object-oriented programming (OOP). The enthusiasm around C++ resembles somewhat the former allegations about Artificial-Intelligence (AI) languages like Lisp and Prolog: these languages were supposed to solve the most difficult AI-problems `almost without effort'. Obviously, too promising stories about any programming language must be overdone; in the end, each problem can be coded in any programming language (even BASIC or assembly language). The advantages or disadvantages of a given programming language aren't in `what you can do with them', but rather in `which tools the language offers to make the job easier'.
Concerning the above allegations of C++, we think that the following can be concluded. The development of new programs while existing code is reused can also be realized in C by, e.g., using function libraries: thus, handy functions can be collected in a library and need not be re-invented with each new program. Still, C++ offers its specific syntax possibilities for code reuse, apart from function libraries (see chapter 10).
Creating and using new data types is also very well possible in C; e.g.,
by using struct
s, typedef
s etc.. From these types other types can be
derived, thus leading to struct
s containing struct
s and so on.
Memory management is in principle in C++ as easy or as difficult as in
C. Especially when dedicated C functions such as xmalloc()
and
xrealloc()
are used (these functions are often present in `our'
programs, they allocate or abort the program when the memory pool is
exhausted). In short, memory management in C or in
C++ can be coded `elegantly', `ugly' or anything in between --
this depends on the developer rather than on the language.
Concerning `bug proneness' we can say that C++ indeed uses stricter type checking than C. However, most modern C compilers implement `warning levels'; it is then the programmer's choice to disregard or heed a generated warning. In C++ many of such warnings become fatal errors (the compilation stops).
As far as `data hiding' is concerned, C does offer some tools. E.g.,
where possible, local or static
variables can be used and special data
types such as struct
s can be manipulated by dedicated functions. Using
such techniques, data hiding can be realized even in C; though it needs
to be said that C++ offers special syntactical constructions. In
contrast, programmers who prefer to use a global variable int
i
for
each counter variable will quite likely not benefit from the concept of data
hiding, be it in C or C++.
Concluding, C++ in particular and OOP in general are not solutions to all programming problems. C++ however does offer some elegant syntactical possibilities which are worth-while investigating.
static
).
In contrast, or maybe better: in addition to this, an object-oriented approach identifies the keywords in the problem. These keywords are then depicted in a diagram and arrows are drawn between these keywords to define an internal hierarchy. The keywords will be the objects in the implementation and the hierarchy defines the relationship between these objects. The term object is used here to describe a limited, well-defined structure, containing all information about some entity: data types and functions to manipulate the data.
As an example of an object-oriented approach, an illustration follows:
The employees and owner of a car dealer and auto garage company are paid
as follows. First, mechanics who work in the garage are paid a certain sum
each month. Second, the owner of the company receives a fixed amount each
month. Third, there are car salesmen who work in the showroom and receive
their salary each month plus a bonus per sold car. Finally, the company
employs second-hand car purchasers who travel around; these employees
receive their monthly salary, a bonus per bought car, and a restitution of
their travel expenses.
When representing the above salary administration, the keywords could be mechanics, owner, salesmen and purchasers. The properties of such units are: a monthly salary, sometimes a bonus per purchase or sale, and sometimes restitution of travel expenses. When analyzing the problem in this manner we arrive at the following representation:
In the hierarchy of objects we would define the dependency between the first two objects by letting the car salesmen be `derived' from the owner and mechanics.
The hierarchy of the thus identified objects further illustrated in figure 1.
//
and ends with the
end-of-line marker. The standard C comment, delimited by /*
and
*/
can still be used in C++:
The end-of-line comment was already implemented as an extension to C in some C compilers, such as the Microsoft C Compiler V5.
0
. In C, where
pointers are concerned, NULL
is often used. This difference is purely
stylistic, though one that is widely adopted. In C++ there's no need
anymore to use NULL
. Indeed, according to the descriptions of the
the pointer-returning operator new
0 rather than NULL
is returned when
memory allocation fails.
The program
does often compile under C, though with a warning that printf()
is
not a known function. Many C++ compilers will fail to produce code in
such a situation (When GNU's g++ compiler encounters an unknown
function, it assumes that an `ordinary' C function is meant. It does complain
however.). The error is of course the missing #include<stdio.h>
directive.
means in C that the argument list of the declared function is not
prototyped: the compiler will not be able to warn against improper argument
usage. When declaring a function in C which has no arguments, the keyword
void
is used, as in:
Because C++ maintains strict type checking, an empty argument list is
interpreted as the absence of any parameter. The keyword void
can then be
left out. In C++ the above two declarations are equivalent.
__cplusplus
: it is as if each source file were prefixed with the
preprocessor directive #define __cplusplus
.
We shall see examples of the usage of this symbol in the following sections.
As an example, the following code fragment declares a function xmalloc()
which is a C function:
This declaration is analogous to a declaration in C, except that the
prototype is prefixed with extern "C"
.
A slightly different way to declare C functions is the following:
It is also possible to place preprocessor directives at the location of the
declarations. E.g., a C header file myheader.h
which declares
C functions can be included in a C++ source file as follows:
The above presented methods can be used without problem, but are not very current. A more frequently used method to declare external C functions is presented below.
__cplusplus
and of the
possibility to define extern "C"
functions offers the ability to
create header files for both C and C++. Such a header file might,
e.g., declare a group of functions which are to be used in both C and
C++ programs.
The setup of such a header file is as follows:
Using this setup, a normal C header file is enclosed by extern
"C" {
which occurs at the start of the file and by }
, which
occurs at the end of the file. The #ifdef
directives test for the type of
the compilation: C or C++. The `standard' header files, such as
stdio.h
, are built in this manner and therefore usable for both C
and C++.
An extra addition which is often seen is the following. Usually it is
desirable to avoid multiple inclusions of the same header file. This can
easily be achieved by including an #ifndef
directive in the header file.
An example of a file myheader.h
would then be:
When this file is scanned for the first time by the preprocessor, the
symbol _MYHEADER_H_
is not yet defined. The #ifndef
condition
succeeds and all declarations are scanned. In addition, the symbol
_MYHEADER_H_
is defined.
When this file is scanned for a second time during the same compilation,
the symbol _MYHEADER_H_
is defined. All information between the
#ifndef
and #endif
directives is skipped.
The symbol name _MYHEADER_H_
serves in this context only for recognition
purposes. E.g., the name of the header file can be used for this purpose, in
capitals, with an underscore character instead of a dot.
Furthermore local variables can be defined in some statements, just prior to
their usage. A typical example is the for
statement:
In this code fragment the variable i
is created inside the for
statement. According to the ANSI-standard, the variable does not exist
prior to the for
-statement and not beyond the for
-statement.
With some compilers, the variable continues to exist after the execution of
the for
-statement, but a warning like
warning: name lookup of `i' changed for new ANSI `for' scoping using obsolete binding at `i'will be issued when the variable is used outside of the
for
-loop. The
implication seems clear: define a variable just before the for
-statement
if it's to be used beyond that statement, otherwise the variable can be
defined at the for
-statement itself.
Defining local variables when they're needed requires a little getting used to. However, eventually it tends to produce more readable code than defining variables at the beginning of compound statements. We suggest the following rules of thumb for defining local variables:
{
,
for
-statement, but
also all situations where a variable is only needed, say, half-way through
the function.
::
is described first. This operator can be
used in situations where a global variable exists with the same name as a
local variable:
In this code fragment the scope operator is used to address a global variable instead of the local variable with the same name. The usage of the scope operator is more extensive than just this, but the other purposes will be described later.
In the above fragment three functions show()
are defined, which only
differ in their argument lists: int
, double
and char *
. The
functions have the same name. The definition of several functions with the
same name is called `function overloading'.
It is interesting that the way in which the C++ compiler implements
function overloading is quite simple. Although the functions share the same
name in the source text (in this example show()
), the compiler --and
hence the linker-- use quite different names. The conversion of a name in the
source file to an internally used name is called `name mangling'. E.g., the
C++ compiler might convert the name void
show
(int)
to the
internal name VshowI
, while an analogous function with a char*
argument might be called VshowCP
. The actual names which are internally
used depend on the compiler and are not relevant for the programmer, except
where these names show up in e.g., a listing of the contents of a library.
A few remarks concerning function overloading are:
show()
are still somewhat related (they print information to the
screen).
However, it is also quite possible to define two functions
lookup()
, one of which would find a name in a list while the other
would determine the video mode. In this case the two functions have
nothing in common except for their name. It would therefore be more
practical to use names which suggest the action; say, findname()
and
getvidmode()
.
printf()
(The return value is, by the way, an integer which
states the number of printed characters. This return value is practically
never inspected.). Two functions printf()
which would only
differ in their return type could therefore not be distinguished by the
compiler.
given the three functions show()
above. The zero could be
interpreted here as a NULL
pointer to a char
, i.e., a
(char *)0
, or as an integer with the value zero. C++ will
choose to call the function expecting an integer argument, which might not
be what one expects.
An example is shown below:
The possibility to omit arguments in situations where default arguments are defined is just a nice touch: the compiler will supply the missing argument when not specified. The code of the program becomes by no means shorter or more efficient.
Functions may be defined with more than one default argument:
When the function two_ints()
is called, the compiler supplies one or two
arguments when necessary. A statement as two_ints(,6)
is however
not allowed: when arguments are omitted they must be on the right-hand side.
Default arguments must be known to the compiler when the code is generated where the arguments may have to be supplied. Often this means that the default arguments are present in a header file:
Note that supplying the default arguments in the function definition instead of in the header file would not be the correct approach.
typedef
is in C++ allowed, but no longer necessary when
it is used as a prefix in union
, struct
or enum
definitions.
This is illustrated in the following example:
When a struct
, union
or other compound type is defined, the tag of
this type can be used as type name (this is somestruct
in the above
example):
struct
. This
is the first concrete example of the definition of an object: as was described
previously (see section 2.2), an object is a structure containing
all involved code and data.
A definition of a struct point
is given in the code fragment below.
In this structure, two int
data fields and one function draw()
are
declared.
A similar structure could be part of a painting program and could, e.g.,
represent a pixel in the drawing. Concerning this struct
it should be
noted that:
draw()
which occurs in the struct
definition
is only a declaration. The actual code of the function, or in other
words the actions which the function should perform, are located
elsewhere: in the code section of the program, where all code is
collected. We will describe the actual definitions of functions inside
struct
s later (see section 3.1.3).
struct
point
is just two int
s. Even
though a function is declared in the structure, its size is not affected
by this. The compiler implements this behavior by allowing the function
draw()
to be known only in the context of a point
.
The point
structure could be used as follows:
The function which is part of the structure is selected in a similar manner in
which data fields are selected; i.e., using the field selector operator
(.
). When pointers to struct
s are used, ->
can be used.
The idea of this syntactical construction is that several types may contain
functions with the same name. E.g., a structure representing a circle might
contain three int
values: two values for the coordinates of the center of
the circle and one value for the radius. Analogously to the point
structure, a function draw()
could be declared which would draw the
circle.