C
C
C
Anne 3
Priode 2
INF585
dition 2012
Programmation en C++
Lo Liberti
Programmes dapprofondissement
Bio-Informatique,
Mathmatiques appliques
LIX,
Ecole Polytechnique
C++ Notes
Leo Liberti
Last update: December 12, 2011
Lecture Notes C++ Notes L. Liberti
Contents
1 Introduction 5
2 Generalities 5
2.1 Denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 The operating system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5 Program execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 The Unix shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Basic C++ 10
3.1 Types, objects, variables and pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2 Preprocessing directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.3 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.4 Memory allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.5 Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.6 C++ Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.6.1 Declarations, assignments, tests, arithmetic/logical operations . . . . . . . . . . . . 15
3.6.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.7.1 Argument passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.7.2 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.8 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.8.1 Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.9 Human-side C++ syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.9.1 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.9.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.10 Structure of a C++ program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.11 The building process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.11.1 Compilation and linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.11.2 File types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
CONTENTS 2
Lecture Notes C++ Notes L. Liberti
3.11.3 Object les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.11.4 Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.11.5 Packaging and distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4 Classes 21
4.1 Basic class semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.1.1 Classes: motivations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.1.2 The class concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1.3 Objects of a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1.4 Referring to the current object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1.5 Constructors and destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1.6 Lifetime of an object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.7 Data access privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.8 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.9 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.1.10 Overloading operators in and out of classes . . . . . . . . . . . . . . . . . . . . . . 25
4.1.11 The stack and the heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.1.12 User-dened memory allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.1.13 Using object pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.2 Input and output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2.1 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2.2 Object onto streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2.3 Overloading the << and >> operators . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3 Inheritance and polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3.2 Nested inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3.3 Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.3.4 Nested inheritance and hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.3.5 Inheritance vs. embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.3.6 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.3.7 Pure virtual classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.3.8 Pure virtual classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
CONTENTS 3
Lecture Notes C++ Notes L. Liberti
5 Templates 31
5.1 User-dened templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.1.1 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.1.2 Internals and warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.2 Standard Template Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.2.1 The STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.2.2 vector example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.2.3 map example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
CONTENTS 4
Lecture Notes C++ Notes L. Liberti
1 Introduction
This is a very brief introduction to the C++ programming language, to be used in courses held at the
Departement dInformatique and the Departement de Mathematiques Appliquees of the
Ecole Polytech-
nique. The aim of the course is to teach the basics of the C++ language in a practical, hands-on way,
without many of the more technical details concerning the more advanced topics. These notes should be
paired with the associated presentation slides:
http://www.lix.polytechnique.fr/
~
liberti/teaching/c++/online/course.pdf
http://www.lix.polytechnique.fr/
~
liberti/teaching/c++/online/course-javausers.pdf
but most importantly with the exercise book (http://www.lix.polytechnique.fr/
~
liberti/teaching/
c++/online/exercises), where most of the material is analyzed in depth. All the didactical content can
be found online at http://www.lix.polytechnique.fr/
~
liberti/teaching/c++/online.
The main sources for these notes are:
1. Bjarne Stroustrup, The C++ Programming Language, 3rd edition, Addison-Wesley, Reading (MA),
1999
2. Stephen Dewhurst, C++ Gotchas: Avoiding common problems in coding and design, Addison-
Wesley, Reading (MA), 2002
3. Herbert Schildt, C/C++ Programmers Reference, 2nd edition, Osborne McGraw-Hill, Berkeley
(CA).
2 Generalities
This C++ course is based on the GNU C++ compiler v. 4.0 and above running under the Unix operating
system. Essential Unix tools, beside the compiler, are: make, tar, gzip/gunzip. As debuggers, we use
gdb with the ddd front-end, and valgrind.
By computer, here, we do not mean a theoretical computer science model such as a Turing machine,
but a real, physical desktop or laptop computer. Although any architecture capable of running a Unix
and the above-mentioned tools will do, the actual C++ course is given on desktop PCs with a 32 bit
Intel processor running a mainstream Linux distribution.
2.1 Denitions
The CPU (Central Processing Unit) of a computer is where all the logical and arithmetic tests, loops
and decisions take place, and where control commands and data exchanges are issued to devices such as
memory, disk, screen, etc. The behaviour of the CPU is determined by its state, which is described by
the content of all its registers and internal memory caches. Letting S be the set of possible CPU states,
the CPU acts like a deterministic function f : S S. According to this model, to each state s S there
corresponds a next state s
= f(s). The rate at which the CPU changes state is governed by the system
clock (usual rates are between 1 and 3 GHz). Thus, around every billionth of a second, the CPU changes
its state.
The form of the function f obviously depends on the CPU make and model. CPUs usually contain
some extremely fast but very small memory chunks called registers which are specically designed to
store either values or memory addresses. The state of the CPU at each clock tick is then determined
Generalities 5
Lecture Notes C++ Notes L. Liberti
by the values contained in each of its registers. The CPU is designed in such a way that at each clock
tick the memory address contained in a certain register will automatically be incremented, and the value
contained at the new address is read and interpreted as a machine code instruction. This allows us
to interpret the function f in a dierent way: we can consider the next state s
= p(i, s) is the value found at the address CS:IP. More information can
be found at http://www.ee.hacettepe.edu.tr/
~
alkar/ELE414/ and http://ourworld.compuserve.
com/homepages/r_harvey/doc_cpu.htm.
Consider now the following (informal) denitions:
Program: set of instructions that can be interpreted by a computer
Instructions: well-formed sequences of characters (syntax)
Interpretation: sequence of operations performed by the computer hardware (semantics)
Programming language: set of rules used to form valid instructions
Algorithm: a program which terminates (though sometimes nd non-terminating algorithm with
abuse of notation)
The well-formedness of the sequence of characters in each instruction corresponds to the C++ syntax
which is one of the subjects of these notes, and will therefore be explained in more detail later. The same
holds for the semantics of each C++ program.
1
We use the terms simple and complex here with their natural English language meanings. In CPU-related technical
language, simple instruction set and complex instruction set have a very specic meaning (look for RISC and CISC
on Google for more information on this point).
Generalities 6
Lecture Notes C++ Notes L. Liberti
2.2 Data
Computers can only perform arithmetic and logic operations in the eld F
2
= {0, 1}. A set of 8 bits is
called a byte. The 8086 and 80286 had a 16-bit bus (the bus is the data transfer capacity per clock tick);
modern Intel-based processors have 32- or 64-bit wide buses. An n-bit wide memory chunk can hold 2
n
dierent values, which are usually either from 2
n1
to 2
n1
1 or from 0 to 2
n
1. An integer in
C/C++ on an Intel architecture is usually stored in 32 bits (=4 bytes). Floating point values are stored
(usually in 8 bytes) according to a specic encoding. Any type of data that can be represented by an
encoding of nite-sized integers can be stored in a computer memory. In C/C++ data are categorized
according to their types. Data types can be elementary: bool (C++ only), char, short, int, long,
float, double or user-dened: union, struct, class (C++ only). A user-dened data type is a nite
sequence of existing data types and occupies either the sum (struct, class) or the maximum (union)
of the sizes of the sequence elements. Memory addresses, by contrast, normally occupy a xed amount
of storage (32 bits in 32-bit CPUs).
2.3 Memory
Computers rely on dierent types of memory to store data (this means: values and addresses). Read-Only
Memory (ROM) can only be read. Random-Access Memory (RAM) can be read and written to. Other
types of memory include EEPROMs, Flash RAM, disks, CD-ROMs, tapes and so on. Within the scope
of these notes, we shall only be concerned with RAM type memory. This is very fast (in modern PCs
RAM chips are refreshed at frequencies that are around one tenth as that of the CPU), comparatively
expensive, and loses information if the power is switched o. The model of memory that best ts these
C++ notes is that of a linear array, as depicted in Fig. 1.
...
a
1
a
2
a
3
a
4
a
5
a
6
a
7
a
8
v
1
v
2
v
3
v
4
v
5
v
6
v
7
v
8
Figure 1: A linear array model of memory: the i-th box has address a
i
and contains value v
i
.
RAM can be seen as a long (nite) list of boxes; the i-th box in the list is addressed by a value a
i
and contains a value v
i
. The size of an address is usually the same as the processor bus (normally 32 or
64 bit). The size of the value, on the other hand, can be anything depending on the type of value being
stored.
2.4 The operating system
An operating system is a set of running programs that handle the interactions between the computer (in
all its parts) and its users. The operating system assigns memory to each program and protects each
assigned memory segment from unauthorized access (so that a malfunctioning program cannot bring the
whole computer to a halt); it shares the CPU times amongst all the running processes; it operates all
data exchanges with external devices (keybord, screen, network, printers, disks). Operating systems have
three main parts.
The user interface, or shell. It can be command-line driven or a Graphical User Interface (GUI).
The kernel, containing the core functionalities.
Generalities 7
Lecture Notes C++ Notes L. Liberti
The device drivers, a set of programs each of which handles the data exchange with a particular
device.
A process is a program being run. In practice, the computer keeps track of the machine code instruction
being executed for each running process. The operating system kernel stores information on currently
running processes on its process table (this lists for example the memory segments reserved for and by the
process, the amount of CPU time used by the process and so on). Processes may in turn be segmented
into threads, which can be seen as separate processes which do not have a separate entry on the process
table.
Any time a C++ program is compiled and linked, an executable le consisting of the equivalent
machine code instructions is created. Upon launching this executable, the operating system creates a
new corresponding process in the process table, and then starts executing the machine code instructions
during the CPU time slices allocated to it. By calling the operating system function fork(), a C/C++
program can create a new process in the process table with a copy of itself (this means that at the
clock tick immediately after the fork call has nished, the two processes are indistinguishable, save for
a dierent process ID and dierent allocated CPU time slices later on, conditional instruction usually
dierentiates the two processes: this is the standard way of creating new processes from within a C/C++
program). Threads can be dealt with by linking against special libraries, but this is outside the scope of
this document.
2.5 Program execution
When
2
a program is loaded into memory, it is organized into three areas of memory, called segments:
the text segment, stack segment, and heap segment. The text segment (sometimes also called the code
segment) is where the compiled code of the program itself resides. This is the machine language repre-
sentation of the program steps to be carried out, including all functions making up the program, both
user dened and system.
The remaining two areas of system memory is where storage may be allocated by the compiler for
data storage. The stack is where memory is allocated for variables within functions. A stack is a Last In
First Out (LIFO) storage device where new storage is allocated and deallocated at only one end (the
top of the stack).
Every C/C++ program begins executing at a function called main(): space is allocated on the stack
for all variables declared within main(). If main() calls a function, say myFunction(), additional storage
is allocated for the variables in myFunction() at the top of the stack. Notice that the parameters passed
by main() to myFunction() are also stored on the stack. If myFunction() were to call any additional
functions, storage would be allocated at the new top of stack. When myFunction() returns, storage for
its local variables is deallocated, and the top of the stack is resumed at the old position. As can be seen,
the memory allocated in the stack area is used and reused during program execution. It should be clear
that memory allocated in this area will contain garbage values left over from previous usage.
The heap segment provides more stable storage of data for a program; memory allocated in the
heap remains in existence for the duration of a program. Manually allocated memory, global and static
variables are allocated on the heap.
On many architectures, including Intel-based PCs, the relative order of text and stack segment is such
that the text segment is placed after the stack segment. This means that if one uses a variable stored on
the stack to hold an array of bytes longer than the stack itself, one ends up overwriting parts of the text
segment. Since the text segment contains the executable machine code instructions, it is theoretically
possible to change the behaviour of the computer by simply entering some meaningful data in a variable.
2
Parts of this section are taken from http://www-ee.eng.hawaii.edu/
~
tep/EE160/Book/chap14/subsection2.1.1.8.
html.
Generalities 8
Lecture Notes C++ Notes L. Liberti
This was the technique used by some of the most spectacular network hacks of the eighties and nineties
(see http://insecure.org/stf/smashstack.html).
The death of a process can occur by explicit program termination (returning from the main() func-
tion, use of the exit() system function) or by an operating system signal occurring because of some
extraordinary condition. The most common are user intervention (SIGINT, SIGKILL) and runtime errors
(SIGSEGV segmentation violation when the process tries to read or write memory that does not
belong to it). When the process terminates, it is removed from the process table along with all the
memory it occupies, and its ID becomes available for new processes.
2.6 The Unix shell
There are many types of shells in Unix. The most popular are bash and tcsh. Personally, I prefer the
rst one, but the
Ecole Polytechnique installs the second by default in the didactical computer rooms. In
any case one can start either of them by simply opening a terminal and typing the corresponding name.
These notes are based on the bash shell, but most notions will work on either.
Basic commands:
cd directoryName : change working directory
pwd : print the working directory
cat leName : display the (text) le leName to standard output
mv le position : move le to a new position: e.g. mv /etc/hosts . moves the le hosts from the
directory /etc to the current working directory (.)
cp le position : same as mv, but copy the le
rm le : remove le
rmdir directory : remove an empty directory
grep string le(s) : look for a string in a set of les: e.g. grep -Hi complex * looks in all les in
the current directory (*) for the string complex ignoring upper/lower case (-i) and displays the
name of the le (-H) as well as the line where the match occurs.
Most Unix commands can be chained: the output of a command is read as the input of the next.
By default, unix tools send their output messages to the standard output stream (stdout) and their
error messages to the standard error stream (stderr)
Both streams can be redirected. E.g., to redirect both stdout and stderr, use:
sh -c command options arguments > outFile 2>&1
The output stream of a command can become the input stream of the next command in a chain:
e.g. nd | grep \.cxx nds all les with extension .cxx in all subdirectories of the home direc-
tory; the rst command (find) sends a recursive list across subdirectories of the home directory
(denoted by ) to stdout. This stream is transformed by the pipe character (|) in the standard
input (stdin) stream of the following command (grep), which lters out all lines not containing
.cxx.
Generalities 9
Lecture Notes C++ Notes L. Liberti
3 Basic C++
A C++ code consists of symbols denoting types, variables and functions linked by logical, arithmetical,
conditional and repetitional operations.
3.1 Types, objects, variables and pointers
The main C++ program entities are types and objects. As remarked in Sect. 2.2, types can be elementary
or user-dened. Each type describes a category of dierent values, also called objects. In practice, a type
is a record dening the size and memory layout of the objects of that type. Objects are values belonging
to an existing type that reside in a well-dened memory area. Consider the two objects:
float myFloat;
int myInt;
The myFloat object is of type float. In most architectures, this occupies 4 bytes of RAM. myInt is
an object of type int that also occupies 4 bytes. This means that both objects could be stored at the
very same memory location; yet, the fact that their type is dierent makes it impossible to misinterpret
myFloat as an integer or myInt as a oat.
A variable is a symbol that is assigned a type and a memory area of the correct size. Although an
object is usually attached to a variable, it need not be so: one could manually create an object in memory
and then delete the variable without freeing the memory. The object still resides in memory but is not
attached to any variable. A pointer is a particular type of variable, whose assigned memory contains
an address pointing to another memory area where the actual object is kept. Since pointers allow user
access to an arbitrary part of the memory, they wield an unlimited control over the machine. Use with
care.
In Fig. 2, a new type class MyClass is rst declared as consisting of a char and an int. An object
myObject of type MyClass is dened, and its components myChar and myInt are assigned values x and
1 respectively. A pointer ptrObject of type MyClass* is then simultaneously declared and dened, and
assigned the address where the object myObject is stored.
x 0 0 ...
...
0 1 2
0
4 3 5
1
6
class MyClass {
char myChar;
int myInt;
};
MyClass myObject;
myObject.myChar = x;
myObject.myInt = 1;
MyClass* ptrObject = &myObject;
myObject
myInt
MyClass type: 1 byte of char, 4 bytes of int
myChar
ptrObject contains address "1"
Figure 2: Types, objects, variables and pointers.
3.2 Preprocessing directives
A C++ program consists of several types of preprocessing directives and statements. Preprocessing
directives are interpreted before compilation, must be limited to one line of text, are all prepended by
Basic C++ 10
Lecture Notes C++ Notes L. Liberti
a hash character # and do not necessarily terminate with a semicolon character ;. The purpose of
preprocessing directives is mostly to enable conditional compilation and include other source les in the
current compilation. Some examples:
#define MYCONSTANT 10
#include<iostream>
#include "myheader.h"
#ifdef DEBUG
const char version[] = "Debug";
#else
const char version[] = "Release";
#endif
The rst line denes a constant called MYCONSTANT to take the value 10. Each time the string MY-
CONSTANT appears in the rest of the program being compiled, it will be replaced by the string 10.
The purpose of isolating constants to the beginning of the source le, where all the #defines are usually
kept, is to avoid having to hunt for constants within the whole le each time the constant is changed.
Although #defines are very popular in the C language, in C++ one usually uses equivalent constructs
which are not preprocessing directives, such as
const int MYCONSTANT = 10;
which is a normal C++ statement. The second line instructs the compiler to look for a le called iostream
within the compilers include search path (i.e. a predetermined list of paths within the lesystem where
a lot of include les are stored; typically, on Unix systems, this is /usr/include:/usr/local/include),
read it, and compile it as if it was part of the source code being compiled. The third line is similar to
the second but the slightly dierent syntax indicates that the le myheader.h is a user-created le and
probably resides in the current directory. Lines 4-8 are a conditional preprocessing directive, instructing
the compiler to dene the character array version dierently according as to whether the preprocessing
symbol DEBUG was dened or not when the compiler was called. It is possible to dene such symbols
either explicitly, by including a preprocessor directive #define DEBUG before the conditional statement,
or implicitly by launching the compiler from the shell with the -DDEBUG option.
3.3 Statements
The actual C++ program resides in the statements. Each statement can span multiple lines of texts.
All statements are terminated by a semicolon character ;. A sequence of statements within braces {,
} is a block. The scope of an instruction is the extent of the block it belongs to. Statements and block
can be part of a declaration or a denition. Declarations specify a syntax, whereas denitions specify a
semantics.
A declaration is used to create a new type or make the calling syntax of a function explicit. Examples:
class MyClass {
char myChar;
int myInt;
};
denes a new class called MyClass.
int myFunction(int myArgument);
Basic C++ 11
Lecture Notes C++ Notes L. Liberti
denes calling syntax (also called prototype) of a function called myFunction, which is passed an int and
returns an int. Declarations do not use memory and do not generate code; rather, they instruct the
compiler on how handle variable symbols appearing subsequently in the code and how to issue function
calls. Declaration blocks are terminated by a semicolon.
A denition species the meaning attached to a symbol. For a computer, the meaning of a symbol is
given by what happens to the computer when the code relating to that particular symbol is executed. In
other words, virtually all C++ code is part of a denition. For example:
MyClass* myPointer = &myObject;
is simultaneously a declaration and a denition. It is a declaration insofar as myPointer is a symbol
being declared a pointer of type MyClass*. It is a denition because some memory of the correct size
(i.e. enough to hold an address in memory, which on 32 bit architectures would be 4 bytes) is set aside
and assigned to the symbol myPointer, and because that memory is lled with the address of memory
where the object myObject is stored (see Fig. 2). As another example,
int myFunction(int myArgument) {
int ret = myArgument * myArgument;
return ret;
}
is a denition of the semantics of the function myFunction: it takes an integer argument, squares it, and
returns it.
3.4 Memory allocation
The allocation of memory (i.e. obtaining some memory from the operating system) can be automatic or
manual. Automatic allocation is done transparently by the program; in this case, deallocation (giving the
memory back to the operating system) is automatic too, and occurs at the end of the scope. Automatic
allocation always occurs on the stack. Manual allocation is done explicitly by the user, and deallocation
must be done manually too. Manual allocation always occurs on the heap. Variables are always allocated
automatically on the stack. The memory pointed to by pointers, however, is manually assigned to each
pointer and may be manually allocated on the heap.
Examples.
1. The following code declares that myChar is a variable of type char, reserves 1 byte on the stack,
and lls that byte with the 8-bit number 120 (the ASCII code for x). The allocated stack byte is
deallocated (released) at the closing brace.
{
...
char myChar = x;
...
}
2. The following code declares that ptrChar is a pointer of type char*, reserves 4 bytes on the stack,
and lls those bytes with the 32-bit address of the memory where the value corresponding to the
myChar variable is kept. The allocated stack space is released at the closing brace.
{
Basic C++ 12
Lecture Notes C++ Notes L. Liberti
...
char* ptrChar = &myChar;
...
}
3. The rst line of the following code declares that ptrDouble is a pointer of type double* and reserves
4 bytes on the stack (enough for a 32-bit memory address). The second line obtains 8 bytes of heap
memory (enough to contain a double) and assigns its 32-bit address to the pointer ptrDouble. The
third line writes the oating point number 0.2 into the heap memory area whose address is stored
in ptrDouble. Notice that in order to actually use manually allocated heap memory, one needs to
allocate some stack space to hold the address. The deallocation of the stack space occurs at the
closing brace, but the deallocation of the heap space does not occur. In fact, this is an example
of memory leak: we allocate some heap space, assign the address to a pointer, but the pointer is
deallocated (and the address lost) before the heap space is deallocated; at this point, there is no
way to deallocate the heap space, apart from the operating system automatically reclaiming the
memory at the end of the execution.
{ // 1
double* ptrDouble; // 2
ptrDouble = new double; // 3
*ptrDouble = 0.2; // 4
} // 5
4. This is like the example above, but revised so that memory leak does not occur. In line 2, a pointer
ptrOther of type double is declared, and 4 bytes are allocated on the stack for holding a 32-bit
address. Lines 4-6 are as above. In line 7 we copy the 32-bit address held in ptrDouble to ptrOther
(this means that the heap memory address where the number 0.2 is stored is held in two dierent
pointers). At line 8, as above, the stack space for ptrDouble is released and the address within it
is lost. However, since ptrOther was allocated outside the scope being closed at line 8, ptrOther
still exists after line 8: since ptrOther contains the address for the heap memory allocated in line
5, we can issue a manual delete command to release the heap memory, at line 9. Although the
heap memory is released, the stack memory used for storing the ptrOther pointer is not: this is
released at line 10 (the end of the scope).
{ // 1
double* ptrOther; // 2
{ // 3
double* ptrDouble; // 4
ptrDouble = new double; // 5
*ptrDouble = 0.2; // 6
ptrOther = ptrDouble; // 7
} // 8
delete ptrOther; // 9
} // 10
3.5 Bugs
When programming, it is virtually impossible not to make mistakes. Programming errors are called
bugs. The etymology of the term bug is discussed in http://en.wikipedia.org/wiki/Computer_bug.
Some of the bugs concern the syntax, and can be caught by the compiler during compilation (i.e. at
compile-time). Others change the semantics of the program but do not invalidate the syntax. These bugs
only manifest themselves during execution (i.e. at run-time) and are the hardest to trace.
Compile-time bugs are easy to detect (the program does not compile) but it may be hard to pinpoint
the line of code where the bug actually is: although compile-time bugs invalidate the language syntax,
Basic C++ 13
Lecture Notes C++ Notes L. Liberti
they may not invalidate the syntax at the line where they appear. For example, inserting a spurious
open brace { on a line by itself is often perfectly legal. It is only at the end of the le that the compiler
detects a mismatched number of brackets, and may not be able to trace the even to the spurious open
brace. Compiler error messages often refer to the implications of the bug rather than the bug itself, so
may be next to useless (or rather, they must be interpreted in the light of experience).
The run-time bugs that are hardest to trace are those that manifest themselves rarely and in nonde-
terministic ways. Since the computer is a deterministic machine, this sentence needs an explanation. One
source of nondeterminism, for example, is given by the values stored in unused memory. Since memory
is never implicitly initialized, these may be values left over from a terminated process, or simply random
values from the computer power-on procedure. Since C++ pointers make it possible to read the whole
computer memory, an erroneous use of pointers may yield random values nondeterministically. Nonde-
terministic runtime bugs are hard to trace because it is often impossible to recreate the conditions by
which they occur.
I personally had two nightmarish nondeterministic runtime bug experiences, and both took me
months to eradicate. The rst one had something to do with the C++ Standard Template Library
(STL) sort() algorithm with a user-dened less than relation. I had erroneously dened my less
than as a relation instead of a strict ordering < relation. Depending on the contents and mem-
ory position of the array, then, in a completely irreproducible fashion, sorting succeeded or termi-
nated in a SIGSEGV abort. In the second one I was iterating over an STL vector<int> v as fol-
lows: for(vector<int>::iterator vi = v.begin(); vi < v.end(); vi++) {...} , mimicking the
usual integer iteration for(int i = 0; i < n; i++) { ...} . STL iterators, however, are more like
pointers than like integers, and STL vectors are not necessarily organized linearly in memory: which
means that although logically the array element corresponding to the iterator vi may be before the
end of the array (signalled by v.end()), the actual memory address contained in vi might be af-
ter it in the physical memory organization. Consequently, depending on the values contained in v,
the iterator loop sometimes aborted before reaching the end of the vector. Save yourselves a lot of
trouble by employing the correct syntax with a dierent (!=) instead of a less than (<) operator:
for(vector<int>::iterator vi = v.begin(); vi != v.end(); ++vi) {...} .
As a last piece of advice when confronted with a bug that just wouldnt go away: if you spent weeks
looking for a runtime bug without nding it, it probably means its so simple it has escaped your attention.
Question your simplest statements.
3.6 C++ Syntax
See http://www.csci.csusb.edu/dick/c++std/cd2/gram.html for an ANSI C++ grammar.
boolean value: bool (1 bit), true or false
ASCII character: char (1 byte), integer between -128 and 127
integer number:
int (usually 4 bytes), between 2
31
and 2
31
1
long (usually 8 bytes)
can be prexed by unsigned
oating point: double (also float, rarely used)
arrays:
typeName variableName[constArraySize] ; char myString[15];
Basic C++ 14
Lecture Notes C++ Notes L. Liberti
pointers (a pointer contains a memory address): typeName * pointerName ; char* stringPtr;
3.6.1 Declarations, assignments, tests, arithmetic/logical operations
declaration: typeName variableName ; int i;
assignment: variableName = expression ; i = 0;
test:
if ( condition ) {
statements ;
} else {
statements ;
}
if (i == 0) {
i = 1;
} else if (i < 0) {
i = 0;
} else {
i += 2;
}
logical operators: and (&&), or (||), not (!)
condition1 logical op condition2 ; if (!(i == 0 || (i > 5 && i % 2 == 1))) { ...
arithmetic operators: +, -, *, /, %, ++, --, +=, -=, *=, /=, . . .
3.6.2 Loops
loop (while):
while ( condition ) {
statements ;
}
while (i < 10) {
i = i + 1;
}
loop (for):
for ( initial statement ; condition ;
itn statement ) {
statements ;
}
for (i = 0; i < 10; i++) {
std::cout << "i = " << i <<
std::endl;
}
3.7 Functions
function declaration: typeName functionName(typeName1 argName1, . . . );
double performSum(double op1, double op2);
function call: varName = functionName(argName1, . . . ) ;
double d = performSum(1.0, 2.1);
return control to calling code: return value ;
double performSum(double op1, double op2) {
return op1 + op2;
}
Basic C++ 15
Lecture Notes C++ Notes L. Liberti
3.7.1 Argument passing
Arguments are passed from the calling function to the called function in two possible ways:
1. by value
2. by reference
Passing by value (default): the calling function makes a copy of the argument and passes the copy
to the called function; the called function cannot change the argument
double performSum(double op1, double op2);
Passing by reference (prepend a &): the calling function passes the argument directly to the called
function; the called function can change the argument
void increaseArgument(double& arg) { arg++; }
3.7.2 Overloading
Dierent functions with the same name but dierent arguments: overloading
Often used when dierent algorithms exist to obtain the same aim with dierent data types
void getInput(int theInput) {
std::cout << "an integer" << std::endl;
}
void getInput(std::string theInput) {
std::cout << "a string" << std::endl;
}
Can be used in recursive algorithms to dierentiate initialization and recursive step
void retrieveData(std::string URL, int maxDepth, Digraph& G,
bool localOnly, bool verbose);
void retrieveData(std::string URL, int maxDepth, Digraph& G,
bool localOnly, bool verbose,
int currentDepth, VertexURL* vParent);
3.8 Pointers
retrieve the address of a variable:
pointerName = &variableName ;
int* pi;
pi = &i;
retrieve the value stored at an address:
variableName = *pointerName ;
int j;
j = *i;
using pointers as arrays:
const int bufferSize = 10;
char buffer[bufferSize] = "J. Smith";
char* bufPtr = buffer;
while(*bufPtr != ) {
bufPtr++;
}
std::cout << ++bufPtr << std::endl;
Basic C++ 16
Lecture Notes C++ Notes L. Liberti
3.8.1 Warnings
As already remarked, pointers allow direct access to the computer memory, and hence deliver an enormous
power into the programmers hands.
Pointers allow you to access memory directly, hence can be very dangerous
Attempted memory corruption results in segmentation fault error and abort, or garbage output,
or unpredictable behaviour
Most common dangers:
1. writing to memory outside bounds
char buffer[] = "LeoLiberti";
char* bufPtr = buffer;
while(*bufPtr != ) {
*bufPtr = ;
bufPtr++;
}
2. deallocating memory more than once
Pointer bugs are usually very hard to track
3.9 Human-side C++ syntax
There are many dierent ways to write perfectly legal C++ programs. Some of these ways make the
program clear when read by humans, others are less perspicuous. Within this document, we propose a
programming style that should be clear to read. Code clarity is the main weapon against bugs.
Examples of particulary obscure code stylings can be found in the International Obfuscated C Code
Contest (http://www.ioccc.org/). A 2004 nalist reads as follows:
When compiled with the command cc -o anonymous anonymous.c and executed with:
./anonymous "ash nazg durhbatuluhk, ash nazg gimbatul, ash nazg thrakatuluhk, agh burzhumh-ishi krimpatul."
> anonymous.pgm
it produces an output le anonymous.pgm containing the graphics below.
3.9.1 Indentation
Indentation is a coding style that emphasizes the logical structure of blocks. It is the easiest way to
prevent bugs. More details can be found in the exercise book.
Not necessary for the computer
Absolutely necessary for the programmer / maintainer
Basic C++ 17
Lecture Notes C++ Notes L. Liberti
After each opening brace {: new line and tab (2 characters)
Each closing brace } is on a new line and untabbed
double x, y, z, epsilon;
. . .
if (fabs(x) < epsilon) {
if (fabs(y) < epsilon) {
if (fabs(z) < epsilon) {
for(int i = 0; i < n; i++) {
x *= y*z;
}
}
}
}
Proper indentation can be obtained by editing source les with the GNU Emacs text editor.
Traditional GNU/Linux text editor: emacs emacs programName.cxx
Many key-combination commands (try ignoring menus!)
Legenda: C-key: CTRL+key, M-key: ALT+key (for keyboards with no ALT key or for remote
connections can obtain same eect by pressing and releasing ESC and then key)
Basics:
1. C-x C-s: save le in current buer (screen) with current name (will ask for one if none is
supplied)
2. C-x C-c: exit (will ask for conrmation for unsaved les)
3. C-space: start selecting text (selection ends at cursor position)
4. C-w: cut, M-w: copy, C-y: paste
5. tab: indents C/C++ code
6. M-x indent-region: properly indents all selected region
3.9.2 Comments
Not necessary for the computer
Absolutely necessary for the programmer / maintainer
One-line comments: introduced by //
Multi-line comments: /* . . . */
Avoid over- and under-commentation
Example of over-commentation
// assign 0 to x
double x = 0;
Example of under-commentation
char buffer[] = "01011010 01100100";
char* bufPtr = buffer;
while(*bufPtr &&
(*bufPtr++ = *bufPtr == 0 ? F : T));
Basic C++ 18
Lecture Notes C++ Notes L. Liberti
3.10 Structure of a C++ program
Example.
Each executable program coded in C++ must have one function called main()
int main(int argc, char** argv);
The main function is the entry point for the program
It returns an integer exit code which can be read by the shell
The integer argc contains the number of arguments on the command line
The array of character arrays **argv contains the arguments: the command ./mycode arg1 arg2
gives rise to the following storage:
argv[0] is a char pointer to the string ./mycode
argv[1] is a char pointer to the string arg1
argv[2] is a char pointer to the string arg2
argc is an int variable containing the value 3
C++ programs are stored in one or more text les
Source les: contain the C++ code, extension .cxx
Header les: contain the declarations which may be common to more source les, extension .h
Source les are compiled
Header les are included from the source les using the preprocessor directive #include
#include<standardIncludeHeader> #include "userDefinedIncludeFile.h"
3.11 The building process
All source code is held in ASCII text les. The process by which these les are transformed into an
executable program consisting of machine code instructions is called building. A program may involve
many source les written in many dierent languages. Some of the code may already be pre-compiled
into libraries, against which the program must be linked. Handling the whole process may be a nontrivial
task, and is therefore an important part of the programming knowledge. To a program we associate a
project, which loosely speaking is a workspace containing everything that is needed to build the program.
The stages for building a project are as follows.
Creating a directory for your project(s) mkdir directoryName
Entering the directory cd directoryName
Creating/editing the C++ program
Building the source
Debugging the program/project
Packaging/distribution (Makeles, READMEs, documentation. . . )
Basic C++ 19
Lecture Notes C++ Notes L. Liberti
3.11.1 Compilation and linking
The translation process from C++ code to executable is called building, carried out in two stages:
1. compilation: production of an intermediate object le (.o) with unresolved external symbols
2. linking: resolve external symbols by reading code from standard and user-dened libraries
int
getReturnValue(void);
int main() {
int ret = 0;
ret =
getReturnValue();
return ret;
}
Compilation OBJECT
CODE: dictionary asso-
ciating function name to
machine language, save
for undened symbols
(getReturnValue)
main: 0010 1101
...getReturnValue
Linking looks up
libraries (.a and
.o) for unresolved
symbols deni-
tions, produces
executable
3.11.2 File types
C++ declarations are stored in text les with extension .h (header les)
C++ source code is stored in text les with extension .cxx
Executable les have no extensions but their executable property is set to on (e.g. ls -la /bin/bash
returns x in the properties eld)
Each executable must have exactly one symbol main corresponding to the rst function to be
executed
An executable can be obtained by compiling many source code les (.cxx), exactly one of which
contains the denition of the function int main(int argc, char** argv); , and linking all the
objects together
Source code les are compiled into object les with extension .o by the command
c++ -c sourceCode.cxx
3.11.3 Object les
An object le (.o) contains a table of symbols used in the corresponding source le (.cxx)
The symbols whose denition was given in the corresponding source le are resolved
The symbols whose denition is found in another source le are unresolved
Unresolved symbols in an object le can be resolved by linking the object with another object le
containing the missing denitions
An executable cannot contain any unresolved symbol
A group of object les file1.o, . . . , fileN.o can be linked together as a single executable file
by the command c++ -o le le1.o . . . leN.o only if:
1. the symbol main is resolved exactly once in exactly one object le in the group
2. for each object le in the group and for each unresolved symbol in the object le, the symbol
must be resolved in exactly one other le of the group
Basic C++ 20
Lecture Notes C++ Notes L. Liberti
3.11.4 Debuggers
GNU/Linux debugger: gdb
Graphical front-end: ddd
Designed for Fortran/C, not C++
Can debug C++ programs but has troubles on complex objects (use the insert a print statement
technique when gdb fails)
Memory debugger: valgrind (to track pointer bugs)
In order to debug, compile with -g ag: c++ -g -o helloworld helloworld.cxx
More details in the exercise book
3.11.5 Packaging and distribution
For large projects with many source les, a Makefile (detailing how to build the source) is essential
Documentation for a program is absolutely necessary for both users and maintainers
Better insert a minimum of help within the program itself (to be displayed on screen with a particular
option, like -h)
A README le to briey introduce the software is usual
There exist tools to embed the documentation within the source code itself and to produce Makeles
more or less automatically
UNIX packages are usually distributed in tarred, compressed format (extension .tar.gz obtained
with the command tar zcvf directoryName.tar.gz directoryName
4 Classes
4.1 Basic class semantics
4.1.1 Classes: motivations
1. Problem analysis is based on data and algorithm break-down structuring hierarchical design for
data and algorithms
2. Fewer bugs if data inter-dependency is low design data structure rst, then associate algorithms
to data (not the reverse)
3. Data structures are usually complex entities need for suciently rich expressive powers for data
design
4. Dierent data objects may share some properties exploit this fact in hierarchical design
Classes 21
Lecture Notes C++ Notes L. Liberti
4.1.2 The class concept
A class is a user-dened data type. It contains some data elds and the methods (i.e. algo-
rithms) acting on them.
class TimeStamp {
public: // can be accessed from outside
TimeStamp(); // constructor
TimeStamp(); // destructor
long get(void) const; // some methods
void set(long theTimeStamp);
void update(void);
private: // can only be accessed from inside
long timestamp; // a piece of data
};
4.1.3 Objects of a class
An object is a piece of data having a class data type
A class is declared, an object is dened
In a program there can only be one class with a given name, but several objects of the same class
Example:
TimeStamp theTimeStamp; // declare an object
theTimeStamp.update(); // call some methods
long theTime = theTimeStamp.get();
std::cout << theTime << std::endl;
4.1.4 Referring to the current object
Occasionally, we may want to know the address of an object within one of its methods
Each object is endowed with the this pointer cout << this << endl;
4.1.5 Constructors and destructors
The class constructor denes the data elds and performs all user-dened initialization actions
necessary to the object
The class constructor is called only once when the object is dened
The class destructor performs all user-dened actions necessary to object destruction
The class destructor is called only once when the object is destroyed
An object is destroyed when its scope ends (i.e. at the rst brace } closing its level)
Classes 22
Lecture Notes C++ Notes L. Liberti
4.1.6 Lifetime of an object
int main(int argc, char** argv) {
using namespace std;
TimeStamp theTimeStamp; // object created here
theTimeStamp.update();
long theTime = theTimeStamp.get();
if (theTime > 0) {
cout << "seconds from 1/1/1970: "
<< theTime << endl;
}
return 0;
} // object destroyed before brace (scope end)
Constructor and destructor code:
TimeStamp::TimeStamp() {
std::cout << "TimeStamp object constructed at address "
<< this << std::endl;
}
TimeStamp::TimeStamp() {
std::cout << "TimeStamp object at address "
<< this << " destroyed" << std::endl;
}
Output:
TimeStamp object constructed at address 0xbffff24c
seconds from 1/1/1970: 1157281160
TimeStamp object at address 0xbffff24c destroyed
4.1.7 Data access privileges
class ClassName {
public:
members with no access restriction
protected:
access by: this, derived classes, friends
private:
access by: this, friends
} ;
a derived class is a class which inherits from this (see inheritance below)
a function can be declared friend of a class to be able to access its protected and private data
class TheClass {
...
friend void theFriendMethod(void);
};
4.1.8 Namespaces
All C++ symbols (variable names, function names, class names) exist within a namespace
Classes 23
Lecture Notes C++ Notes L. Liberti
The complete symbol is namespaceName::symbolName
The only pre-dened namespace is the global namespace
(its name is the empty string ::varName )
Standard C++ library: namespace std std::string
namespace WET {
const int maxBufSize = 1024;
const char charCloseTag = >;
}
char buffer[WET::maxBufSize];
using namespace WET;
for(int i = 0; i < maxBufSize - 1; i++) {
buffer[i] = charCloseTag;
}
4.1.9 Exceptions
Upon failure, a method may abort its execution
We do not wish the whole program to abort
Mechanism:
1. method throws an exception
2. caller method catches it
3. called method handles it if it can
4. otherwise it re-throws the exception
Exceptions are passed on the method calling hierarchy levels until one of the method can handle it
If exceptions reaches main(), the program is aborted
An exception is a class. Exceptions can be thrown and caught by methods. If a method throws
an exception, it must be declared: returnType methodName(arguments) throw (ExceptionName)
The TimeStamp::update() method obtains the current time through the operating system, which
is outside the programs control
update() does not know how to deal with a failure directly, as it can only update the time; should
failure occur, control is delegated to higher-level methods
class TimeStampException {
public:
TimeStampException();
TimeStampException();
}
Classes 24
Lecture Notes C++ Notes L. Liberti
void TimeStamp::update(void) throw (TimeStampException) {
using namespace std;
struct timeval tv;
struct timezone tz;
try {
int retVal = gettimeofday(&tv, &tz);
if (retVal == -1) {
cerr << "TimeStamp::updateTimeStamp(): "
<< "could not get system time" << endl;
throw TimeStampException();
}
} catch (...) {
cerr << "TimeStamp::updateTimeStamp(): "
<< "could not get system time" << endl;
throw TimeStampException();
}
timestamp = tv.tv sec;
}
4.1.10 Overloading operators in and out of classes
Suppose you have a class Complex with two pieces of private data, double real; and double
imag;
You wish to overload the + operator so that it works on objects of type Complex
There are two ways: (a) declare the operator outside the class as a friend of the Complex class; (b)
declare the operator to be a member of the Complex class
(a) declaration:
class Complex {
public:
Complex(double re, double im) : real(re), imag(im) {}
...
friend Complex operator+(Complex& a, Complex& b);
private:
double real;
double imag;
}
denition (out of the class):
Complex operator+(Complex& a, Complex& b) {
Complex ret(a.real + b.real, a.imag + b.imag);
return ret;
}
(b) declaration:
class Complex {
public:
Complex(double re, double im) : real(re), imag(im) {}
...
Complex operator+(Complex& b);
private:
double real;
double imag;
}
Classes 25
Lecture Notes C++ Notes L. Liberti
denition (in the class):
Complex Complex::operator+(Complex& b) {
Complex ret(this->real + b.real, this->imag + b.imag);
return ret;
}
this-> is not strictly required, but it makes it clear that the left operand is now the object calling
the operator+ method
4.1.11 The stack and the heap
Executable program can either refer to near memory (the stack) or far memory (the heap)
Accessing the stack is faster than accessing the heap
The stack is smaller than the heap
Variables are allocated on the stack TimeStamp tts;
Common bug (but hard to trace): stack overow char veryLongArray[1000000000];
Memory allocated on the stack is deallocated automatically at the end of the scope where it was
allocated (closing brace })
Memory on the heap can be accessed through user-dened memory allocation
Memory on the heap must be deallocated explicitly, otherwise memory leaks occur, exhausting all
the computers memory
Memory on the heap must not be deallocated more than once (causes unpredictable behaviour)
4.1.12 User-dened memory allocation
Operator new: allocate memory from the heap pointerType* pointerName = new pointerType ;
TimeStamp* ttsPtr = new TimeStamp;
Operator delete: release allocated memory delete pointerName; delete ttsPtr;
Commonly used with arrays in a similar way:
pointerType* pointerName = new pointerType [size];
double* positionVector = new double [3];
delete [] pointerName ; delete [] positionVector;
Improper user memory management causes the most dicult C++ bugs!!
4.1.13 Using object pointers
Suppose ttsPtr is a pointer to a TimeStamp object
Two equivalent ways to call its methods:
1. (*ttsPtr).update();
2. ttsPtr->update();
Prefer second way over rst
Classes 26
Lecture Notes C++ Notes L. Liberti
4.2 Input and output
4.2.1 Streams
Data run through streams
Stream types: input, output, input/output, standard, le, string, user-dened
outputStreamName << varName or literal . . . ; std::cout << "i = " << i << std::endl;
inputStreamName >> varName ; std::cin >> i;
stringstream buffer;
char myFileName[] = "config.txt";
ifstream inputFileStream(myFileName);
char nextChar;
while(inputFileStream && !inputFileStream.eof()) {
inputFileStream.get(nextChar);
buffer << nextChar;
}
cout << buffer.str();
4.2.2 Object onto streams
Complex objects may have a complex output procedure
Example: we want to be able to say cout << theTimeStamp << endl; and get
Thu Sep 7 12:23:11 2006 as output
Solution: overload the << operator
std::ostream& operator<<(std::ostream& s,
TimeStamp& t)
throw (TimeStampException);
#include <ctime>
std::ostream& operator<<(std::ostream& s, TimeStamp& t)
throw (TimeStampException) {
using namespace std;
time t theTime = (time t) t.get();
char* buffer;
try {
buffer = ctime(&theTime);
} catch (...) {
cerr << "TimeStamp::updateTimeStamp(): "
"couldnt print system time" << endl;
throw TimeStampException();
}
buffer[strlen(buffer) - 1] = \0;
s << buffer;
return s;
}
4.2.3 Overloading the << and >> operators
How does an instruction like cout << "time is " << theTimeStamp << endl; work?
Can parenthesize is as (((cout << "time is ") << theTimeStamp) << endl); to make it clearer
Classes 27
Lecture Notes C++ Notes L. Liberti
Each << operator is a binary operator whose left operand is an object of type ostream (like the
cout object); we need to dene an operator overloading for each new type that the right operand
can take
Luckily, many overloadings are already dened in the Standard Template Library
The declaration to overload is:
std::ostream& operator<<(std::ostream& outStream,
newType& newObject)
To output objects of type TimeStamp, use:
std::ostream& operator<<(std::ostream& outStream,
TimeStamp& theTimeStamp)
Note: in order for the chain of << operators to output all their data to the same ostream object,
each operator must return the same object given at the beginning of the chain (in this case, cout)
In other words, each overloading must end with the statement return outStream; (notice outStream
is the very same name of the input parameter so if the input parameter was, say, cout, then
thats whats being returned by the overloading)
4.3 Inheritance and polymorphism
4.3.1 Inheritance
Consider a class called FileParser which is equipped with methods for parsing text occurrences
like tag = value in text les
We now want a class HTMLPage representing an HTML page with all links
HTMLPage will need to parse an HTML (text) le to nd links; these are found by looking at
occurrences like HREF="url"
It is best to keep the text le parsing data/methods and HTML-specic parts independent
HTMLPage can inherit the public data/methods from FileParser:
class HTMLPage : public FileParser {...} ;
4.3.2 Nested inheritance
Consider a corporate personnel database
Need class Employee;
Certain employees are empowered (have more responsibilities): need
class Empowered : public Employee;
Among the empowered employees, some are managers: need class Manager : public Empowered;
Manager contains public data and methods from Empowered, which contains public data and meth-
ods from Employee
Classes 28
Lecture Notes C++ Notes L. Liberti
class Employee {
public:
Employee();
Employee();
double
getMonthlySalary(void);
void getEmployeeType(void);
};
and set the cycle at coloured cottons; you satisfy yourself that you dont have a full load,
so you press the half-load button; nally, you start the washing cycle. Is this a program? (b) Now
suppose your girlfriend tells you to do the washing, please. Is this a program? Justify your answers.
1.2 Denition of programming language
Are the rules of chess a programming language? Justify your answer.
1.3 Indentation
Give a precise denition of the concept well-indented C++ program. Check whether your denition
correctly classies the following snippets of code. In the case of programs that are not well-indented, nd
the reason why they are not.
// 1. this is a well-indented C++ program
#include<iostream>
int main(int argc, char** argv) {
int ret = 0;
std::cout << "Hello world" << std::endl;
return ret;
}
// 2. this is a well-indented C++ program
#include<iostream>
const int theGlobalValue = 0;
int main(int argc, char** argv) {
theGlobalValue = 1;
std::cout << theGlobalValue << std::endl;
return 0;
5
Exercises Introduction to C++ L. Liberti
}
// 3. this is NOT a well-indented C++ program (why?)
#include<iostream>
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "error" << std::endl;
} else {
std::cout << argv[1] << std::endl;
}
}
// 3. this is NOT a well-indented C++ program (why?)
#include<iostream>
const int theGlobalValue = 0;
bool isArgcGood(int argc) {
if (argc < 2) {
std::cout << "error" << std::endl;
return false; }
return true;
}
int main(int argc, char** argv) {
if (isArgcGood(argc)) {
std::cout << argv[1] << std::endl;
}
return 0;
}
// 4. this is NOT a well-indented C++ program (why?)
#include<iostream>
int main(int argc, char** argv) {
int i = 0;
while(argv[i]) i++;
std::cout << i << std::endl;
}
Indentation 6
Chapter 2
Basic syntax
2.1 A minimal C++ program
Edit, save and build the following program.
/***********************************************
* Name: minimal.cxx
* Author: Leo Liberti
* Source: GNU C++
* Purpose: minimal C++ program
* Build: c++ -o minimal minimal.cxx
* History: 060818 work started
***********************************************/
#include<iostream>
int main(int argc, char** argv) {
using namespace std;
int ret = 0;
// your code goes here
return ret;
}
Now modify it to print the array of characters argv[0].
2.2 Variables and pointers
Consider the following program.
#include<iostream>
const char endOfCharArray = \0;
7
Exercises Introduction to C++ L. Liberti
int main(int argc, char** argv) {
using namespace std;
int ret = 0;
if (argc < 2) {
cerr << "error: no arguments on cmd line" << endl;
ret = 1;
} else {
for(int i = 0; i < argc; i++) {
while(*(argv[i]) != endOfCharArray) {
cout << *(argv[i]);
argv[i]++;
}
cout << endl;
}
}
return ret;
}
(a) List the variables in the program and their types. (b) List the pointers in the program and
their types. (c) How many pointers are there in the program when it is run with the command line
./variables pointers 123 456 789? (d) Edit, save, build and run the program with the command
line above. What is the output?
2.3 The second word
(a) Write a program that writes the second word (and its length) in an array of characters. Words are
separated by spaces, punctuation and tabs. Test your code on the sentence Actually, couldnt you come
with me? The correct answer should be couldnt(8). If youre stuck, you can follow the template
below.
// test if c is in the array containing all the valid separators
bool isSeparator(char c, char* separators);
// return the length of the char array a
int charArrayLength(char *a);
int main(int argc, char** argv) {
// find the beginning of the second word
// find the end of the second word
}
2.4 Random numbers
The following code can be used to generate pseudo-random oating point numbers in [0, 1].
#include<iostream>
#include<sys/time.h>
int main(int argc, char** argv) {
using namespace std;
// initialize randomizer
Random numbers 8
Exercises Introduction to C++ L. Liberti
struct timeval theTV;
struct timezone theTZ;
gettimeofday(&theTV, &theTZ);
srandom(theTV.tv_usec);
// pick a random number between 0 and 1
double normalizedRndCoeff = (double) random() / (double) RAND_MAX;
cout << normalizedRndCoeff << endl;
return 0;
}
Write a small program that generates a random sample of given size N (input from command line)
and that computes the mean and the variance. Use your program to show empirically that
= lim
N
Var(X)
= 0.083,
where X is a random variable. Prove that =
1
12
.
Optional: update your program to generate a random sample whose elements are within given
numerical bounds.
2.5 The second column
Write a program that reads in a text le and prints the sum of the numbers found in the second column
(a word is in the second column of a le if its rst character is the rst non-space character after the rst
space character in the line). An example for opening and reading a text le is given below.
#include <iostream>
#include <fstream>
#include <cstdlib>
const int BUFSIZE = 1024;
int main(int argc, char** argv) {
using namespace std;
int ret = 0;
if (argc < 2) {
cerr << argv[0] << ": need filename on cmd line" << endl;
exit(1);
}
char buffer[BUFSIZE];
ifstream myStream(argv[1]);
while(!myStream.eof()) {
myStream.getline(buffer, (streamsize) BUFSIZE-1);
cout << buffer << endl;
}
return ret;
}
The second column 9
Exercises Introduction to C++ L. Liberti
The second column 10
Chapter 3
Classes
3.1 Complex numbers
The following code is the class header describing a complex number class.
/*
** Name: complex.h
** Author: Leo Liberti
** Purpose: header file for a complex numbers class
** Source: GNU C++
** History: 061019 work started
*/
#ifndef _COMPLEXH
#define _COMPLEXH
#include<string>
#include<iostream>
class Complex {
public:
Complex();
Complex(double re, double im);
~Complex();
double getReal(void);
double getImaginary(void);
void setReal(double re);
void setImaginary(double im);
void fromString(const std::string& complexString);
Complex operator+(Complex& theComplex);
Complex operator-(Complex& theComplex);
Complex operator*(Complex& theComplex);
Complex operator/(Complex& theComplex);
private:
double real;
double imag;
};
std::ostream& operator<<(std::ostream& out, Complex& theComplex);
#endif
Write the corresponding implementation le complex.cxx. Test it on the following main() function
denition, using the command ./test 2.1+0.2i / 0.2-0.3i . The output should be 2.76923 + 5.15385i .
/*
11
Exercises Introduction to C++ L. Liberti
** Name: test.cxx
** Author: Leo Liberti
** Purpose: testing the complex numbers class
** Source: GNU C++
** History: 061019 work started
*/
#include <iostream>
#include <string>
#include "complex.h"
int main(int argc, char** argv) {
using namespace std;
if (argc < 4) {
cerr << "need an operation on command line" << endl;
cerr << " e.g. ./test 4.3+3i - 2+3.1i" << endl;
cerr << " (no spaces within each complex number, use spaces to\n";
cerr << " separate the operands and the operator - use arithmetic\n";
cerr << " operators only)" << endl;
return 1;
}
string complexString1 = argv[1];
string complexString2 = argv[3];
Complex complex1;
complex1.fromString(complexString1);
Complex complex2;
complex2.fromString(complexString2);
Complex complex3;
if (argv[2][0] == +) {
complex3 = complex1 + complex2;
} else if (argv[2][0] == -) {
complex3 = complex1 - complex2;
} else if (argv[2][0] == * || argv[2][0] == .) {
argv[2][0] = *;
complex3 = complex1 * complex2;
} else if (argv[2][0] == /) {
complex3 = complex1 / complex2;
}
cout << complex1 << " " << argv[2][0] << " (" << complex2 << ") = "
<< complex3 << endl;
return 0;
}
Use the following Makefile to compile (use tabs, not spaces after each label for that you need to
use the Emacs editor):
# Name: complex.Makefile
# Author: Leo Liberti
# Purpose: makefile for complex class project
# Source: GNU C++
# History: 061019 work started
CXXFLAGS = -g
all: test
test: complex.o test.cxx
c++ $(CXXFLAGS) -o test test.cxx complex.o
complex.o: complex.cxx complex.h
c++ $(CXXFLAGS) -c -o complex.o complex.cxx
clean:
rm -f *~ complex.o test
3.2 Virtual inheritance
The code below denes a virtual base class VirtualBase and a derived class Derived which implements
it. The VirtualBase interface is simply to set and get an integer value. The Derived adds a method
for printing the value. We then have two functions in the global namespace, printTheValue1() and
Virtual inheritance 12
Exercises Introduction to C++ L. Liberti
printTheValue2(), which print out the values in dierent ways: the rst simply uses the get method
of the interface to retrieve the value; the second tries to transform the VirtualBase interface pointer
passed as argument to a pointer to the Derived class, and then calls the Derived class print method.
// this program does not compile!
#include<iostream>
class VirtualBase {
public:
virtual ~VirtualBase() { }
virtual void setValue(int i) = 0;
virtual int getValue(void) = 0;
};
class Derived : public virtual VirtualBase {
public:
~Derived() { }
void setValue(int i) {
theValue = i;
}
int getValue(void) {
return theValue;
}
void printValue(void) {
std::cout << "Derived::printValue(): value is " << theValue << std::endl;
}
private:
int theValue;
};
void printTheValue1(VirtualBase* v) {
std::cout << "printTheValue1(): value is " << v->getValue() << std::endl;
}
void printTheValue2(VirtualBase* v) {
Derived* d = v;
d->printValue();
}
int main(int argc, char** argv) {
int ret = 0;
Derived d;
VirtualBase* v = &d;
v->setValue(1);
printTheValue1(v);
printTheValue2(v);
return ret;
}
The desired output is:
printTheValue1(): value is 1
Derived::printValue(): value is 1
Virtual inheritance 13
Exercises Introduction to C++ L. Liberti
However, the program fails to compile with the error:
virtual2.cxx: In function void printTheValue2(VirtualBase*):
virtual2.cxx:31: error: invalid conversion from VirtualBase* to Derived*
virtual2.cxx:31: error: cannot convert from base VirtualBase
to derived type Derived via virtual base VirtualBase
What has gone wrong? How can you x this program? [Hint: look at C++ casting operators]
3.3 Virtual and nonvirtual inheritance
What is the output of the following code?
#include<iostream>
class A {
public:
void f() {
std::cout << "A::f" << std::endl;
}
virtual void g() {
std::cout << "A::g" << std::endl;
}
};
class B : public A {
public:
void f() {
std::cout << "B::f" << std::endl;
}
virtual void g() {
std::cout << "B::g" << std::endl;
}
};
int main(int argc, char** argv) {
A a;
B b;
A* aPtr = &a;
A* bPtr = &b;
aPtr->f();
aPtr->g();
bPtr->f();
bPtr->g();
return 0;
}
Is there anything surprising? Can you explain it?
Since this is produced by this code:
aPtr->f();
Virtual and nonvirtual inheritance 14
Exercises Introduction to C++ L. Liberti
aPtr->g();
bPtr->f();
bPtr->g();
It is surprising that the pointer to the B object does not print B::f but A::f. The reason is that the
derived class B hides the method A::f(), which was not declared virtual. This is known as compile-time
polymorphism: at compile-time, the compiler only knows that bPtr is of type A*, and therefore binds
to it the f() method found in A. The g() method, however, was declared virtual in the base class,
which means that the compiler will implement run-time polymorphism (almost always the desired kind),
and will delegate binding decisions (i.e. , deciding what method to call) to run-time. At run-time, even
though bPtr is of type A*, it is possible to nd out that in fact it points to an object of type B, so the
correct binding takes place.
3.4 Nonvirtual base class destructor
The code below shows the ill eects of hiding a non-virtual constructor of a base class, and then using
the derived class as a base one.
// this program is buggy!
#include<iostream>
class Base {
public:
Base() {
std::cerr << "constructing Base " << this << std::endl;
i = new int;
}
~Base() {
std::cerr << "destroying Base " << this << std::endl;
delete i;
}
private:
int* i;
};
class Derived : public Base {
public:
Derived() {
std::cerr << "constructing Derived " << this << std::endl;
d = new double;
}
~Derived() {
std::cerr << "destroying Derived " << this << std::endl;
delete d;
}
private:
double* d;
};
int main(int argc, char** argv) {
using namespace std;
int ret = 1;
Nonvirtual base class destructor 15
Exercises Introduction to C++ L. Liberti
Base* thePtr = new Derived;
delete thePtr;
return ret;
}
The output of this program is
constructing Base 0x804a008
constructing Derived 0x804a008
destroying Base 0x804a008
This is a bug, because the Derived object was never deallocated. At best, this is a memory leak. In
some other cases it may become the source of more serious problems. Can you explain what happened
and how to x the program?
Nonvirtual base class destructor 16
Chapter 4
Debugging
4.1 Segmentation fault
The following code contains three serious memory bugs, usually ending in a segmentation fault. Find the
bugs, solve them and explain them.
// this program is buggy
#include<iostream>
int main(int argc, char** argv) {
using namespace std;
double* d = new double;
for(unsigned int i = 0; i < 3; i++) {
d[i] = 1.5 + i;
}
for(unsigned int i = 2; i >= 0; i--) {
cout << d[i] << endl;
}
}
4.2 Pointer bug
The following snippet of code behaves unpredictably: it will often print a value for testInt which is
dierent from 1, although in the variable testInt is never explicitly changed. Debug the program using
ddd and valgrind. What is the bug? How can you x it? Change the order of the statements marked
(a), (b), (c) and test the resulting programs: what is the behaviour? Can you explain it?
// this program is buggy!
#include<iostream>
#include<cstring>
const int bufSize = 20;
int main(int argc, char** argv) {
17
Exercises Introduction to C++ L. Liberti
using namespace std;
if (argc < 3) {
cerr << "need a char and a word as arguments on cmd line" << endl;
return 1;
}
// a test integer to which we assign the value 1
int testInt = 1; // (a)
// get the first char of the first argument
char theChar = argv[1][0]; // (b)
// get the second argument as word
char buffer[bufSize]; // (c)
strncpy(buffer, argv[2], bufSize);
char* wordPtr = buffer;
// skip characters in the word and delete them, until we find theChar
while(*wordPtr != theChar) {
*wordPtr = ;
wordPtr++;
cout << (int) (wordPtr - buffer) << endl;
}
// now see what value has the test integer
cout << testInt << endl;
return 0;
}
4.3 Scope of local variables
Inspect the following code. Convince yourself that the expected output should be 0 1 2 3 4 . Compile
and test the code. The output is unpredictable (may be similar to
134524936 134524968 134524952 134524984 134525024 depending on the machine and circumstances).
How do you explain this bug? How can you x it?
#include<iostream>
#include<vector>
class ValueContainer {
public:
ValueContainer() : theInt(0) { }
~ValueContainer() { }
void set(int* t) {
theInt = t;
}
int get(void) {
return *theInt;
}
private:
int* theInt;
Scope of local variables 18
Exercises Introduction to C++ L. Liberti
};
ValueContainer* createContainer(int i) {
ValueContainer* ret = new ValueContainer;
ret->set(&i);
return ret;
}
const int vecSize = 5;
int main(int argc, char** argv) {
using namespace std;
vector<ValueContainer*> value;
for(int i = 0; i < vecSize; i++) {
value.push_back(createContainer(i));
}
for(int i = 0; i < vecSize; i++) {
cout << value[i]->get() << " ";
}
cout << endl;
for(int i = 0; i < vecSize; i++) {
delete value[i];
}
return 0;
}
4.4 Erasing elements from a vector
The following code initializes an STL vector of integers to (2, 1) then iterates to erase all elements with
value 1. The code compiles but on running it we get a segmentation fault abort. Find and x the bug.
// this program is buggy!
#include<vector>
int main(int argc, char** argv) {
using namespace std;
int ret = 0;
vector<int> theVector;
theVector.push_back(2);
theVector.push_back(1);
vector<int>::iterator vi;
for(vi = theVector.begin(); vi != theVector.end(); vi++) {
if (*vi == 1) {
theVector.erase(vi);
}
}
return ret;
}
Erasing elements from a vector 19
Exercises Introduction to C++ L. Liberti
Erasing elements from a vector 20
Chapter 5
A full-blown application
In this chapter we will describe in a step-by-step fashion how to build a full-blown application in C++.
The application is called WET (WWW Exploring Topologizer) and its purpose is to explore a neighbour-
hood of a given URL in the World Wide Web and to output it in the shape of a graph. This application
can be used to trace website maps or relations between websites (which often underline relations between
the corresponding corporations); see for examples the maps of the IEEE (Fig. 5.1) and PSA (Fig. 5.2)
websites.
www.ieee.com
ieeexplore.ieee.org?WT.mc_id=tu_xplore
spectrum.ieee.org
www.spectrum.ieee.org
www.ieee.org
www.scitopia.org
careers.ieee.org
ieeexplore.ieee.org www.adicio.com
Thu Jan 10 18:18:18 2008
Figure 5.1: The IEEE website map.
We delegate graph visualization to the GraphViz library www.graphviz.org, and in particular to the
dot and neato UNIX utilities. These accept the input graph in a particular format
digraph graphName {
# list of nodes with special properties
0 [ label = "thenode", color = red ];
# list of arcs
21
Exercises Introduction to C++ L. Liberti
www.psa-peugeot-citroen.com
www.peugeot.com www.sustainability.psa-peugeot-citroen.com b2b.psa-peugeot-citroen.com www.psa.net.cn www.slovakia.psa-peugeot-citroen.com
www.developpement-durable.psa.fr www.peugeot.sk www.citroen.sk
www.peugeot-avenue.com www.citroen-bazar.sk www.nascortech.com
Thu Jan 10 18:43:51 2008
Figure 5.2: The PSA website map.
0 -> 1;
1 -> 2;
}
So the task performed by WET is to download a given URL and explore its links up to the n-th
recursion level, then to put the obtained graph in the above format.
This chapter starts with a section on the software architecture; each class is then described and its
implementation mostly delegated as exercise. The software architecture section does not contain any
exercises, but it is a prerequisite for understanding what follows. Moreover, it is a (crude) example
showing how to formalize the architecture of a software.
5.1 The software architecture
5.1.1 Description
Given an URL, this tool explores a local neighbourhood of the URL. Each web page is represented by a
vertex in a graph, and given two vertices u, v there is an arc between them if there is a hyperlink citing
the web page v in the web page u. The graph is then printed on the screen.
5.1.2 Formalization of requirements
Input:
the URL we must start the retrieval from
the maximum hyperlink depth
Output:
graph and time when the exploration was undertaken shown on screen
5.1.3 Modularization of tasks
web page retrieval: downloads the HTML page for a given URL
web page parser: nds all the hyperlinks in a given HTML page
watch: returns the current time
graph: stores vertices, arcs and a timestamp; outputs the graph to screen
The software architecture 22
Exercises Introduction to C++ L. Liberti
5.1.4 Main algorithm
1. Input options and necessary data (from command line)
2. Start recursive URL retrieval algorithm
retrieveData(URL, maxDepth, Graph) {
get current timestamp;
store timestamp in graph
retrieveData(URL, maxDepth, Graph, 0);
}
store graph on disk
retrieveData(URL, maxDepth, Graph, currentDepth) {
if (currentDepth < maxDepth) {
retrieve URL;
parse HTML, get list of sub-URLs;
for each sub-URL in list {
store arc (URL, sub-URL) in graph
retrieveData(sub-URL, maxDepth, graph, currentDepth + 1);
}
}
}
5.1.5 Fundamental data structures
URL
timestamp
vertex
arc
graph
5.1.6 Classes
class TimeStamp; // stores a time stamp
class fileParser; // parses text files for tag=value (or tag="value") cases
class HTMLPage : public fileParser; // stores an HTML page
class URL; [contains HTMLPage object] // retrieves an HTML page
class Vertex; // public virtual class for a vertex
class VertexURL : public virtual Vertex; [contains URL] // implements a vertex
class Arc; [contains two Vertex objects] // arc
class Digraph; [contains a list of arcs and a timestamp] // graph
The diagram below shows the class interactions.
The software architecture 23
Exercises Introduction to C++ L. Liberti
5.2 The TimeStamp class
Design and implement a class called TimeStamp managing the number of seconds elapsed since 1/1/1970.
The class should have three methods: long get(void) to get the number of seconds since 1/1/1970, void
set(long t) to set it, void update(void) to read it o the operating system. Furthermore, it must be
possible to pass a TimeStamp object (call it timeStamp) on to a stream, as follows: cout << timeStamp;
to obtain the date pointed at by timeStamp. Failures should be signalled by throwing an exception called
TimeStampException.
You can use the following code to nd the number of seconds elapsed since 1970 from the operating
system:
#include<sys/time.h>
[...]
struct timeval tv;
struct timezone tz;
int retVal = gettimeofday(&tv, &tz);
long numberOfSecondsSince1970 = tv.tv_sec;
The following code transforms the number of seconds since 1970 into a meaningful date into a string:
#include<string>
#include<ctime>
#include<cstring>
[...]
time_t numberOfSecondsSince1970;
char* buffer = ctime(&numberOfSecondsSince1970);
buffer[strlen(buffer) - 1] = \0;
std::string theDateString(buffer);
Write the class declaration in a le called timestamp.h and the method implementation in timestamp.cxx.
Test your code with the following program tsdriver.cxx: and verify that the output is:
The TimeStamp class 24
Exercises Introduction to C++ L. Liberti
seconds since 1/1/1970: 999999999 corresponding to Sun Sep 9 03:46:39 2001
Mon Sep 11 11:33:42 2006 corresponds to 1157967222 number of seconds since 1970
The solution to this exercise is included as an example of coding style. The rest of the exercises in
this chapter should be solved by the student.
5.3 The FileParser class
Design and implement a class called FileParser that scans a le for a given string tag followed by a
character = and then reads and stores the string after the = either up to the rst space character
or, if an opening quote character " is present after =, up to the closing quote character. The class
functionality is to be able to detect all contexts such as tag = string or tag = "string" in a text le,
store the strings and allow subsequent access to them.
The class should be declared in fileparser.h and implemented in fileparser.cxx. You can use
the following header code for class declaration. Pay attention to the following details:
It is good coding practice to provide all classes with a set of get/set methods to access/modify
internal (private) data.
The compareCaseInsensitive and isTailCaseInsensitive methods are declared protected be-
cause they may not be accessed externally (i.e. they are for the class private usage) but it may
be useful to make them accessible to derived classes. Here is the denition of these two (technical)
methods:
int FileParser::compareCaseInsensitive(const std::string& s1,
const std::string& s2) const {
using namespace std;
string::const_iterator p1 = s1.begin();
string::const_iterator p2 = s2.begin();
while(p1 != s1.end() && p2 != s2.end()) {
if (toupper(*p1) < toupper(*p2)) {
return -1;
} else if (toupper(*p1) > toupper(*p2)) {
return 1;
}
p1++;
p2++;
}
if (s1.size() < s2.size()) {
return -1;
} else if (s1.size() > s2.size()) {
return 1;
}
return 0;
}
bool FileParser::isTailCaseInsensitive(const std::string& s1,
const std::string& s2) const {
using namespace std;
int s2len = s2.size();
if (s1.size() >= s2.size() &&
compareCaseInsensitive(s1.substr(s1.size() - s2len, s2len), s2) == 0) {
The FileParser class 25
Exercises Introduction to C++ L. Liberti
return true;
}
return false;
}
In order to write the parse method, you have to scan the le one character at a time. The parser
can be in any of the following three states: outState, inTagState, inValueState. Normally, the
status is outState. When the parse tag is detected, a transition is made to inTagState. After that,
if an equal character (=) is found, another transition is made to inValueState. The parser has
three dierent behaviours, one in each state. When in outState, it simply looks for the parse tag
parseTag. When in inTagState, it looks for the character =, when in inValueState, it stores
the value up to the rst separating space or between quotes.
The following code can be used to open a le and scan it one character at a time:
#include<iostream>
#include<fstream>
#include<istream>
#include<sstream>
#include<iterator>
[...]
// opens the file for input
string fileName("myfilename.ext");
ifstream is(fileName.c_str());
if (!is) {
// cant open file, throw exception
}
// save all characters to a string buffer
char nextChar;
stringstream buffer;
while(!is.eof()) {
is.get(nextChar);
buffer << nextChar;
}
// output the buffer
cout << buffer.str() << endl;
// erase the buffer
buffer.str("");
You can test your code with the following program fpdriver.cxx: Make a small HTML le as
follows, and call it myfile.html:
<html>
<head>
<title>MyTitle</title>
</head>
<body>
My <a href="http://mywebsite.com/mywebpage.html">absolute link</a>
and my <a href="otherfile.html">relative link</a>.
</body>
</html>
Now run the fpdriver code as follows: ./fpdriver myfile.html href , and verify that it pro-
duces the following output:
The FileParser class 26
Exercises Introduction to C++ L. Liberti
http://mywebsite.com/mywebpage.html
otherfile.html
5.4 The HTMLPage class
Design and implement a class called HTMLPage which inherits from FileParser. It should have the
following functionality: opens an HTML le (of which the URL is known), reads and stores its links
(transforming relative links into absolute links), allows external access to the stored links. This class is
derived from FileParser because it uses FileParsers functionality in a specic manner, performing
some specic task (relative links are transformed into absolute links) of which FileParser knows nothing
about.
A link in an HTML page is the value in the tag href="value" (or simply href=value). HTML is
a case insensitive language, so you may also nd HREF instead of href.
A WWW link is absolute if it starts with http:// and a DNS name and is relative otherwise.
Given a relative link (i.e. not starting with a slash / character, e.g. liberti/index.html) and
an absolute link (e.g. http://www.lix.polytechnique.fr/users), the relative link is transformed
into an absolute link by concatenating the two URLs (absolute and relative) with any missing
slashes in between (e.g. http://www.lix.polytechnique.fr/users/liberti/index.html. On
the other hand, if the relative link starts with a slash / (e.g. / liberti/index.html) then
the transformation of the relative link to absolute occurs by concatenating the rst part of the
absolute link (up to and including the DNS name) and the relative one (e.g. http://www.lix.
polytechnique.fr/
~
liberti/index.html).
Remember that not all links found next to HREF tags are valid HTTP links, some may concern
dierent protocols. You should make sure that the link introduced by HREF is either relative (in
which case you should make it absolute as explained above) or specically an http link.
Test your class implementation using the following driver hpdriver.cxx: and check that the
output of the command ./hpdriver http://127.0.0.1 myfile.html is
http://mywebsite.com/mywebpage.html
http://127.0.0.1/otherfile.html
5.5 The URL class
Design and implement a class called URL containing a pointer to an HTMLPage object. Its functionality is
to download an HTML le from the internet given its URL, to allow access to the links contained therein,
and to return the hostname of the URL.
We shall make use of the wget external utility to download the HTML le. This can be used from
the shell: wget http://my.web.site/mypage.html will download the specied URL to a local
le called mypage.html. We can specify the output le with the option -O output file name. In
order to call an external program from within C++, use the following code.
#include<cstdlib>
[...]
char charQuote = \";
The URL class 27
Exercises Introduction to C++ L. Liberti
string theURL = "http://127.0.0.1/index.html";
string outputFile = ".wet.html";
string cmd = "wget --quiet -O " + outputFile + " " + charQuote + theURL + charQuote;
int status = system(cmd.c_str());
You will need to delete the temporary le .wet.html after having parsed it use the following
code.
#include<unistd.h>
[...]
unlink(outputFile.c_str());
The URL class will expose a method std::string getNextLink(void) to read the links in the
downloaded HTML page in the order in which they appear. Each time the method is called,
the next link is returned. When no more links are present, the empty string should be returned.
Subsequent calls to getNextLink() should return the sequence of links again as in a cycle.
The URL class will contain a pointer to an HTMLPage object which parses the HTML code and nds
the relevant links. Note that this means that a user memory allocation/deallocation will need to
be performed.
Test your URL implementation using the driver program urldriver.cxx: Run the program
./urldriver http://www.enseignement.polytechnique.fr/profs/informatique/Leo.
Liberti/test.html
and verify that the output is
http://www.enseignement.polytechnique.fr/profs/informatique/Leo.Liberti/test3.html
http://www.enseignement.polytechnique.fr/profs/informatique/Leo.Liberti/test2.html
5.6 The Vertex interface and VertexURL implementation
Design and implement a class VertexURL inheriting from the pure virtual class Vertex: VertexURL is
an encapsulation of a URL object which conforms to the Vertex interface. This will be used later in the
Digraph class to store the vertices of the graph. This separation between the Vertex interface and its
particular implementation (for example the VertexURL class) allows the Digraph class to be re-used with
other types of vertices, and therefore makes it very useful. An object conforming to the Vertex interface
must know (and be able to set and get): its own integer ID, the IDs of the vertices adjacent to it, a text
label associated to the vertex. Note that Vertex has a virtual destructor for the following reason: should
any agent attempt a direct deallocation of a Vertex object, the destructor actually called will be the
destructor of the interface implementation (VertexURL in this case, but might be dierent depending on
run-time conditions) rather than the interface destructor.
Test your implementation on the following code: Run the program
./vurldriver http://kelen.polytechnique.fr and verify that the output is vertex 0: star 1 2 .
5.7 The Arc and Digraph classes
1. Design and implement an Arc class to go with the Vertex class above. In practice, an Arc object
just needs to know its starting and ending vertex IDs.
The Arc and Digraph classes 28
Exercises Introduction to C++ L. Liberti
2. Design and implement a Digraph class to go with the Vertex and Arc classes dened above. This
needs to store a TimeStamp object, a vector of pointers to Vertex objects, a vector of pointers to
Arc objects. We need methods both for constructing (addVertex, addArc) as well as accessing the
graph information (getNumberOfVertices, getNumberOfArcs, getVertex, getArc). We also want
to be able to send a Digraph object to the cout stream to have a GraphViz compatible text output
which we shall display using the GraphViz utilities.
Test your implementation on the following program dgdriver.cxx: and verify that the output is:
# graphviz output by WET (L. Liberti 2006)
digraph www_1158015497 {
0 [ label = "name1" ];
1 [ label = "name2" ];
2 [ label = "name3" ];
3 [ label = "Thu Jan 10 19:24:53 2008", color = red ];
1 -> 2;
1 -> 3;
2 -> 1;
3 -> 3;
}
5.8 Putting it all together: main()
Code the main function and all auxiliary functions into the les wet.h (declarations) and wet.cxx (def-
initions). The program should: read the given URL and the desired downloading recursion depth limit
from the command line, then recursively download the given URL and all meaningful sub-links up to the
given depth limit, whilst storing them as vertices and arcs of a digraph, which will then be printed out
in GraphViz format.
The recursive part of the algorithm can be coded into a pair of functions (one is the driver
function, the other is the truly recursive one) declared as follows:
// retrieve the data, driver
void retrieveData(std::string URL, int maxDepth, Digraph& G,
bool localOnly, bool verbose);
// retrieve the data, recursive
void retrieveData(std::string URL, int maxDepth, Digraph& G,
bool localOnly, bool verbose,
int currentDepth, VertexURL* vParent);
(also see Section 5.1.4).
Endow your WET application with a command line option -v (stands for verbose) that prints
out the URLs as they are downloaded
Endow your WET application with a command line option -l (stands for local) that ignores all
URLs referring to webservers other than localhost (127.0.0.1). Optional: also try to devise an
option -L that ignores all URLs referring to webservers dierent from the one given on command
line.
The GraphViz system can read a text description of a given graph and output a graphical rep-
resentation of the graph in various formats. The WET application needs to output the text de-
scription of the Digraph object. One self-explanatory example of the required format has been
Putting it all together: main() 29
Exercises Introduction to C++ L. Liberti
given at the beginning of this chapter. You can type man dot at the shell to get more infor-
mation. Implement WET so that it outputs the graph description in the correct format on cout.
When your WET application is complete, run it so that it saves the output to a le with the
.dot extension: ./wet http://kelen.polytechnique.fr/ 3 > mygraph.dot . The command
dot -Tgif -o mygraph.gif mygraph.dot builds a GIF le which you can display using the
command xv mygraph.gif .
Test your WET application by running
[./wet http://www.enseignement.polytechnique.fr/profs/informatique/Leo.Liberti/test.html
4] and verifying that the output is
# graphviz output by WET (L. Liberti 2006)
digraph www_1199989821 {
0 [ label = "www.enseignement.polytechnique.fr" ];
1 [ label = "www.enseignement.polytechnique.fr" ];
2 [ label = "www.enseignement.polytechnique.fr" ];
3 [ label = "Thu Jan 10 19:30:21 2008", color = red ];
0 -> 1;
1 -> 0;
1 -> 2;
2 -> 0;
2 -> 1;
0 -> 2;
}
By saving wets output as wetout.dot and running dot -Tgif -o wetout.gif wetout.dot we get
Fig. 5.3, left. Reducing the depth level to 2 yields Fig. 5.3, right.
www.enseignement.polytechnique.fr
www.enseignement.polytechnique.fr
www.enseignement.polytechnique.fr
Thu Jan 10 19:30:28 2008 www.enseignement.polytechnique.fr
www.enseignement.polytechnique.fr
www.enseignement.polytechnique.fr
Thu Jan 10 19:31:24 2008
Figure 5.3: The neighbourhoods graphs (depth 4, left; depth 2, right).
Putting it all together: main() 30
Chapter 6
Exam questions
This section contains a set of typical exam questions.
6.1 Debugging question 1
The following code causes some compilation errors. Find them and correct them.
// this code contains errors
#include <iostream>
#include <cassert>
int main(int argc, char** argv) {
int ret = 0;
if (argc < 2) {
cerr << "error: need an integer on command line";
return 1;
}
int theInt = atoi(argv[1]);
if (isPrime(theInt)) {
cout << theInt << "is a prime number!" >> endl;
} else {
cout << theInt << "is composite" << endl;
return ret;
}
bool isPrime (int num) {
assert(num >= 2);
if (num == 2) {
return true;
} else if (num % 2 == 0) {
return false;
} else {
bool prime = true;
int divisor = 3;
int upperLimit = sqrt(num) + 1;
while (divisor <= upperLimit) {
31
Exercises Introduction to C++ L. Liberti
if (num % divisor == 0) {
prime = false;
}
divisor +=2;
}
return prime;
}
}
What is the corrected program output when executed with ./debug1 1231 ?
6.2 Debugging question 2
The following code causes some compilation errors. Find them and correct them.
// this code contains errors
#include <iotsream>
class A {
A() {
std::cout << "constructing object of class A" << std::endl;
}
~A() {
std::cout << "destroying object of class A" << std::endl;
}
setDatum(int i) {
theDatum = i;
}
getDatum(void) {
return theDatum;
}
private:
int theDatum;
}
int main(int argc, char** argv) {
A a;
a.SetDatum(5);
std::cout << a.GetDatum() << std::endl;
return;
}
What is the corrected program output when executed with ./debug2 ?
6.3 Debugging question 3
The following code looks for a word in a text le and replaces it with another word:
// this code contains errors
Debugging question 3 32
Exercises Introduction to C++ L. Liberti
// [search and replace a word in a text file]
#include<iostream>
#include<fstream>
#include<string>
int main(int argc, char** argv) {
using namespace std;
if (argc < 4) {
cout << "need a filename, a word and its replacement on cmd line" << endl;
return 1;
}
ifstream ifs(argv[1]);
if (!ifs) {
cout << "cannot open file " << argv[1] << endl;
return 2;
}
string s;
while(!ifs.eof()) {
ifs >> s;
if (ifs.eof()) {
break;
}
if (s == argv[2]) {
cout << argv[3];
} else {
cout << s;
}
cout << " ";
}
cout << endl;
ifs.close();
return 0;
}
Test it on the folling le, called story.txt, with the command ./debug3 story.txt wolf teddy-bear :
This is just a stupid little story with no meaning whatsoever. The wolf
came out of his den and chased little red hood, found her and pounced on
her. Little red hood cried, "the wolf!", just stepped aside and the
wolf cracked his neck on the pavement, his brain in pulp.
She was to hear of him no more.
The output is:
This is just a stupid little story with no meaning whatsoever. The teddy-bear
came out of his den and chased little red hood, found her and pounced on her.
Little red hood cried, "the wolf!", just stepped aside and the
teddy-bear cracked his neck on the pavement, his brain in pulp. She was to
hear of him no more.
Note that one occurrence of the word wolf was not actually changed to teddy-bear. Fix this
problem.
Debugging question 3 33
Exercises Introduction to C++ L. Liberti
6.4 Debugging question 4
Compile and run the following code:
// if you change t with s in the snippet, the code has the same effect
#include<iostream>
#include<string>
std::string method(std::string& s) {
s = s.substr(1, s.npos);
return s;
}
int main(int argc, char** argv) {
using namespace std;
string s("ciao");
cout << s << endl;
while(true) {
string& t = s;
//////////// snippet ////////////////
t = method(t);
if (t.length() == 0) {
break;
}
cout << t << endl;
//////////// end snippet ////////////
}
return 0;
}
the output is:
ciao
iao
ao
o
Now examine the part of the code marked snippet: it has the curious property that if you replace any
occurrence of the t variable symbol with the s variable symbol, you still obtain the same output. Explain
why.
6.5 Debugging question 5
The following code contains a simple list implementation and a main() which creates a list, prints it,
nds a node and removes it, then prints the list again.
// this code contains errors
// [form and print a list of integers]
#include <iostream>
Debugging question 5 34
Exercises Introduction to C++ L. Liberti
class Node {
public:
// constructors / destructor
Node() : next(NULL), previous(NULL) { }
Node(int a) : next(NULL), previous(NULL), data(a) { }
~Node() {
if (next) {
delete next;
}
}
// set/get interface
void setData(int a) {
data = a;
}
int getData(void) {
return data;
}
void setNext(Node* theNext) {
next = theNext;
}
Node* getNext(void) {
return next;
}
void setPrevious(Node* thePrevious) {
previous = thePrevious;
}
Node* getPrevious(void) {
return previous;
}
// list capabilities
// return true if node is the first of the list, false otherwise
bool isFirst(void) {
return !previous;
}
// return true if node is the last of the list, false otherwise
bool isLast(void) {
return !next;
}
// return the size of the sublist starting from this node
int size(void) {
Node* t = this;
int ret = 1;
while(!t->isLast()) {
t = next;
ret++;
}
return ret;
}
Debugging question 5 35
Exercises Introduction to C++ L. Liberti
// append a new given node at the end of the list
void append(Node* theNext) {
Node* t = this;
while(!t->isLast()) {
t = next;
}
t->setNext(theNext);
theNext->setPrevious(t);
}
// create a new node with value a and append it at the end of the list
void appendNew(int a) {
Node* t = this;
while(!t->isLast()) {
t = next;
}
Node* theNewNode = new Node(a);
t->setNext(theNewNode);
theNewNode->setPrevious(t);
}
// remove this node from the list
void erase(void) {
previous->setNext(next);
next->setPrevious(previous);
}
// replace this node with a given node
void replaceWith(Node* replacement) {
previous->setNext(replacement);
next->setPrevious(replacement);
}
// find first node with a specified value in sublist starting from this node
// return NULL if not found
Node* find(int needle) {
if (data == needle) {
return this;
}
Node* t = this;
while(!t->isLast()) {
t = next;
if (t->getData() == needle) {
return t;
}
}
return NULL;
}
// print the data in the sublist starting from this node
void print(void) {
Node* t = this;
while(!t->isLast()) {
std::cout << t.getData() << ", ";
Debugging question 5 36
Exercises Introduction to C++ L. Liberti
t = next;
}
std::cout << t->getData() << std::endl;
}
protected:
// pointer to next node in list
Node* next;
// pointer to previous node in list
Node* previous;
private:
// the integer data stored in the node
int data;
};
int main(int argc, char** argv) {
using namespace std;
int c = 1;
// create a new list
Node start(c);
// add 10 nodes to the list
while(c < 10) {
c++;
start.appendNew(c);
}
// print the list
start.print();
// find the node with value 5
Node* t = start.find(5);
// erase this node
t->erase();
// print the list again
start.print();
return 0;
}
The code contains:
1. one compilation error;
2. some run-time errors;
3. a memory leak.
Correct the compilation error, then compile the code it should simply hang. Use the ddd debugger to
nd the run-time errors, nd all instances thereof, and correct them (remember to compile with the -g
option for debugging). Satisfy yourself that the code output is:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1, 2, 3, 4, 6, 7, 8, 9, 10
Debugging question 5 37
Exercises Introduction to C++ L. Liberti
Now use the valgrind memory debugger to check for memory leaks. Run the command
valgrind --leak-check=yes ./debug5
and using its output nd and x the error.
6.6 Debugging question 6
Compile the following code with cc -o schnitzi schnitzi.c and run it with ./schnitzi 36 to test
it, and try other numbers. Explain what it does and why.
#include<stdio.h>
main(l,i,I)char**i;{l/=!(l>(I=atoi(*++i))||fork()&&main(l+1,i-1)||I%l);
return printf("%d\n",l);}
6.7 Specication coding question 1
Write the implementation of the following SimpleString class.
/* name: simplestring.h
** author: L. Liberti
*/
#ifndef _SIMPLESTRINGH
#define _SIMPLESTRINGH
#include<iostream>
class SimpleString {
public:
// default constructor: initialise an empty string
SimpleString();
// initialise a string from an array of characters
SimpleString(char* initialiseFromArray);
// destroy a string
~SimpleString();
// return the size (in characters)
int size(void) const;
// return the i-th character (overloading of [] operator)
char& operator[](int i);
// copy from another string
void copyFrom(SimpleString& s);
Specication coding question 1 38
Exercises Introduction to C++ L. Liberti
// append a string to this
void append(SimpleString& suffix);
// does this string contain needle?
bool contains(SimpleString& needle) const;
private:
int theSize;
char* buffer;
int theAllocatedSize;
static const int theMinimalAllocationSize = 1024;
char theZeroChar;
};
std::ostream& operator<<(std::ostream& out, SimpleString& s);
#endif
Test it with with the following main program:
/* name: ssmain.cxx
author: Leo Liberti
*/
#include<iostream>
#include "simplestring.h"
int main(int argc, char** argv) {
using namespace std;
SimpleString s("Im ");
SimpleString t("very glad");
SimpleString u("so tired I could pop");
SimpleString v("very");
cout << s << endl;
cout << t << endl;
cout << u << endl << endl;
SimpleString w;
w.copyFrom(s);
s.append(t);
w.append(u);
cout << s << endl;
cout << w << endl << endl;
if (s.contains(v)) {
cout << "s contains very" << endl;
} else {
cout << "s does not contain very" << endl;
}
if (w.contains(v)) {
cout << "w contains very" << endl;
} else {
cout << "w does not contain very" << endl;
}
return 0;
Specication coding question 1 39
Exercises Introduction to C++ L. Liberti
}
and check that the output is as follows.
Im
very glad
so tired I could pop
Im very glad
Im so tired I could pop
s contains very
w does not contain very
Write also a makele for building the object le simplestring.o and the executable ssmain.
6.8 Specication coding question 2
Write the implementation of the following IntTree class (a tree data structure for storing integers).
/* Name: inttree.h
Author: Leo Liberti
*/
#ifndef _INTTREEH
#define _INTTREEH
#include<vector>
class IntTree {
public:
// constructs an empty tree
IntTree();
// destroys a tree
~IntTree();
// add a new subnode with value n
void addSubNode(int n);
// get the number of subtrees
int getNumberOfSubTrees(void);
// get a pointer to the i-th subtree
IntTree* getSubTree(int i);
// removes a subtree without deleting it (and returning it)
IntTree* removeSubTree(int i);
// adds a tree as a subtree of the current tree
void addSubTree(IntTree* t);
Specication coding question 2 40
Exercises Introduction to C++ L. Liberti
// get pointer to the parent tree
IntTree* getParent(void);
// set parent pointer
void setParent(IntTree* p);
// get value of current tree
int getValue(void);
// set value in current tree
void setValue(int n);
// find the first subtree containing given value by depth-first search
// and put its pointer in found
// PRECONDITION: found must be NULL on entry
// POSTCONDITION: if found is NULL on exit, value was not found
void findValue(int n, IntTree* &found);
// print a tree to standard output
void print(void);
protected:
// value stored in the node (tree)
int value;
// pointer to parent tree
IntTree* parent;
// vector of subtree pointers
std::vector<IntTree*> subTree;
private:
// iterator sometimes used in methods
std::vector<IntTree*>::iterator stit;
};
#endif
Test it with with the following main program:
/* name: itmain.cxx
author: Leo Liberti
*/
#include <iostream>
#include "inttree.h"
int print(IntTree& t) {
using namespace std;
cout << "T = ";
t.print();
Specication coding question 2 41
Exercises Introduction to C++ L. Liberti
cout << endl;
}
int main(int argc, char** argv) {
using namespace std;
IntTree myTree;
myTree.setValue(0);
myTree.addSubNode(1);
myTree.addSubNode(2);
myTree.addSubNode(3);
print(myTree);
int st = myTree.getNumberOfSubTrees();
for(int i = 0; i < st; i++) {
IntTree* t = myTree.getSubTree(i);
t->addSubNode(2*i + st);
t->addSubNode(2*i + 1 + st);
}
print(myTree);
IntTree* anotherTree = new IntTree;
anotherTree->setValue(-1);
anotherTree->addSubNode(-2);
anotherTree->addSubNode(-3);
IntTree* t = NULL;
myTree.findValue(2, t);
if (t) {
st = t->getNumberOfSubTrees();
IntTree* u = t->removeSubTree(st - 1);
print(myTree);
delete u;
t->addSubTree(anotherTree);
}
print(myTree);
myTree.findValue(-3, t);
int level = 1;
while(t != NULL) {
t = t->getParent();
level++;
}
cout << "node (-3) is at level " << level << " (root has level 1)" << endl;
return 0;
}
and check that the output is as follows.
T = 0 ( 1 2 3 )
T = 0 ( 1 ( 3 4 ) 2 ( 5 6 ) 3 ( 7 8 ) )
T = 0 ( 1 ( 3 4 ) 2 ( 5 ) 3 ( 7 8 ) )
T = 0 ( 1 ( 3 4 ) 2 ( 5 -1 ( -2 -3 ) ) 3 ( 7 8 ) )
node (-3) is at level 3 (root has level 1)
Specication coding question 2 42
Exercises Introduction to C++ L. Liberti
6.9 Task question 1
Write a program that prints its own (executable) le size in bytes and in number of printable characters
(a character char c is printable if (isalnum(c) > 0 || ispunct(c) > 0 || isspace(c) > 0)).
6.10 Task question 2
Write a program that prints the name of the image les referenced in the the web page http://www.
enseignement.polytechnique.fr/index.html. Image les are referenced in HTML both by the at-
tribute HREF of the tag A (e.g. <A REF="http://website.com/image.jpg">) and by the attribute SRC
of the tag IMG (e.g. <IMG SRC="/images/logo.gif">). The output should be as follows.
images/entete1.gif
images/remise.gif
images/amphi.gif
/icones/home.gif
6.11 Task question 3
Write a program that reads an mn matrix A = (a
ij
) from a text le with m lines is a list of n double
entries separated by a space, then output the homogeneous system Ax = 0 in explicit algebraic form with
m lines of text having the following format:
a
i1
* x[1] + . . . a
in
* x[n].
The name of the le containing the matrix and the integer n should be read from the command line.
For example, for the text le
1 -2.3 3 0.0
-9.123 2 -50 -1
you should obtain the following output:
x[1] - 2.3*x[2] + 3*x[3] = 0
-9.123*x[1] + 2*x[2] - 50*x[3] - x[4] = 0
Hint: if you have a le stream ifs and you want to read a double value o it, use the following
construct.
double t;
ifs >> t;
Task question 3 43
Exercises Introduction to C++ L. Liberti
Task question 3 44
Chapter 7
Questions from a banking C++ test
In this chapter we group some questions taken from a real online C++ test used by banks worldwide.
You have 60 seconds to answer each multiple choice question.
7.1 Question 1
Code:
char a [] = "ABCD\r\n";
Question: How many elements will be in the array created by the declaration in the sample above?
1. 6
2. 7
3. 8
4. 9
5. 4
7.2 Question 2
Question: Which of the following statements is true when a derivation inherits both a virtual and
non-virtual instance of a base class?
1. Each base class object has derived objects only from the non-virtual instance.
2. Each base class object has derived objects only from the virtual instance.
3. Each derived class object has base objects only from the virtual instance.
4. Each derived class object has base objects only from the non-virtual instance.
5. Each derived class object has a base object from the virtual instance and a base object from non-
virtual instance.
45
Exercises Introduction to C++ L. Liberti
7.3 Question 3
Code:
string somestring ;
Question: Which of the following choices will convert a standard C++ string object somestring
to a C string?
1. Copy.somestring () ;
2. somestring.c str ()
3. std::cstring (somestring)
4. (char *) somestring
5. &somestring [1]
7.4 Question 4
Code:
#include <iostream>
Question: Referring to the sample code above, which of the following could you use to make the standard
I/O stream classes accessible without requiring the scope resolution operator?
1. using iostream ;
2. using namespace iostream ;
3. using namespace std ;
4. using namespace std::iostream ;
5. using std::iostream ;
7.5 Question 5
Code:
class basex {
int x;
public:
void setx(int y) {x=y;}
};
class derived : basex {};
Question: What is the access level for the member function setx in the class derived above?
Question 5 46
Exercises Introduction to C++ L. Liberti
1. protected
2. public
3. local
4. private
5. global
Question 5 47