Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
TI00AA50
Lecture 1 - 10.09.2015
Jarkko.Vuori@metropolia.fi
Motivation
entertainment software
video games
device drivers
operating systems
server side programming
embedded software
scientific computing
TI00AA50/JV
General information
Youll get to the WinhaWille system from the Metropolia web pages (www.metropolia.fi)
The identification code of the course is TI00AA50
You need to enroll the course in order to get the course material from the Tuubi
Understanding principles of object oriented programming, and how to apply them when
designing and implementing programs
Differences to Java programming language are discussed (e.g. memory management,
pointers when using objects)
Professional Skills
TI00AA50/JV
General information
Prerequisites:
You have to fill the request (resit envelope) 10 working days before the resit exam
You can go to two resit exams at maximum, e.g. you can try only two times to pass the exam (after the courses main exam)
This course belongs to the resit exam group A1
Optional books (not mandatory, but for those more interested in C++)
Useful links
All the course material (lecture slides, lab works, example program files, extra material) are available from the Tuubi (tube.metropolia.fi)
TI00AA50/JV
General information
TI00AA50/JV
Timetable
Lecture
Topic
Operator overloading
String class
Basics of inheritance
10
11
12
Exceptions
13
C++ and C++11 smart pointers, right value references, move semantics
TI00AA50/JV
What is ++ in C?
C++
Function overloading
Operator overloading
Default parameters
Reference parameters
Inline-functions
Etc.
TI00AA50/JV
C-language
Function prototype void f();
Nothing is determined about parameters
So you can call it for example in the
following ways:
f(3), f(1.1, 2.2) and f()
C++-language
Function prototype void f();
Function has no parameters.
So the only possible way to call it is in the
form f();
This is same than void f(void)
C- and C++-language
Function prototype void f(void);
Function has no parameters.
So the only possible way to call it is in the
form f();
C++-language
Function prototype void f(...);
Nothing is determined about parameters.
So you can call it for example in the
following ways:
f(3), f(1.1, 2.2) and f()
TI00AA50/JV
Function overloading
Lets assume that we have two types Tpoint and Ttriangle and we need the functions that read
information into variables of these types. These types and functions are used in the same program.
C-language
We need different names for the functions:
void read_point(Tpoint *point);
void read_triangle(Ttriangle
*triangle);
int main (void) {
Tpoint p;
Ttriangle t;
C++-language
We can use function overloading and define
several functions that all have the same
name:
void read(Tpoint *point);
void read(Ttriangle *triangle);
int main (void) {
Tpoint p;
Ttriangle t;
read_point(&p);
read_triangle(&t);
...
read(&p);
read(&t);
...
}
}
TI00AA50/JV
Operator overloading
Lets assume that we want to implement an ADT for complex number calculations
C-language
We could write an ADT that we could use in
the following way:
#include "complex.h"
void main(void) {
Tcomplex a, b, c;
read_complex(&a);
read_complex(&b);
c = sum_complex(a,b);
print_complex(c);
C++-language
Now we can use operator overloading in the
implementation of the ADT. This means that
we can do input, output and addition of
complex numbers in the way we are used to
use for other numbers (ints or floats). This
means that our main program becomes
generic.
#include "complex.h"
void main(void) {
Tcomplex a, b, c;
}
cin >> a;
cin >> b;
c = a + b;
cout << c;
}
TI00AA50/JV
10
Default parameters
(We now assume that x is a column number and y is a row number)
// function prototype
void outputxy(const char *text, int x=-1, int y=-1);
// How to use the function in the main function
outputxy("C-language", 35, 3);
// output goes to column 35 at row 3
outputxy("C++-language");
// the new text follows the previous one
outputxy("Java-language", 10);
// the output goes to column 10 at the current row
// The implementation of the function
void outputxy(const char *text, int x, int y) {
if (x==-1) x = wherex();
if (y==-1) y = wherey();
gotoxy(x,y);
cout << text;
}
TI00AA50/JV
11
int main( ) {
bool rich;
int money = 20000;
rich = money > 10000;
// or rich = true;
if (rich) // if (rich==true)
spend_money();
return 0;
}
return 0;
}
TI00AA50/JV
12
TI00AA50/JV
13
Reference parameters
// prototype of swap
void swap(int *a, int *b);
// prototype of swap
void swap(int &a, int &b);
// application
void main(void) {
in number1 = 1, number2 = 2;
// application
void main(void) {
in number1 = 1, number2 = 2;
swap(&number1, &number2);
// implementation of swap
void swap(int *a, int *b) {
int aux;
aux = *a;
*a = *b;
*b = aux;
}
// implementation of swap
void swap(int &a, int &b) {
int aux;
aux = a;
//deferencing is used internally
a = b;
//with a and b
b = aux;
}
TI00AA50/JV
14
Returning a reference
// prototype of a function
int *getMax(int *arr, int n);
// prototype of a function
int &getMax(int *arr, int n);
// application
int main(void) {
int maxValue;
int array[6] = {3, 5, 2, 8, 6, 7};
printf ("%d", *getMax(array, 6));
//Right value
maxValue = *getMax(array, 6);
*getMax(array, 6) = *getMax(array, 6)*10;
//Left value
// application
int main(void) {
int maxValue;
int array[6] = {3, 5, 2, 8, 6, 7};
printf ("%d", getMax(array, 6));
//Right value
maxValue = getMax(array, 6);
getMax(array, 6) = getMax(array, 6)*10;
//Left value
// implementation getMax
int *getMax(int *arr, int n) {
int i, maxi = 0;
for (i = 1; i < n; i++)
if (arr[i] > arr[maxi])
maxi = i;
return &arr[maxi];
}
// implementation getMax
int &getMax(int *arr, int n) {
int i, maxi = 0;
for (i = 1; i < n; i++)
if (arr [i] > arr[maxi])
maxi = i;
return arr[maxi];
}
TI00AA50/JV
15
Pointer parameters
// prototype of swap
// prototype of swap
// application
int main(void) {
in number1 = 1, number2 = 2;
// application
int main(void) {
in number1 = 1, number2 = 2;
swap(number1, number2);
swap(&number1, &number2);
// implementation of swap
void swap(int a, int b) {
int aux;
aux = a;
//the contents are changed in
here
a = b;
b = aux;
}
// implementation of swap
void swap(int *a, int *b) {
int aux;
aux = *a; //the contents are changed
in the main
*a = *b;
*b = aux;
}
TI00AA50/JV
16
C++
Simple variable
Simple variable
int *p;
p = (int*)malloc(sizeof(int));
*p = 7;
...
free(p);
int *p;
p = new int;
*p = 7;
// or p = new int (7);
...
delete p;
Array
Array
int *a;
a = (int*)malloc(n*sizeof(int));
a[0] = 1; a[1] = 2;
...
free(a);
int *a;
a = new int [n];
a[0] = 1; a[1] = 2;
...
delete a;
TI00AA50/JV
17
Input/Output
Input object cin is an instance of
the class istream and output
object cout is an instance of the
class ostream
Input/Output
Input and output objects cin and
cout are ready-to-use objects
(they are instances of classes,
declared in the system)
float a;
cin >> a;
//
//
//
cout << a; //
//
//
TI00AA50/JV
18
More differences 1
Initialization of a variable
Comments
C-language:
C++-language:
Initialization and assignment can
be done as above
In C++ there is also another way
to initialize a variable:
TI00AA50/JV
Headers
19
More differences 2
Name spaces
All names (identifiers) that belong to C++ are defined in the namespace std
Using names in the namespace is possible only by specifying also the namespace. There
are two options to do that.
Option 1. You can specify the namespace every time you use the name:
Option 2. You and tell in the beginning of the program what namespaces you are going to use:
In large software projects there is a big probability that different modules want to use similar
names, thus we have the concept of (separate) namespaces
C++-language:
The prototype of the function is required before the function call. Otherwise you get an error message from the
compiler
C-language:
C-compiler compiles the function call even it has no prototype. The compiler cannot check whether you have
called the function in the correct way. The compiled program can work in totally wrong way, if you have called it
in wrong way (parameter types are not correct for example).
TI00AA50/JV
20
More differences 3
The basic idea of these operators
is to indicate the purpose of the
cast
Dynamic cast also does the
runtime check whether the type of
the object to be cast is
compatible with the new type
static_cast
reinterpret_cast
const_cast
dynamic_cast
// From C
// Same thing in
// the new way in C++
TI00AA50/JV
21