This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Previous revision Next revision Both sides next revision | ||
|
cpp:class_templates [2020/05/25 06:52] |
cpp:class_templates [2020/05/25 07:50] gthanos [Ένα πιο σύνθετο παράδειγμα] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Templates κλάσεων ====== | ||
| + | Σε αναλογία με τα templates συναρτήσεων μπορούμε να κατασκευάσουμε κλάσεις που να λαμβάνουν ως παράμετρο ένα ή περισσότερους τύπους δεδομένων. Στο παρακάτω παράδειγμα, | ||
| + | |||
| + | <code cpp Box.hpp> | ||
| + | #ifndef _BOX_HPP_ | ||
| + | #define _BOX_HPP_ | ||
| + | |||
| + | template < | ||
| + | class Box { | ||
| + | T e; | ||
| + | public: | ||
| + | Box(T e); | ||
| + | T get() const; | ||
| + | void set(T e); | ||
| + | template < | ||
| + | friend std:: | ||
| + | }; | ||
| + | |||
| + | template < | ||
| + | Box< | ||
| + | this->e = e; | ||
| + | } | ||
| + | |||
| + | template < | ||
| + | T Box< | ||
| + | |||
| + | template < | ||
| + | void Box< | ||
| + | |||
| + | template < | ||
| + | std:: | ||
| + | out << t.get(); | ||
| + | return out; | ||
| + | } | ||
| + | #endif | ||
| + | </ | ||
| + | |||
| + | H παραπάνω κλάση μπορεί να παράγει διαφορετικούς τύπους δεδομένων, | ||
| + | |||
| + | <code cpp BoxUsage.cpp> | ||
| + | #include < | ||
| + | #include " | ||
| + | #include " | ||
| + | |||
| + | using namespace std; | ||
| + | |||
| + | int main() { | ||
| + | | ||
| + | Box< | ||
| + | Box< | ||
| + | Student kate = {" | ||
| + | Box< | ||
| + | Student katherine = studentBox.get(); | ||
| + | katherine.setName(" | ||
| + | | ||
| + | cout << "--- Printing Values ---" << endl; | ||
| + | cout << kate << endl; | ||
| + | cout << katherine << endl; | ||
| + | cout << studentBox.get() << endl; | ||
| + | cout << "--- Destroying objects ---" << endl; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Ένα πιο σύνθετο παράδειγμα ===== | ||
| + | |||
| + | Ας υποθέσουμε ότι θέλουμε να κατασκευάσουμε ένα στατικό πίνακα (συγκεκριμένης χωρητικότητας) για την αποθήκευση πληροφορίας. Σε αναλογία με την κλάση **Box< | ||
| + | |||
| + | <code cpp Array.hpp> | ||
| + | #ifndef __ARRAY_H__ | ||
| + | #define __ARRAY_H__ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | template < | ||
| + | class Array { | ||
| + | T array[size]; | ||
| + | | ||
| + | public: | ||
| + | Array(); | ||
| + | Array(const Array< | ||
| + | void set(T e, int index); | ||
| + | T get(int index) const; | ||
| + | void rmv(int index); | ||
| + | Array< | ||
| + | T& operator[](int index); | ||
| + | template< | ||
| + | friend std:: | ||
| + | }; | ||
| + | |||
| + | template< | ||
| + | Array< | ||
| + | // empty | ||
| + | } | ||
| + | |||
| + | template< | ||
| + | Array< | ||
| + | for(int i=0; i<size; i++) | ||
| + | array[i] = a.array[i]; | ||
| + | } | ||
| + | |||
| + | template< | ||
| + | void Array< | ||
| + | assert(index> | ||
| + | array[index] = e; | ||
| + | } | ||
| + | |||
| + | template< | ||
| + | T Array< | ||
| + | assert(index> | ||
| + | return array[index]; | ||
| + | } | ||
| + | |||
| + | template< | ||
| + | void Array< | ||
| + | assert(index> | ||
| + | for(int i=index+1; i<size; i++) | ||
| + | array[i-1] = array[i]; | ||
| + | } | ||
| + | |||
| + | template< | ||
| + | Array< | ||
| + | for(int i=0; i<size; i++) | ||
| + | array[i] = a.array[i]; | ||
| + | } | ||
| + | |||
| + | template< | ||
| + | T& Array< | ||
| + | assert(index> | ||
| + | return array[index]; | ||
| + | } | ||
| + | |||
| + | template< | ||
| + | std:: | ||
| + | for(int i=0; i<size; i++) { | ||
| + | out << t.array[i] ; | ||
| + | if(i< | ||
| + | out << ", "; | ||
| + | } | ||
| + | return out; | ||
| + | } | ||
| + | #endif | ||
| + | </ | ||
| + | |||
| + | <code cpp ArrayUsage.cpp> | ||
| + | #include " | ||
| + | #include " | ||
| + | |||
| + | #define ARRAY_SIZE 10 | ||
| + | |||
| + | using namespace std; | ||
| + | |||
| + | int main() { | ||
| + | Array< | ||
| + | for(int i=0; i< | ||
| + | a[i] = i; | ||
| + | | ||
| + | cout << a << endl; | ||
| + | Array< | ||
| + | cout << b << endl; | ||
| + | | ||
| + | Array< | ||
| + | sts[0] = Student(" | ||
| + | sts[1] = Student(" | ||
| + | sts[2] = Student(" | ||
| + | | ||
| + | cout << sts << endl; | ||
| + | | ||
| + | } | ||
| + | </ | ||