include cstdio using namespace std class Human private

  • Slides: 92
Download presentation

Παράδειγμα κλάσης #include <cstdio> using namespace std; class Human { private: int height; int

Παράδειγμα κλάσης #include <cstdio> using namespace std; class Human { private: int height; int age; public: void Ages(); void Is. Born(); void Grows(int inc); }; void Human: : Ages(){ age += 1; } void Human: : Is. Born(){ height = 40; age = 0; } void Human: : Grows(int inc){ height += inc; }

Αντικείμενα int main() { Ορισμός του αντικειμένου My. Class my. Class. Instance; . .

Αντικείμενα int main() { Ορισμός του αντικειμένου My. Class my. Class. Instance; . . . my. Class. Instance. public. Method(); Κλήση της public μεθόδου. . } int main() { Human peter; peter. Is. Born(); peter. Ages(); peter. Grows(10); }

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move();

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move(); int Get. Position(); };

Methods void Car: : Initialize. Position() { _pos = 0; } #include <cmath> #include

Methods void Car: : Initialize. Position() { _pos = 0; } #include <cmath> #include <cstdlib> void Car: : Move() { _pos += floor((double(rand())/RAND_MAX)*3)-1; } int Car: : Get. Position() { return _pos; }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int counter = 0; // Αρχικοποίηση αυτοκινήτων while(!collision){ // Τα αυτοκίνητα μετακινούνται // check for collision counter ++; } cout << "Cars collided after " << counter <<" moves" // τύπωσε την θέση της σύγκρουσης }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int counter = 0; Car car. X; Car car. Y; // Αρχικοποίηση αυτοκινήτων while(!collision){ // Τα αυτοκίνητα μετακινούνται // check for collision counter ++; } cout << "Cars collided after " << counter <<" moves" // τύπωσε την θέση της σύγκρουσης }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int counter = 0; Car car. X; Car car. Y; car. X. Initialize. Position(); car. Y. Initialize. Position(); while(!collision){ // Τα αυτοκίνητα μετακινούνται // check for collision counter ++; } cout << "Cars collided after " << counter <<" moves" // τύπωσε την θέση της σύγκρουσης }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int counter = 0; Car car. X; Car car. Y; car. X. Initialize. Position(); car. Y. Initialize. Position(); while(!collision){ car. X. Move(); car. Y. Move(); collision = (car. X. Get. Position() == car. Y. Get. Position()); counter ++; } cout << "Cars collided after " << counter <<" moves" // τύπωσε την θέση της σύγκρουσης }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int counter = 0; Car car. X; Car car. Y; car. X. Initialize. Position(); car. Y. Initialize. Position(); while(!collision){ car. X. Move(); car. Y. Move(); collision = (car. X. Get. Position() == car. Y. Get. Position()); counter ++; } cout << "Cars collided after " << counter <<" moves " << “at position “ << car. X. Get. Position() << endl; }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int counter = 0; Car car. X; Car car. Y; Τι γίνεται αν θέλουμε να αλλάξουμε τον ορισμό της σύγκρουσης? car. X. Initialize. Position(); car. Y. Initialize. Position(); while(!collision){ car. X. Move(); car. Y. Move(); collision = (car. X. Get. Position() == car. Y. Get. Position()); counter ++; } cout << "Cars collided after " << counter <<" moves " << “at position “ << car. X. Get. Position() << endl; }

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move();

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move(); int Get. Position(); int Collide(Car) };

int Car: : Collide(Car other) { return (_pos == other. Get. Position()); }

int Car: : Collide(Car other) { return (_pos == other. Get. Position()); }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int counter = 0; Car car. X; Car car. Y; car. X. Initialize. Position(); car. Y. Initialize. Position(); while(!collision){ car. X. Move(); car. Y. Move(); collision = car. X. Collide(car. Y); counter ++; } cout << "Cars collided after " << counter <<" moves " << “at position “ << car. X. Get. Position() << endl; }

class Mine { private: int _pos; Class Mine public: void Activate(); int Get. Position();

class Mine { private: int _pos; Class Mine public: void Activate(); int Get. Position(); }; void Mine: : Activate() { _pos = floor((double(rand())/RAND_MAX)*11)-5; } int Mine: : Get. Position() { return _pos; }

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move();

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move(); int Get. Position(); int Collide(Car) int Detonate(Mine); };

int Car: : Detonate(Mine some. Mine) { return (_pos == some. Mine. Get. Position());

int Car: : Detonate(Mine some. Mine) { return (_pos == some. Mine. Get. Position()); }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { int collision = 0; int explosion = 0; int counter = 0; Car car. X; Car car. Y; Mine random. Mine; car. X. Initialize. Position(); car. Y. Initialize. Position(); while(!collision && !explosion){ car. X. Move(); car. Y. Move(); random. Mine. Activate(); collision = car. X. Collide(car. Y); explosion = car. X. detonate(random. Mine) || car. Y. detonate(random. Mine); counter ++; } if (collision){ cout << "Cars collided after " << counter <<" moves “ << “at position “ << car. X. Get. Position() << endl; } if (explosion){ cout << “Mine exploded after “ << counter << “ moves, “ << “at position “ << random. Mine. Get. Position() << endl; } }

Είσοδος / Έξοδος δεδομένων //A Simple C++ program #include <iostream> using namespace std; main(){

Είσοδος / Έξοδος δεδομένων //A Simple C++ program #include <iostream> using namespace std; main(){ cout << “Hello world!n”; } Ισοδύναμα: cout << “Hello” << “world!n”;

Είσοδος / Έξοδος δεδομένων #include <iostream> using namespace std; int main(){ int i; cin

Είσοδος / Έξοδος δεδομένων #include <iostream> using namespace std; int main(){ int i; cin >> i; float f; cin >> f; char c; cin >> c; char buf[100]; cin >> buf; } cin >> i >> f >> buf;

Είσοδος / Έξοδος δεδομένων #include <iostream> using namespace std; int main(){ int i; cin

Είσοδος / Έξοδος δεδομένων #include <iostream> using namespace std; int main(){ int i; cin >> i; cout << "i = "; cout << i; cout << "n"; float f; cin >> f; cout << "f = "; cout << f; cout << "n"; }

Είσοδος / Έξοδος δεδομένων #include <iostream> using namespace std; int main(){ int i; float

Είσοδος / Έξοδος δεδομένων #include <iostream> using namespace std; int main(){ int i; float f; cin >> i >> f; cout << "i = " << i << "n" << "f = " << f << "n" ; }

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move();

Class Car class Car { private: int _pos; public: void Initialize. Position(); void Move(); int Get. Position(); bool Collide(Car) bool Detonate(Mine); };

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { bool collision = 0; bool explosion = 0; int counter = 0; Car car. X; Car car. Y; Mine random. Mine; It would work even if we left these two variables to be int car. X. Initialize. Position(); car. Y. Initialize. Position(); while(!collision && !explosion){ car. X. Move(); car. Y. Move(); random. Mine. Activate(); collision = car. X. Collide(car. Y); explosion = car. X. detonate(random. Mine) || car. Y. detonate(random. Mine); counter ++; } if (collision){ cout << "Cars collided after " << counter <<" moves “ << “at position “ << car. X. Get. Position() << endl; } if (explosion){ cout << “Mine exploded after “ << counter << “ moves, “ << “at position “ << random. Mine. Get. Position() << endl; } }

Πίνακες Integers int data. Array[3]; C Strings #include <cstring> char first. Name[6]; main(){ cin

Πίνακες Integers int data. Array[3]; C Strings #include <cstring> char first. Name[6]; main(){ cin >> first. Name; // get the string from input strcpy (first. Name, “eddie”); // copy a value cout << first. Name; // output string }

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int

#include <iostream> #include <cmath> #include <cstdlib> /* Car class definition, method definitions */ int main() { Car car. X; car. X. Initialize. Position(); Car car. Y; car. Y. Initialize. Position(); Mine random. Mine; bool collision = 0; bool explosion = 0; int counter = 0; while(!collision && !explosion){ car. X. Move(); car. Y. Move(); random. Mine. Activate(); collision = car. X. Collide(car. Y); explosion = car. X. detonate(random. Mine) || car. Y. detonate(random. Mine); counter ++; } if (collision){ cout << "Cars collided after " << counter <<" moves “ << “at position “ << car. X. Get. Position() << endl; } if (explosion){ cout << “Mine exploded after “ << counter << “ moves, “ << “at position “ << random. Mine. Get. Position() << endl; } }

Αρχικοποίηση Μεταβλητών int counter(0); //equiv. to: int counter=0; int my. Array[4] = {1, 2,

Αρχικοποίηση Μεταβλητών int counter(0); //equiv. to: int counter=0; int my. Array[4] = {1, 2, 3, 4}; my. Array[0] = 1; my. Array[1] = 2; . . . //could also say int my. Array[] = {1, 2, 3, 4}; // int my. Array[10] = {1, 2, 3, 4}; // creates a table of size 10 //equiv. to: //we start from 0 creates a table of size 4 and fills the rest with 0 s //size: 6, length: 6 –remember the last ‘’ char first. Name[] = “eddie”; //size: 50, length: 6 char name[50] = “eddie”; strcpy(name, ”eddie vedder”); // fits in there

const main(){ int const SIZE = 20; //can be used to initialize an array

const main(){ int const SIZE = 20; //can be used to initialize an array char buffer[SIZE]; SIZE = 8; //will NOT work – // const variables do NOT change }

Εντολές βρόγχων (Loop Statements) while (condition){ statement; . . . } for (declaration-initialization; condition;

Εντολές βρόγχων (Loop Statements) while (condition){ statement; . . . } for (declaration-initialization; condition; iteration){ statement; . . . } //Example of counting. . . for (int i=0; i<5; i++){ //runs 5 times cout << i*5+3; }

break και continue • Χρήσιμα για τον έλεγχο ροής σε loops. // 100 iterations

break και continue • Χρήσιμα για τον έλεγχο ροής σε loops. // 100 iterations or a collision/explosion for(int i = 0; i < 100; i++){ car. X. Move(); car. Y. Move(); random. Mine. Activate(); collision = car. X. Collide(car. Y); explosion = car. X. detonate(random. Mine) || car. Y. detonate(random. Mine); counter ++; if (collision || explosion){ break; } } // cout << i; will give an error

break και continue Αγνόησε συγκρούσεις ή εκρήξεις που γίνονται στο σημείο 0 while(!collision &&

break και continue Αγνόησε συγκρούσεις ή εκρήξεις που γίνονται στο σημείο 0 while(!collision && !explosion){ car. X. Move(); car. Y. Move(); if (car. X. Get. Position() == 0 || car. X. Get. Position() == 0){ continue; } random. Mine. Activate(); collision = car. X. Collide(car. Y); explosion = car. X. detonate(random. Mine) || car. Y. detonate(random. Mine); counter ++; }

Συναρτήσεις #include <iostream> using namespace std; Δηλωση Συνάρτησης float triangle. Area (float width, float

Συναρτήσεις #include <iostream> using namespace std; Δηλωση Συνάρτησης float triangle. Area (float width, float height); main(){ float area = triangle. Area(1. 0, 2. 0); cout << "The area of the triangle is " } << area; Ορίσματα Συνάρτησης float triangle. Area(float width, float height){ float area; // local area = width * height / 2. 0; return area; Επιστρεφόμενη τιμή Συνάρτησης }

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h” //1 square mile is 2. 59 square

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h” //1 square mile is 2. 59 square km //the following function returns square miles //whereas length is in km float square. Area(float length){ return (length*length / 2. 59); } Compiler Error!!! error: redefinition of `float square. Area(float)' main (){ cout << “English(sqr ml)" << square. Area(2. 0); cout << f(); }

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h” //1 square mile is 2. 59 square

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h” //1 square mile is 2. 59 square km //the following function returns square miles //whereas length is in km float square. Area. Miles(float length){ return (length*length / 2. 59); } Όχι καλή λύση, μπερδεμένος κώδικας, πιθανά λάθη main (){ cout << “English(sqr ml)" << square. Area. Miles(2. 0); cout << f(); }

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h" namespace Local{ //1 square mile is 2.

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h" namespace Local{ //1 square mile is 2. 59 square km //the following function returns square miles //whereas length is in km float square. Area(float length){ return (length*length / 2. 59); } } main (){ cout << “English (sqr ml)" << Local: : square. Area(2. 0); cout << Public. Utils: : f(); }

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h" namespace Local{ //1 square mile is 2.

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h" namespace Local{ //1 square mile is 2. 59 square km //the following function returns square miles //whereas length is in km float square. Area(float length){ return (length*length / 2. 59); } } using namespace Local; main (){ cout << “English (sqr ml)" << square. Area(2. 0); cout << Public. Utils: : f(); }

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h" namespace Local{ //1 square mile is 2.

Παράδειγμα (συνέχεια) #include <iostream> #include “util. h" namespace Local{ //1 square mile is 2. 59 square km //the following function returns square miles //whereas length is in km float square. Area(float length){ return (length*length / 2. 59); } } using namespace Local; main (){ cout << “English (sqr ml) " << square. Area(2. 0); cout << “Normal (sqr klm) " << Public. Utils: : sqaure. Area(2. 0); }

ΔΕΙΚΤΕΣ (POINTERS) ΑΝΑΦΟΡΕΣ (REFERENCES)

ΔΕΙΚΤΕΣ (POINTERS) ΑΝΑΦΟΡΕΣ (REFERENCES)

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 0 x 1003 0 x 1004 0 x 1005 0 x 1006 0 x 1007

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 0 x 1003 0 x 1004 0 x 1005 0 x 1006 0 x 1007 0 x 0000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 0 x 1003 0 x 1004 0 x 1005 0 x 1006 0 x 1007 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 0 x 1003 0 x 1004 0 x 1005 0 x 1006 0 x 1007 15 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 0 x 1003 0 x 1004 Στη C++ int *a. Ptr = new int; *a. Ptr = 15; 0 x 1005 0 x 1006 0 x 1007 15 0 x 1000 a. Ptr

Δείκτες – Αποδέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Αποδέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 free(a. Ptr); 0 x 1003 0 x 1004 0 x 1005 0 x 1006 0 x 1007 15 0 x 0000 a. Ptr

Δείκτες – Αποδέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Αποδέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 free(a. Ptr); 0 x 1003 0 x 1004 Στη C++ int *a. Ptr = new int; *a. Ptr = 15; delete a. Ptr; 0 x 1005 0 x 1006 0 x 1007 15 0 x 0000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(3*sizeof(int)); 0 x 1001 *a. Ptr = 15; 0 x 1002 *(a. Ptr + 2) =17; 0 x 1003 free(a. Ptr) 0 x 1004 0 x 1005 0 x 1006 0 x 1007 0 x 1005 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(3*sizeof(int)); 0 x 1001 *a. Ptr = 15; a. Ptr[0] = 15; 0 x 1002 *(a. Ptr + 2) =17; 0 x 1003 free(a. Ptr) 0 x 1004 0 x 1005 0 x 1006 0 x 1007 0 x 1005 15 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(3*sizeof(int)); 0 x 1001 *a. Ptr = 15; a. Ptr[0] = 15; 0 x 1002 *(a. Ptr + 2) =17; a. Ptr[2] = 17; 0 x 1003 free(a. Ptr) 0 x 1004 0 x 1005 15 0 x 1006 0 x 1007 17 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(3*sizeof(int)); 0 x 1001 *a. Ptr = 15; a. Ptr[0] = 15; 0 x 1002 *(a. Ptr + 2) =17; a. Ptr[2] = 17; 0 x 1003 free(a. Ptr) 0 x 1004 0 x 1005 0 x 0000 15 0 x 1006 0 x 1007 17 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr

Δείκτες – Δέσμευση Μνήμης int *a. Ptr = null; 0 x 1000 a. Ptr = (int *)malloc(3*sizeof(int)); 0 x 1001 *a. Ptr = 15; a. Ptr[0] = 15; 0 x 1002 *(a. Ptr + 2) =17; a. Ptr[2] = 17; 0 x 1003 free(a. Ptr) 0 x 1004 Στη C++ int *a. Ptr = new int[3]; *a. Ptr = 15; *(a. Ptr + 2) =17; delete [] a. Ptr; 0 x 1005 0 x 0000 15 0 x 1006 0 x 1007 17 a. Ptr

Αναφορές int my. Int; ? ? my. Int = 4; 4 my. Int int

Αναφορές int my. Int; ? ? my. Int = 4; 4 my. Int int &my. Ref = my. Int; 4 my. Int my. Ref

Τι κρύβουν οι αναφορές #include <iostream> main() { int a = 1; int b

Τι κρύβουν οι αναφορές #include <iostream> main() { int a = 1; int b = 3; int& ref. A = a; main() { int a = 1; int b = 3; int * const ref. A_p = &a; cout << ref. A << endl; cout << a << endl; cout << *ref. A_p << endl; cout << a << endl; ref. A = b; cout << a << endl; *ref. A_p = b; cout << a << endl; } }