Ευάγγελος Θεοδωρίδης theodori@ceid... Αμαλία Φωκά foka@ceid... Ακαδημαϊκό Έτος 2010-2011 http://www.ceid.upatras.gr/courses/cplusplus Τρίτη 9:00-12:00 Β4 Project (Yποχρεωτικό): 30% Φεβ.- 15% Σεπτ. Γραπτή Εξέταση: 70% Προτεινόμενα Βιβλία: The C++ Programming Language, Bjarne Stroustrup C++ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ, H.M. Deitel, P.J. Deitel C++ Primer, S. Lippman, J. Lajoie Thinking in C++, Bruce Eckel 2 Η ιστορία του προγραμματισμού υπολογιστών είναι μια διαρκής μετακίνηση από machine-oriented γλώσσες προς ιδέες και μεταφορές πιο κοντά στην αναπαράσταση του πως εμείς κατανοούμε τον κόσμο. Εξέλιξη: Γλώσσα μηχανής assembly Γλώσσες προγραμματισμού ανεξάρτητες από την μηχανή Διαδικασίες κ Συναρτήσεις Αντικείμενα 3 4
0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 5 6 GCD: SIMPLE: 7 TST BEQ MOV SXT DIV MOV MOV CALL RETURN B SIMPLE A, R5 R4 B, R4 B, A R5, B GCD 8
! This example program solves for roots of the quadratic equation,! ax^2 +bx +c =0,for given values of a, b and c.! PROGRAM bisection IMPLICIT NONE INTEGER :: iteration DOUBLE PRECISION :: CC, Er, xl, x0, x0_old, xr! Set convergence criterion and guess for xl, xr. CC = 1.d-4 xl = 8.d-1 xr = 11.d-1! Bisection method. Er =CC +1 iteration = 0 DO WHILE (Er > CC) iteration = iteration + 1! Compute x0 and the error. x0_old = x0 x0 = (xl + xr) / 2.d0 Er = DABS((x0 - x0_old)/x0)*100.d0 WRITE (*,10) iteration, x0_old, x0, Er 10 FORMAT (1X,I4,3(2X,E10.4)) Προστακτικός Προγραµµατισµός algorithms + data structures = programs. Πρωταρχικός µηχανισµός αφαίρεσης η procedure ή η function. Βιβλιοθήκες µε επαναχρησιµοποιούµενες functions ή subroutines υπάρχουν από τις αρχές του 1960s. Τρόπος προγραµµατισµού procedural abstraction. Στα 1970s data abstraction παρουσιάστηκε µαζί µε τους abstract data types. Data abstraction χρησιµοποιεί ένα τύπο από ενθυλάκοση (encapsulation- εγκιβωτισµός) για να περιορίσουν την εµβέλεια και την παρουσία µεταβλητών και τιµών και οι συναρτήσεις επενεργούν πάνω σε αυτές τις τιµές 9 stack υλοποιείται µε δοµή Η stack πρέπει να αρχικοποιηθεί Συναρτήσεις που επενεργούν στη stack είναι οι push, pop, top, empty. H interface στη stack δίνεται σε ένα C header file που περιέχει την ταυτότητα της κάθε συνάρτησης Ξεχωριστή µεταγλώττιση κρύβει τα δεδοµένα Όµως, η ξεχωριστή µεταγλώττιση δεν είναι ο ιδανικός τρόπος αποµόνωσης των δεδοµένων γιατί ορίζει ένα µόνο στιγµιότυπο stack. Χρειαζόµαστε έναν τύπο Stack
To header file περιέχει µόνο τον ορισµό του τύπου stack. Η stack είναι στην ουσία έναν δείκτης σε µία εγγραφή που υλοποιεί διασυνδεδεµένη λίστα Οι συναρτήσεις περιορίζουν την λίστα ώστε να είναι συνεπής µε την stack. Η stack πρέπει ρητώς να δηλωθεί και να αρχικοποιηθεί Η stack για να χρησιµοποιηθεί πρέπει να περάσει σε όλες τις συναρτήσεις include stack.h. newstack() function Η C δεν δίνει πραγµατικό µηχανισµό ενθυλάκωσης Η ιδέα είναι να συνδεθούν δεδοµένων και κώδικας µαζί σε ένα τµήµα κώδικα Αυτό γίνεται µε τα : Pascal units Ada packages Modula modules
program ValueArg(output); {Shows how to arrange for a procedure to have arguments. procedure PrintInitials(First, Last : char); {Within this procedure, the names First and Last represent the argument values. We ll call write to print them. begin write( My initials are: ); write(first); writeln(last) end; {PrintInitials begin PrintInitials ( D, C ); {Any two characters can be arguments. PrintInitials ( Q, T ); {Like strings, characters are quoted. PrintInitials ( &, # ) end. {ValueArg Τα Modules δεν παρέχουν µηχανισµό για αυτόµατη αρχικοποίηση εν υπάρχει µηχανισµός αποδέσµευσης της µνήµης εν υπάρχει τρόπος επέκτασης ενός module Στα τέλη του 1980s η ιδέα των αντικειµένων προτάθηκε 19
class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; public void addminutes (int m) { int totalminutes = ((60*hour) + minute + m) % (24*60); if (totalminutes<0) totalminutes = totalminutes + (24*60); hour = totalminutes / 60; minute = totalminutes % 60; 21 class Procedural Object Oriented Time hour minute void addminutes( int m ) intime hour = 8 minute = 30 void addminutes(int m) Withdraw, deposit, transfer Customer, money, account outtime hour = 17 minute = 35 void addminutes(int m) αντικείµενα 24
Μία κλάση είναι ένα πρωτότυπο για κατασκευή αντικειμένων Όταν προγραμματίζουμε έναν πρόγραμμα σε αντικειμενοστραφή γλώσσα προγραμματισμού όπως C++,Java κλπ, ορίζουμε κλάσεις που χρησιμοποιούνται για τη δημιουργία αντικειμένων Μία κλάση είναι ένας constructor για την δημιουργία αντικειμένων class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; constructor public void addminutes (int m) { int totalminutes = ((60*hour) + minute + m) % (24*60); if (totalminutes<0) totalminutes = totalminutes + (24*60); hour = totalminutes / 60; minute = totalminutes % 60; 25 26 Ένα αντικείμενο είναι μία υπολογιστική οντότητα που: 1. Εμπεριέχει/Αποθηκεύει μία κατάσταση 2. Είναι ικανή να εκτελεί ενέργειες (μεθόδους) 3. Επικοινωνεί με άλλα αντικείμενα μέσω μηνυμάτων Όπως οι εγγραφές (record) στη Pascal, οι δομές (struct) στη C : είναι ένα σύνολο από μεταβλητές (διαφόρων τύπων) που περιγράφουν την κατάσταση του αντικειμένου Οι μεταβλητές αυτές καλούνται ενός αντικειμένου καλούνται attributes (ή fields, ή instance variables, ή datamembers, ) 27 28
type TimeTYPE = record Hour: 1..23; Minute: 0..59; end; var intowork, outfromwork: TimeTYPE; class Time { private int hour, minute; attributes of Time public Time (int h, int m) { hour = h; minute = m; constructor for Time 29 Time intowork = new Time(8, 30); Time outfromwork = new Time(17, 35); Time hour minute class void addminutes( int m ) outtime hour = 17 minute = 35 void addminutes(int m) intime hour = 8 minute = 30 void addminutes(int m) αντικείµενα 31 Ένα αντικείμενο είναι κάτι παραπάνω από ένα record/struct Ένα αντικείμενο συμπεριλαμβάνει και ένα σύνολο procedures/functions που διεκπεραιώνουν ενέργεις class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; a method of Time public void addminutes (int m) { int totalminutes = ((60*hour) + minute + m) % (24*60); if (totalminutes<0) totalminutes = totalminutes + (24*60); hour = totalminutes / 60; minute = totalminutes % 60; 32
void bill 33 34 Σε µία µέθοδο του bill εκτελούνται οι ακόλουθες γραµµές: Time intowork = new Time(8, 30); intowork.addminutes(15); Σε µία µέθοδο του bill εκτελούνται οι ακόλουθες γραµµές: Time intowork = new Time(8, 30); intowork.addminutes(15); bill bill intowork hour = 8 minute = 30 void addminutes(int m) 35 36
Σε µία µέθοδο του bill εκτελούνται οι ακόλουθες γραµµές: Time intowork = new Time(8, 30); intowork.addminutes(15); Σε µία µέθοδο του bill εκτελούνται οι ακόλουθες γραµµές: Time intowork = new Time(8, 30); intowork.addminutes(15); intowork.addminutes(15) bill intowork hour = 8 minute = 30 void addminutes(int m) bill intowork hour = 8 minute = 45 void addminutes(int m) 37 38 class name { declarations constructor definition(s) method definitions attributes και symbolic constants Πως δηµιουργούνται και αρχικο οιούνται τα αντικείµενα της κλάσης Πως µ ορούµε να χειριστούµε την κατάσταση των αντικειµένων SIMULA I (1962-65) and Simula 67 (1967) were the first two object-oriented languages Developed at the Norwegian Computing Center, Oslo, Norway by Ole-Johan Dahl and Kristen Nygaard Simula 67 introduced most of the key concepts of object-oriented programming: objects and classes, subclasses ( inheritance ), virtual procedures 39 40
Alan Kay, Adele Goldberg και άλλοι Xerox PARC επεκτείνουν τη Simula δημιουργώντας Smalltalk (1970 s) Εισάγουν τον όρο object oriented Smalltalk είναι η πρώτη πλήρως object oriented γλώσσα Προτείνουν έναν νέο programming paradigm Συμπεριλαμβάνουν graphical user interfaces και interactive program execution Bjarne Stroustrup develops C++ (1980 s) Εφαρμόζει τις object oriented αρχές στην C Eiffel (B. Meyer) CLOS (D. Bobrow, G. Kiczales) SELF (D. Ungar et al.) Java (J. Gosling et al.) BETA (B. Bruun-Kristensen, O. Lehrmann Madsen, B. Møller-Pedersen, K. Nygaard) Other languages add object dialects, όπως TurboPascal, Fortran 41 42 Ένα αντικείμενο είναι μία υπολογιστική οντότητα που: 1. Εμπεριέχει/Αποθηκεύει μία κατάσταση 2. Είναι ικανή να εκτελεί ενέργειες (μεθόδους) 3. Επικοινωνεί με άλλα αντικείμενα μέσω μηνυμάτων ΙΔΕΑ: ένα σύστημα αποτελείται από ένα σύνολο ανεξάρτητων αντικειμένων που κρατούν τα υποσυστήματα πραγματικά ανεξάρτητα επικοινωνούν με τον αυστηρό τρόπο ανταλλαγής μηνυμάτων Διαφορετικές ομάδες προγραμματισμών μπορούν παράλληλά να δουλεύουν σε διαφορετικά υποσυστήματα προσπαθώντας απλός να συμβιβαστούν με ένα interface Είναι δυνατό να χτίσουμε μεγαλύτερα συστήματα με μικρότερο κόπο 43 44
Συστήματα από interacting objects: extreme modularity Πιο κοντά στην ανθρώπινη σκέψη (ή όχι) Αποφυγή επανεγγραφής κώδικα υψηλό code-reuse Οι κλάσεις μπορούν να ταξινομηθούν σε ιεραρχίες Υποκλάσεις κληρονομούν χαρακτηριστικά και μεθόδους από τις γονικές κλάσεις Αποφεύγουμε έτσι την επανεγγραφή κώδικά καθώς οι κλάσεις extend επεκτείνουν άλλες με λίγο κόπο Ιδανικό για μεγάλους δομημένους σχηματισμούς 45 46 Object Αντικείµενα αυτής της κλάσης έχουν όλα τα attributes και τις methods των κλάσεων µέχρι την ρίζα του δέντρου Rectangle length width int computearea( ) Shape color borderwidth Circle radius tostring( ) equals( Object obj ) getclass( ) Color getcolor( ) void setborderwidth( int m ) int computearea( ) Time hour minute void addminutes( int m ) Triangle base height int computearea( ) 47 Ένα αντικείμενο έχει multiple identities, ανάλογα με το δένδρο κληρονομίας τουbased on its class inheritance tree => άρα μπορεί να χρησιμοποιηθεί με πολλαπλούς τρόπους!!! Ένας κύκλος είναι ένα σχήμα είναι ένα αντικείμενο Object Shape Circle Object Shape color borderwidth Circle radius tostring( ) equals( Object obj ) getclass( ) Color getcolor( ) void setborderwidth( int m ) int computearea( ) 48
Circle c = new Circle( ); 1. Circle c = new Circle( ); Object Shape c Circle Execution Time 49 50 1. 2. 1. 2. 3. Object Object Object Object Object Shape Shape Shape Shape Shape c Circle c Circle c Circle c Circle c Circle Execution Time Execution Time Circle c = new Circle( ); 51 Circle c = new Circle( ); 52
1. Χρήση Πολυμορφισμού σε Arrays 2. Χρήση Πολυμορφισμού σε Method Arguments 3. Χρήση Πολυμορφισμού σε Method Return Type Δηλώνουμε ένα array με Shape objects, και αποθηκεύουμε Rectangles, Circles, or Triangles Object tostring( ) equals( Object obj ) getclass( ) Shape color borderwidth Color getcolor( ) void setborderwidth( int m ) 53 Rectangle length width int computearea( ) Circle radius int computearea( ) Triangle base height int computearea( ) 54 Δηλώνουμε ένα array με Shape objects, και αποθηκεύουμε Rectangles, Circles, or Triangles samples (an array of Shape objects) firstshape length [2] = 17 width = 35 int computearea( ) secondshape radius = 11 int computearea( ) thirdshape base = 15 height = 7 int computearea( ) [0] [1] [2] Δημιουργούμε μία διαδικασία που έχει σαν όρισμα ένα αντικείμενο Shape, και στη συνέχεια τη χρησιμοποιούμε περνώντας αντικείμενα του τύπου Rectangle, Circle, and Triangle public int calculatepaint (Shape myfigure) { final int PRICE = 5; int totalcost = PRICE * myfigure.computearea( ); return totalcost; 55 Ο ορισµός της computearea( ) είναι γνωστός µόνο κατά τη ώρα εκτέλεσης this is dynamic binding 56
Μπορούμε να γράψουμε γενικό κώδικα και ο τύπος που επιστρέφεται να αποφασίζεται κατά την εκτέλεση public Shape createpicture ( ) { /* Read in choice from user */ System.out.println( 1 for rectangle, + 2 for circle, 3 for triangle: ); SimpleInput sp = new SimpleInput(System.in); int i = sp.readint( ); if ( i == 1 ) return new Rectangle(17, 35); if ( i == 2 ) return new Circle(11); if ( i == 3 ) return new Triangle(15, 7); 57 Ο αντικειμενοστραφής προγραμματισμός παρέχει έναν ποιοτικά ανώτερο τρόπο να οργανώνουμε εφαρμογές λογισμικού It encourages a high degree of modularity in programming, making large projects easier to implement It provides powerful techniques like inheritance and polymorphism to help organize and reuse code Αντικειμενοστραφείς γλώσσες προγραμματισμού όπως η C++,Java, C# είναι ο σύγχρονος τρόπος υλοποίησης εφαρμογών σε διάφορα περιβάλλοντα PC, servers, Internet, multiprocessor computers etc. 58 Αντικειµενοστραφής Προγραµµατισµός C++ Encapsulation (data hiding) Enable programmer to group data & subroutines (methods) together, hiding irrelevant details from users Inheritance Enable a new abstraction (i.e., derived class) to be defined as an extension of an existing abstraction, retaining key characteristics Dynamic method binding Enable use of new abstraction (i.e., derived class) to exhibit new behavior in context of old abstraction Αρχικοποίηση και Αποδέσµευση Αντικειµένων Choosing a constructor How are constructors supported in the language? References and values Value model object creation results from elaboration Execution order of initialization E.g., with derived classes Garbage Collection? Don t need destructors!
Παράδειγµα σε C++ Παράδειγµα σε C++ Αρχικοποίηση και Αποδέσµευση Αντικειµένων Initialization in C++ This is NOT assignment!!! This IS assignment!!! C++ automatically calls constructors for base classes before current constructor Base class members can be explicitly initialized using foo::foo (foo args) : bar (bar args), member1 (m1 args), member2 (m2 args) { Use of this syntax results in call to copy constructor rather than 0-arg constructor followed by operator= Java, like C++, by default calls 0- argument versions of base class constructors Call to super (args) overrides 0-arg version
Any class can limit the visibility of its members: Public members are visible anywhere the class is in scope. Private members are visible only within the class s methods. Protected members are visible inside members of the class and derived classes. Friend classes are granted exceptions to (some) of the rules. Derived classes can further restrict visibility of base class members, but not increase it: Private members of a base class are never visible in a derived class. Protected and public members of a public base class are protected or public, respectively, in a derived class. Protected and public members of a protected base class are protected members of a derived class. Protected and public members of a private base class are private members of a derived class. Derived classes that limit visibility of base class members can restore visibility by inserting a using declaration in its protected or public sections. Rules in other languages can be significantly different.
The vtable entry contains the this correction for each method
A new opportunity for ambiguity and additional implementation complexity
A base class composed entirely of abstract methods is called an interface (at least in Java). It has no data nor implements any methods. Inheritance from one real base class and any number of interfaces is called mixed inheritance. Virtual methods of the interface(s) are mixed into the methods of the derived class.