informatik 1 woche 12 - eth z · self-assessment • gibt euch rückmeldung über euren...
TRANSCRIPT
Informatik 1Woche 12Moritz Schneider
Moritz Schneider 23. Mai 2019 1
Heute:
1. Self Assesment
2. Repetition: Classes
3. Repetition: Const Typen
4. Memory Management
5. Optional Exercise
Moritz Schneider 23. Mai 2019 2
Übungen
Hat alles geklappt?
Moritz Schneider 23. Mai 2019 3
Self-Assessment
• Gibt euch Rückmeldung über euren Fortschritt.
• Er testet nicht, wie gut Ihr bis jetzt in den Vorlesungen aufgepasst habt.
• Die Teilnahme ist komplett freiwillig, aber stark empfohlen.
• Die erzielte Note zählt in keiner Weise mit zur Endnote!
• Er wird unmittelbar im Anschluss zusammen korrigiert.
• Zeit: 20 Minuten
Moritz Schneider 23. Mai 2019 4
Repetition
Repetition
Moritz Schneider 23. Mai 2019 5
Classes I
• Definiert Interface für Problemlösung
• Erlaubt Modulisierung / Abstraktion
• Optional Dokumentation
• Details der Implementierung werden versteckt• Beispiel: Physischer Taschenrechner
– Interface: Tasten– Konzept: Mathematische Operationen– Dokumentation: Handbuch– Interne Details müssen nicht bekannt sein um ihn zu benützen
Moritz Schneider 23. Mai 2019 6
Classes II - Deklaration
• Interface Spezifikation
• Immer im “.h” File
• Aufgeteilt in private und public• Enthält:
– State (Variabeln)– Methoden (Funktionen)
class Complex {private :
double r, i;public :
// Konstruktor 1Complex ( double r, double i);// Konstruktor 2Complex () {
this ->r = 0;this ->i = 0;
}void print ();void add( Complex c);
};
Moritz Schneider 23. Mai 2019 7
Classes III - Definition
• Details der Implementierung
• Meistens im “.cpp” File
• Immer Klassenname vor Methoden
void Complex :: add( Complex c ){..}
• normale Funktionen gleich wie bisher:
void print( Complex c){..}
#include "complex.h"
// Klassen -Konstruktor 1Complex :: Complex(double r, double i) {
this ->r = r;this ->i = i;
}
// Normale Funktion (sichtbar im cpp file)std:: string double_str(double d) {
return std:: to_string(d);}
// Public Klassen -Methodevoid Complex :: print() {
std::cout << "["<< double_str(this ->r) << ","<< double_str(this ->i) << "]";
}
Moritz Schneider 23. Mai 2019 8
Classes IV - Instanzen
• Beispiel:Complex c1(0, 0);Complex c2(0, 0);
• print muss auf verschiedene rund i zugreifen.
• Das wird mit this gemacht!
Beispiel: c2.print ()...
c1.r &c1
c1.ic1
...
c2.r &c2
c2.ic2
...
Moritz Schneider 23. Mai 2019 9
Classes IV - Instanzen
• Beispiel:Complex c1(0, 0);Complex c2(0, 0);
• print muss auf verschiedene rund i zugreifen.
• Das wird mit this gemacht!
Beispiel: c2.print ()...
c1.r &c1
c1.ic1
...
this->r &c2this
this->ic2
...
Moritz Schneider 23. Mai 2019 10
Const
const für Pointer:
• const int a = 5; // immutable int
• const int* a; // pointer to a constant int
• int const* a; // pointer to a constant int
• int* const a; // constant pointer to mutable int
• const int* const a; // constant pointer to const int
• int const* const a; // constant pointer to const int
Moritz Schneider 23. Mai 2019 11
Memory Management
class Cell{private:
int value;public:
Cell() {value = 0;std::cout << "c␣";
}Cell(int val) {
value = val;std::cout << "oc␣";
}}
void step1() {Cell c1;Cell *c2 = new Cell (2);std::cout << "end␣"
}void step2() {
Cell c(3):Cell c_copy = c;std::cout << "end␣"
}void step3() {
Cell c(42);Cell c_copy;c_copy = c;std::cout << "end␣"
}int main() {
step1 (); // "c oc end "step2 (); // "oc end "step3 (); // "oc c end "
}
Moritz Schneider 23. Mai 2019 12
Memory Management II
class Cell{private:
int value;public:
Cell() {value = 0;std::cout << "c␣";
}Cell(int val) {
value = val;std::cout << "oc␣";
}// Note the Destructor~Cell() {
std::cout << "d␣";}
}
void step1() {Cell c1;Cell *c2 = new Cell (2);std::cout << "end␣"
}void step2() {
Cell c(3):Cell c_copy = c; // Cell c_copy = Cell(c);std::cout << "end␣"
}void step3() {
Cell c(42);Cell c_copy;c_copy = c;std::cout << "end␣"
}int main() {
step2 (); // "oc end d d "step3 (); // "oc c end d d "step1 (); // "c oc end d "
}
Moritz Schneider 23. Mai 2019 13
Memory Management IIIclass Cell{
private:int value;
public:Cell() {
value = 0;std::cout << "c␣";
}Cell(int val) {
value = val;std::cout << "oc␣";
}// Note the Copy ContructorCell(Cell &b) { // requires reference
this ->value = b.value;std::cout << "cc␣";
}~Cell() {
std::cout << "d␣";}
}
void step1() {Cell c1;Cell *c2 = new Cell (2);std::cout << "end␣"
}void step2() {
Cell c(3):Cell c_copy = c; // Cell c_copy = Cell(c)std::cout << "end␣"
}void step3() {
Cell c(42);Cell c_copy;c_copy = c;std::cout << "end␣"
}int main() {
step1 (); // "c oc end d "step2 (); // "oc cc end d d "step3 (); // "oc c end d d"
}
Moritz Schneider 23. Mai 2019 14
Memory Management IVclass Cell{
private:int value;
public:Cell() {
value = 0;std::cout << "c␣";
}Cell(int val) {
value = val;std::cout << "oc␣";
}Cell(Cell &b) {
this ->value = b.value;std::cout << "cc␣";
}~Cell() {
std::cout << "d␣";}Cell& operator =(Cell &b) {
this ->value = b.value;std::cout << "a␣";return *this;
}}
void step1() {Cell c1;Cell *c2 = new Cell (2);std::cout << "end␣"
}void step2() {
Cell c(3):Cell c_copy = c; // Cell c_copy = Cell(c)std::cout << "end␣"
}void step3() {
Cell c(42);Cell c_copy;c_copy = c;std::cout << "end␣"
}int main() {
step1 (); // "c oc end d "step2 (); // "oc cc end d d "step3 (); // "oc c a end d d"
}Moritz Schneider 23. Mai 2019 15
Speicher Freigeben
• Speicher, der mit new alloziert wurde, muss manuell “gelöscht” werden
• Wird mit delete gemacht
• Beispiel:
int *a = new int ();// do something with adelete a;
• Muss auch mit Arrays gemacht werden:
int *a = new int ()[5];// do something with adelete [] a;
Moritz Schneider 23. Mai 2019 16
Muddiest Point
Welche C++ Konzepte sind noch nicht klar?
Moritz Schneider 23. Mai 2019 17
Find mistakes in the following code and suggest fixes:
1 // PRE: len is the length of the memory block that starts at array
2 void test1(int* array, int len) {
3 int* fourth = array + 3;
4 if (len > 3) {
5 std::cout << *fourth << std::endl;
6 }
7 for (int* p = array; p != array + len; ++p) {
8 std::cout << *p << std::endl;
9 }
10 }
1
Find mistakes in the following code and suggest fixes:
1 // PRE: len is the length of the memory block that starts at array
2 void test1(int* array, int len) {
3 //int* fourth = array + 3; // ERROR
4 if (len > 3) {
5 int* fourth = array + 3; // OK
6 std::cout << *fourth << std::endl;
7 }
8 for (int* p = array; p != array + len; ++p) {
9 std::cout << *p << std::endl;
10 }
11 }
Even if the pointer is not dereferenced, it must point into a memory block or to theelement just after its end.
2
Find mistakes in the following code and suggest fixes:
1 // PRE: len >= 2
2 int* fib(unsigned int len) {
3 int* array = new int[len];
4 array[0] = 0; array[1] = 1;
5 for (int* p = array+2; p < array + len; ++p) {
6 *p = *(p-2) + *(p-1); }
7 return array; }
8 void print(int* array, int len) {
9 for (int* p = array+2; p < array + len; ++p) {
10 std::cout << *p << " ";
11 }
12 }
13 void test2(unsigned int len) {
14 int* array = fib(len);
15 print(array, len);
16 }
3
1 // PRE: len >= 2
2 int* fib(unsigned int len) {
3 int* array = new int[len];
4 array[0] = 0; array[1] = 1;
5 for (int* p = array+2; p < array + len; ++p) {
6 *p = *(p-2) + *(p-1); }
7 return array; }
8 void print(int* array, int len) {
9 for (int* p = array+2; p < array + len; ++p) {
10 std::cout << *p << " ";
11 }
12 }
13 void test2(unsigned int len) {
14 int* array = fib(len);
15 print(array, len);
16 } // array is leaked; to fix add: delete [] array
4
Find mistakes in the following code and suggest fixes:
1 // PRE: len >= 2
2 int* fib(unsigned int len) {
3 // ...
4 }
5 void print(int* m, int len) {
6 for (int* p = m+2; p < m + len; ++p) {
7 std::cout << *p << " ";
8 }
9 delete m;
10 }
11 void test2(unsigned int len) {
12 int* array = fib(len);
13 print(array, len);
14 delete [] array;
15 }
5
1 // PRE: len >= 2
2 int* fib(unsigned int len) {
3 // ...
4 }
5 void print(int* m, int len) {
6 for (int* p = m+2; p < m + len; ++p) {
7 std::cout << *p << " ";
8 }
9 delete m; // should be delete []
10 }
11 void test2(unsigned int len) {
12 int* array = fib(len);
13 print(array, len);
14 delete [] array; // array deallocated twice
15 }
6
Fragen?
Moritz Schneider 23. Mai 2019 18