Module Outline
Introduction Inheritance
The non object Aggregation
oriented basics Polymorphism
Classes Multifile Development
Design Approaches
Testing
1
Historical Notes
C++ owes most to C.
Other ancestors are Simula67
and Algol68. C++ 1987
First versions of C++ in 1980 under the
name “C with classes”. Since 1983 the
name C++ is used.
1990: ANSI/ISO 9899 defines a standard
for C
1998: ISO/IEC 14882 specifies the
standard for C++ 2
C++ and C
C is a subset of C++.
Advantages: Existing C libraries can be
used, efficient code can be generated.
But: C++ has the same caveats and
problems as C (e.g. pointer arithmetic,…).
C++ can be used both as a low level and
as a high level language.
3
C++ and Java
Java is a full object oriented language, all
code has to go into classes.
C++ - in contrast - is a hybrid language,
capable both of functional and object
oriented programming.
So, C++ is more powerful but also
more difficult to handle than Java.
4
Today:
Extensive analysis of an example
program.
Data types, operators, functions and I/O.
Arrays, strings, pointers
Control structures.
5
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
int main()
{
int p = 7;
doSomething(p);
cout << “I have something done.” << endl;
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h> The header of the file. Should
void doSomething(int p);contain general information
int main()
as file name, author,
{ description, etc.
int p = 7;
doSomething(p); The compiler ignores these
cout << “I have something done.” << endl;
return 0; lines (see next slide).
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h> This is a C++ comment.
void doSomething(int p);
Lines starting with //
int main() are ignored by the compiler.
{
int p = 7; Also ignored is text enclosed
doSomething(p);
by /*done.”
cout << “I have something and */<< endl;
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p); A pre-processor directive.
int main() Lines starting with a # are
{ interpreted by a pre-
int p = 7;
doSomething(p); processor before the compiler
processes
cout << “I have something theendl;
done.” << file.
return 0;
} Other important directives are
void doSomething(int p) {#define, #ifdef, #endif,
for( int i = 0; i < p; i++#pragma,
){ ...
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
In this example we include
void doSomething(int p);
the file iostream.h into the
int main() program. iostream.h defines
{
int p = 7; classes and objects related to
doSomething(p); input and output.
cout << “I have something done.” << endl;
return 0; We need it here because
}
cout is declared there.
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
Observation:
void doSomething(int p);
int main()
cout is not a keyword of C++
{ but an identifier defined in
int p = 7; the iostream library.
doSomething(p);
cout << “I have something done.” << endl;
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
int main()
{ A function prototype.
int p = 7;
In this line the compiler is told
doSomething(p);
cout << “I have something
about done.” with
a function << endl;
the name
return 0;
} doSomething and its signature.
void doSomething(intThe p)
function
{ here has one
for( int i = 0; i parameter
< p; i++ ) { (int) and no return value
cout << “*” << endl;
} (void).
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
int main()
{ Also global variables and class
int p = 7; declarations usually come here.
doSomething(p);
cout << “I have something done.” << endl;
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
The main function is the entry point
int main()
{ of a C++ program. Each C++
int p = 7; program must have exactly one
doSomething(p);
cout << “I havemain() method.
something done.” << endl;
return 0;
} The return value of main() is an int.
This value is passed to the system
void doSomething(int p) {
which
for( int i = 0; i < p; i++invoked
){ the C++ program.
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
The code which implements the
int main()
{ main function is enclosed in { and
int p = 7; }.
doSomething(p);
cout << “I haveStatements
something done.” << endl;
enclosed in { and } are
return 0;
} called a block.
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
We declare a variable p of type int.
int main()
{ Other important data types of C++
int p = 7;
doSomething(p);are char, unsigned int, long,
cout << “I haveunsigned
somethinglong,
done.”float,
<< endl;
double, bool.
return 0;
} The variable p is immediately
void doSomething(intinitialised
p) { with the value 7.
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
The function
// Title: The program doing somethingdoSomething() is
#include <iostream.h>called with the parameter p.
void doSomething(int p);
int main()
{
int p = 7;
doSomething(p);
cout << “I have something done.” << endl;
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp This statement prints the string
// Title: The program doing something
“I have something done.” to the
#include <iostream.h>screen.
void doSomething(int p);
The “stream manipulator” endl
int main() outputs a newline and then
{ “flushes the output buffer”.
int p = 7;
doSomething(p);
cout << “I have something done.” << endl;
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp Functions with a return value
// Title: The program doing
whichsomething
is not void use the return
#include <iostream.h>keyword in order to return their
value to the calling function.
void doSomething(int p);
int main() In the special situation here, the
{ main() method has no calling
int p = 7;
doSomething(p); function. The value 0 is passed
back todone.”
cout << “I have something system
<< when
endl; the
return 0; program is finished. Usually 0
}
means that the program worked
void doSomething(int p) {
correctly.
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
int main()
{ The implementation of the
int p = 7;
doSomething(p); previously defined function
cout << “I have something done.” << endl;
doSomething.
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
A typical C++ program
// FileID: hello.cpp
// Title: The program doing something
#include <iostream.h>
void doSomething(int p);
int main()
{
int p = 7;
doSomething(p);
cout << “I have something done.” << endl;
return 0;
}
void doSomething(int p) {
for( int i = 0; i < p; i++ ) {
cout << “*” << endl;
}
}
Basics of C++ - data types
Some Fundamental data types:
char characters: ’a’, ’b’, ’\n’, ’\0’, ’7’
int integers: 3, 6883, -5, 0
double floating point numbers: 3.14, 7e9
bool true or false.
Also: float, long, unsigned long, short, unsigned
char, wchar_t
22
Basics of C++ - variables
Declaring variables in a program:
char a;
int b;
double c;
Assignment:
b = 4; a = 'w’; c = -3.777;
int x = 78;
23
Basics of C++ - variables
Constants:
const double PI=3.1415926;
const int MAXBUFFER=20;
Note: the const keyword is also used for
method parameters, methods and return
values (later)
24
Basics of C++ - operators
Arithmetic operators: Bitwise:
+, -, *, /, % &, |, ~,^
Comparison: Shortcuts:
==, !=, <, >, >=, <= +=, *=, ^=, (etc.)
Logical: Other:
&&, ||, ! <<, >>, ? :, ->, ., ,
Assignment:
=
25
Basics of C++ - operators
The unary operators ++ and --:
++ increment by 1
-- decrement by 1
The language C++ got its name by this
operator!
Note, that i++ and ++i have different
behaviour […] 26
Basics of C++ - functions
Name
int someFunction(double f, char c)
{
// … Body
} Parameter
List
Return Type
27
Basics of C++ - functions
Please note, that a function is specified by
the name and the parameter types. The
following functions are all different:
int exampleFunction(int i, char c);
int exampleFunction(double f);
int exampleFunction();
int exampleFunction(char c, int i);
28
Basics of C++ - functions
Pass by reference, example:
void square(int &v) { v = v * v; }
The parameter
v is not copied.
In contrast, pass by value:
int square(int v) { return v * v; }
29
Basics of C++: I/O
For output use cout, e.g.
cout << “The result is: “ << result << endl;
For input use cin, e.g.
cin >> x;
Note that iostream.h must be included via
#include <iostream.h>
30
Basics of C++ - arrays
Declaration:
int numbers[10];
Declaration & Initialisation:
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 };
Access:
numbers[6] = 2483;
cout << “The fourth prime is “ <<
primes[4];
31
Basics of C++ - Strings
There aren’t any strings in C++.
32
Basics of C++ - Strings
There aren’t any strings in C++.
O.k., that’s only half of the truth. In fact, by
convention, strings are represented in C++ as ’\0’
terminated arrays of characters. In addition, the
file string.h declares useful string manipulating
functions, e.g. strcpy, strlen which deal with ’\0’
terminated character arrays.
33
Basics of C++ - pointer
A pointer points to a memory location
which contains data of a particular type.
The contents of a pointer is the address of
some data
Declaration:
int *p;
double *aDoublePointer;
34
Basics of C++ - pointer
In C++ pointer and arrays are strongly
related (“array = pointer + memory”).
int primes[] = {2, 3, 5, 7, 11 }; pointer
int *aPr = primes; arithmetic
aPr++;
cout << “The third prime is “ << *(aPr + 2);
The * operator accesses the
data on the memory address 35
Control Structures -
Decisions
The if statement:
if ( x > 0 ) {
cout << “positive”;
} else {
cout << “negative or zero”;
}
36
Control Structures -
Decisions
The switch statement - example:
int x;
cout << "Enter choice (1, 2, or 3)";
cin >> x;
switch(x) {
case 1: doThis(); break;
case 2: doThat(); break;
case 3: doSomethingElse(); break;
default: cout << "Sorry, invalid Input";
} 37
Control Structures -
Iteration
The for loop:
Terminating condition
Start condition
for(k = 0; k < 10; k++ ) {
cout << “The square of “ << k << “ is “
<< k * k << endl;
}
Action taking place at
the end of each iteration38
Control Structures -
Iteration
The while loop:
while ( condition ) {
// do something
}
Equivalent to:
for( ; condition ; ) {
// do something
}
39
Control structures -
do … while
The do … while loop:
do {
// something Equivalent to:
} while( condition); // something
while( condition) {
// something
}
40
Control Structures
And finally:
Yes, C++ has a goto.
Don’t use it.
41