cpp:function_templates

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
cpp:function_templates [2019/05/04 04:32]
gthanos [Templates συναρτήσεων με παραμέτρους σύνθετους τύπους (κλάσεις)]
cpp:function_templates [2020/05/18 15:23]
gthanos [Templates συναρτήσεων με αριθμητικές παραμέτρους]
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+Εφόσον προσθέσουμε την παραπάνω συνάρτηση υπεφόρτωσης του τελεστή %%<%% το πρόγραμμα εκτυπώνει τα εξής: 
-#include<cstring>+<code cpp find_max2.cpp
 +#include <iostream> 
 +#include "Student.hpp" 
 +using namespace std;
  
-#ifndef _STUDENT_H_ +template <typename T> 
-#define _STUDENT_H_+T& maximum(T& a, T& b) { return (a>b?a:b); }
  
-class Student { +int main() { 
-  char *name; + Student george("George", 1234), kate("Kate"4321);
-  int aem; +
-   +
-public: +
-  Student(const char *name, int aem); +
-  Student()+
-  Student(const Student& ); +
-  Student(const Student* ); +
-  ~Student(); +
-  char *getName() const; +
-  int getAEM() const; +
-  void setName(const char *name); +
-  void setAEM(int aem); +
-  friend std::ostream& operator<<(std::ostream& outconst Student & st)+
-  bool operator>(const Student& st) const; +
-  Student& operator=(const Student& st); +
-};+
  
-Student::Student(const char *nameint aem+ cout << "max('George','Kate'): " << maximum(georgekate<< endl;
-  this->name = new char [strlen(name) + 1]; +
-  strcpy(this->namename)+
-  this->aem = aem;+
 } }
- 
-Student::Student(const Student& st) { 
-  name = new char [strlen(st.name) + 1]; 
-  strcpy(name, st.name); 
-  aem = st.aem; 
-} 
- 
-Student::Student(const Student* st) { 
-  name = new char [strlen(st->name) + 1]; 
-  strcpy(name, st->name); 
-  aem = st->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; 
-} 
- 
-void Student::setName(const char *name) { 
-  if(this->name != nullptr) 
-    delete this->name; 
-  this->name = new char [strlen(name) + 1]; 
-  strcpy(this->name, name); 
-} 
- 
-void Student::setAEM(int aem) { 
-  this->aem = aem; 
-} 
- 
-Student& Student::operator=(const Student& st) { 
-  if(name != nullptr) 
-    delete name; 
-  name = new char [strlen(st.name) + 1]; 
-  strcpy(name, st.name); 
-  aem = st.aem; 
-  return *this; 
-} 
- 
-std::ostream& operator<<(std::ostream& out, const Student& st) { 
-  out << st.name << " " << st.aem; 
-  return out; 
-} 
- 
-bool Student::operator>(const Student& st) const { 
-  if(aem > st.aem) 
-    return true; 
-  return false; 
-} 
-#endif 
-</code> 
- 
-Εφόσον προσθέσουμε την παραπάνω συνάρτηση υπεφόρτωσης του τελεστή **<**το πρόγραμμα εκτυπώνει τα εξής: 
-<code> 
-max('George','Kate'): Kate 12345 
 </code> </code>
  
Line 185: Line 102:
  
 Ο κώδικας χρήσης της παραπάνω συνάρτησης θα μπορούσε να είναι ο εξής: Ο κώδικας χρήσης της παραπάνω συνάρτησης θα μπορούσε να είναι ο εξής:
-<code cpp maximum3.cpp>+<code cpp find_max3.cpp>
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
 +
 +incluce "Student.hpp"
  
 template <typename T, int size> template <typename T, int size>
cpp/function_templates.txt · Last modified: 2021/06/04 16:29 (external edit)