1 / 21

Διαφάνειες παρουσίασης #7

Διαφάνειες παρουσίασης #7. Templates συναρτήσεων Templates κλάσεων Templates και κληρονομικότητα Templates και φίλες συναρτήσεις Χειρισμός εξαιρέσεων. Templates συναρτήσεων ( i). Ανάγκη αποφυγής περιττού κώδικα int i1 = 3, i2 = 5; int i = min(i1, i2); int min (int i1, int i2) {

Download Presentation

Διαφάνειες παρουσίασης #7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Διαφάνειες παρουσίασης #7 Templates συναρτήσεων Templates κλάσεων Templates και κληρονομικότητα Templates και φίλες συναρτήσεις Χειρισμός εξαιρέσεων

  2. Templates συναρτήσεων (i) • Ανάγκη αποφυγής περιττού κώδικα int i1 = 3, i2 = 5; int i = min(i1, i2); int min (int i1, int i2) { return (i1 < i2) ? i1 : i2; } double d1 = 1.4, d2 = 3.6; double d = min(d1, d2); Name n1("Antonius"), n2("Cleopatra"); Name n = min(n1, n2);

  3. Templates συναρτήσεων (ii) • Ορισμός template συνάρτησης template <class T> T min (T t1, T t2) { return (t1 < t2) ? t1 : t2; } • Ερμηνεία • Για κάθε τύπο Tορίζεται μια συνάρτηση T min (T t1, T t2) • Ο ορισμός της συνάρτησης γίνεται όταν αυτή χρησιμοποιηθεί για πρώτη φορά (instantiation)

  4. Templates συναρτήσεων (iii) • Υπερφόρτωση template template <class T> T min (int size, const T array[]) { T result = array[0]; for (int i=1; i<size; i++) if (array[i] < result) result = array[i]; return result; }

  5. Templates συναρτήσεων (iv) • Εξειδίκευση template template <class T> T min (T t1, T t2) { return (t1 < t2) ? t1 : t2; } char * min (char * s1, char * s2) { return (strcmp(s1, s2) < 0) ? s1 : s2; }

  6. Templates κλάσεων (i) • Ανάγκη αποφυγής περιττού κώδικα PairOfInt si(1, 5); cout << si.fst() << "\n"; PairOfDouble sd(3.5, -2.9); cout << sd.fst() << "\n"; PairOfName sn( Name("Antony"), Name("Cleopatra") ); cout << sn.fst() << "\n";

  7. Templates κλάσεων (ii) • Ορισμός template κλάσης template <class T> class Pair { // definition } • Ερμηνεία • Για κάθε τύπο Tορίζεται μια κλάση Pair<T> • Ο ορισμός της κλάσης γίνεται όταν αυτή χρησιμοποιηθεί για πρώτη φορά (instantiation)

  8. Templates κλάσεων (iii) • Παράδειγμα: ζεύγη ομοίων template <class T> class Pair { private: T eFirst, eSecond; public: Pair (const T & a, const T & b); T fst () const; T snd () const; };

  9. Templates κλάσεων (iv) • Παράδειγμα (συνέχεια) template <class T> Pair<T>::Pair (const T & a, const T & b) : eFirst(a), eSecond(b) { /* nothing */ } template <class T> T Pair<T>::fst () const { return eFirst; } template <class T> T Pair<T>::snd () const { return eSecond; }

  10. Templates κλάσεων (v) • Παράδειγμα: ζεύγη ανομοίων template <class T1, class T2> class Pair { private: T1 eFirst; T2 eSecond; public: Pair (const T1 & a, const T2 & b); T1 fst () const; T2 snd () const; };

  11. Templates κλάσεων (vi) • Παράδειγμα (συνέχεια) template <class T1, class T2> Pair<T1, T2>::Pair (const T1 & a, const T2 & b) : eFirst(a), eSecond(b) { /* nothing */ } template <class T1, class T2> T1 Pair<T1, T2>::fst () const { return eFirst; } template <class T1, class T2> T2 Pair<T1, T2>::snd () const { return eSecond; }

  12. Templates και κληρονομικότητα (i) • Παράδειγμα: τριάδες ανομοίων template <class T1, class T2, class T3> class Triple : public Pair< T1, Pair<T2, T3> > { public: Triple (const T1 & a, const T2 & b, const T3 & c); T2 snd () const; // hides old snd T3 trd () const; };

  13. Templates και κληρονομικότητα (ii) • Παράδειγμα (συνέχεια) template <class T1, class T2, class T3> Triple<T1, T2, T3>::Triple ( const T1 & a, const T2 & b, const T3 & c) : Pair< T1, Pair<T2, T3> >( a, Pair<T2, T3>(b, c)) { // nothing }

  14. Templates και κληρονομικότητα (iii) • Παράδειγμα (συνέχεια) template <class T1, class T2, class T3> T2 Triple<T1, T2, T3>::snd () const { return eSecond.fst(); } template <class T1, class T2, class T3> T3 Triple<T1, T2, T3>::trd () const { return eSecond.snd(); }

  15. Templates και κληρονομικότητα (iv) • Παράδειγμα (κύριο πρόγραμμα) Pair<int, double> p(42, 1.7); cout << p.fst() << ", " << p.snd() << ".\n"; Triple<int, double, const char *> t(42, 1.7, "hello"); cout << t.fst() << ", " << t.snd() << ", " << t.trd() << ".\n";

  16. Templates και φίλες συναρτήσεις (i) • Παράδειγμα template <class T1, class T2> class Pair { // ... friend Pair<T1, T2> operator + <T1, T2> ( const Pair<T1, T2> & p1, const Pair<T1, T2> & p2); };

  17. Templates και φίλες συναρτήσεις (ii) • Παράδειγμα template <class T1, class T2> Pair<T1, T2> operator + ( const Pair<T1, T2> & p1, const Pair<T1, T2> & p2) { return Pair<T1, T2>( p1.eFirst + p2.eFirst, p1.eSecond + p2.eSecond); }

  18. Χειρισμός εξαιρέσεων (i) • Τί είναι εξαιρέσεις • Κάθε είδους ανωμαλίες ή σφάλματα που προκύπτουν κατά την εκτέλεση του προγράμματος και πρέπει να ξεπερασθούνμε ειδικό τρόπο • Π.χ. διαίρεση με το μηδέν, εξάντληση μνήμης • Χειρισμός εξαιρέσεων στη C++ • Μηχανισμός: try - throw - catch

  19. Χειρισμός εξαιρέσεων (ii) class Exc { ... }; try { } void f () throw(Exc) { ... if (wrong) { Exc e(...); throw e; } ... } ... f () ... // something catch (Exc e) { // do something }

  20. Χειρισμός εξαιρέσεων (iii) • Παράδειγμα int min (int size, const int array[]) throw (const char *) { if (size <= 0) throw "ERROR: size <= 0"; if (array == NULL) throw "ERROR: array == NULL"; int result = array[0]; for (int i=1; i < size; i++) if (array[i] < result) result = array[i]; return result; }

  21. Χειρισμός εξαιρέσεων (iv) • Παράδειγμα (συνέχεια) int a [] = {42, 34, 5, 99, 37, 2, 21}; try { cout << "min(7, a) = " << min(7, a) << "\n"; cout << "min(4, a) = " << min(4, a) << "\n"; cout << "min(0, a) = " << min(0, a) << "\n"; // !!! } catch (const char * msg) { cout << "\nCaught exception:\n" << msg << "\n"; }

More Related