User Tools

Site Tools


cpp:function_templates

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:function_templates [2019/05/03 14:31] – [Templates συναρτήσεων με παραμέτρους σύνθετους τύπους (κλάσεις)] gthanoscpp:function_templates [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 26: Line 26:
  
 Ας προσπαθήσουμε να χρησιμοποιήσουμε την παραπάνω συνάρτηση. Ας προσπαθήσουμε να χρησιμοποιήσουμε την παραπάνω συνάρτηση.
-<code cpp maximum1.cpp>+<code cpp find_max1.cpp>
 template <typename T> template <typename T>
 T& maximum(T& a, T& b) { return (a>b?a:b); } T& maximum(T& a, T& b) { return (a>b?a:b); }
Line 47: Line 47:
 <WRAP tip 80% center round> <WRAP tip 80% center round>
 Μπορείτε να παραλείψετε τον τύπο των παραμέτρων κατά την κλήση της συνάρτησης **maximum** ως εξής: Μπορείτε να παραλείψετε τον τύπο των παραμέτρων κατά την κλήση της συνάρτησης **maximum** ως εξής:
-<code cpp maximum1.cpp>+<code cpp find_max1.cpp>
 template <typename T> template <typename T>
 T& maximum(T& a, T& b) { return (a>b?a:b); } T& maximum(T& a, T& b) { return (a>b?a:b); }
Line 66: Line 66:
 ===== Templates συναρτήσεων με παραμέτρους σύνθετους τύπους (κλάσεις) ===== ===== Templates συναρτήσεων με παραμέτρους σύνθετους τύπους (κλάσεις) =====
  
-Ας υποθέσουμε ότι θέλουμε να κάνουμε χρήση της παραπάνω συνάρτηση **maximum** προκειμένου να βρούμε το μέγιστο μεταξύ δύο φοιτητών. Η κλάση του φοιτητή δίνεται παρακάτω:+Ας υποθέσουμε ότι θέλουμε να κάνουμε χρήση της παραπάνω συνάρτηση **maximum** προκειμένου να βρούμε το μέγιστο μεταξύ δύο φοιτητών. Η κλάση //Student// του φοιτητή δίνεται στην προηγούμενη ενότητα:
  
-<code cpp Student.hpp> +Εφόσον προσθέσουμε την παραπάνω συνάρτηση υπεφόρτωσης του τελεστή %%>%% το πρόγραμμα εκτυπώνει τα εξής: 
- +<code cpp find_max2.cpp> 
-#include<cstring> +#include <iostream> 
- +#include "Student.hpp" 
-class Student { +using namespace std;
-  char *name; +
-  int aem; +
-   +
-public: +
-  Student(const char *name, int aem); +
-  Student(); +
-  ~Student(); +
-  char *getName() const; +
-  int getAEM() const; +
-  friend std::ostream& operator<<(std::ostream& out, const Student & st); +
-}; +
- +
-Student::Student(const char *name, int aem) { +
-  this->name = new char [strlen(name) + 1]; +
-  strcpy(this->name, name); +
-  this->aem = aem; +
-+
- +
-Student::Student() { +
-  this->name = nullptr; +
-  this->aem = 0; +
-+
- +
-Student::~Student() { +
-  if(name != nullptr) +
-    delete []name; +
-+
- +
-char* Student::getName() const { +
-  return name; +
-+
- +
-int Student::getAEM() const { +
-  return aem; +
-+
- +
-std::ostream& operator<<(std::ostream& out, const Student& st) { +
-  out << st.name << " " << st.aem; +
-  return out; +
-+
-</code> +
- +
-O κώδικας που κάνει χρήση της παραπάνω κλάσης είναι ο εξής:+
  
-<code cpp maximum2.cpp> 
 template <typename T> template <typename T>
 T& maximum(T& a, T& b) { return (a>b?a:b); } T& maximum(T& a, T& b) { return (a>b?a:b); }
- 
-#include <iostream> 
-using namespace std; 
- 
-#include "Student.hpp" 
  
 int main() { int main() {
-  Student george("George", 1234), kate("Kate", 12345);  + Student george("George", 1234), kate("Kate", 4321);
-  cout << "max('George','Kate'): " << maximum(george, kate<< endl; +
-+
-</code>+
  
-Εάν προσπαθήσουμε να μεταγλωττίσουμε, το πρόγραμμα βγάζει αρκετά μηνύματα λάθους τα οποία συμπυκνώνονται στο ότι ο μεταγλωττιστής δεν ξέρει πως να διαχειριστεί την ανισότητα μεταξύ δύο αντικειμένων τύπου //Student//. Για τον λόγο αυτό θα πρέπει να υπερφορτώσουμε τον τελεστή **<** για την κλάση Student ως εξής: + cout << "max('George','Kate')<< maximum(george, kate) << endl;
- +
-<code cpp> +
-// σύγκριση δύο μαθητών με βάση το ΑΕΜ τους +
-bool Student::operator<(const Student& stconst { +
-  if(aem st.aem) +
-    return true; +
-  return false;+
 } }
-</code> 
- 
-Εφόσον προσθέσουμε την παραπάνω συνάρτηση υπεφόρτωσης του τελεστή **<**το πρόγραμμα εκτυπώνει τα εξής: 
-<code> 
-max('George','Kate'): Kate 12345 
 </code> </code>
  
Line 166: Line 102:
  
 Ο κώδικας χρήσης της παραπάνω συνάρτησης θα μπορούσε να είναι ο εξής: Ο κώδικας χρήσης της παραπάνω συνάρτησης θα μπορούσε να είναι ο εξής:
-<code cpp maximum3.cpp>+<code cpp find_max3.cpp>
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
 +
 +#include "Student.hpp"
  
 template <typename T, int size> template <typename T, int size>
Line 178: Line 116:
   return max;   return max;
 } }
- 
-#include "Student.hpp" 
  
 int main() { int main() {
cpp/function_templates.1556893890.txt.gz · Last modified: 2019/05/03 13:31 (external edit)