cpp:class_templates
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cpp:class_templates [2019/05/04 04:25] – [Templates κλάσεων] gthanos | cpp:class_templates [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| <code cpp Box.hpp> | <code cpp Box.hpp> | ||
| + | #ifndef _BOX_HPP_ | ||
| + | #define _BOX_HPP_ | ||
| + | |||
| template < | template < | ||
| class Box { | class Box { | ||
| Line 31: | Line 34: | ||
| return out; | return out; | ||
| } | } | ||
| + | #endif | ||
| </ | </ | ||
| Line 51: | Line 55: | ||
| katherine.setName(" | katherine.setName(" | ||
| | | ||
| + | cout << "--- Printing Values ---" << endl; | ||
| cout << kate << endl; | cout << kate << endl; | ||
| cout << katherine << endl; | cout << katherine << endl; | ||
| cout << studentBox.get() << endl; | cout << studentBox.get() << endl; | ||
| + | cout << "--- Destroying objects ---" << endl; | ||
| } | } | ||
| </ | </ | ||
| Line 59: | Line 65: | ||
| ===== Ένα πιο σύνθετο παράδειγμα ===== | ===== Ένα πιο σύνθετο παράδειγμα ===== | ||
| - | Ας υποθέσουμε ότι θέλουμε να κατασκευάσουμε ένα στατικό πίνακα (συγκεκριμένης χωρητικότητας) για την αποθήκευση πληροφορίας. Σε αναλογία με την κλάση **Box< | + | Ας υποθέσουμε ότι θέλουμε να κατασκευάσουμε ένα στατικό πίνακα (συγκεκριμένης χωρητικότητας) για την αποθήκευση πληροφορίας. Σε αναλογία με την κλάση **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 << "--- Printing a ---\n"; | ||
| + | cout << a << endl; | ||
| + | | ||
| + | Array< | ||
| + | | ||
| + | cout << "--- Printing b ---\n"; | ||
| + | cout << b << endl; | ||
| + | | ||
| + | Array< | ||
| + | sts[0] = Student(" | ||
| + | sts[1] = Student(" | ||
| + | sts[2] = Student(" | ||
| + | sts[3] = Student(" | ||
| + | | ||
| + | cout << "--- Printing sts ---\n"; | ||
| + | cout << sts << endl; | ||
| + | | ||
| + | } | ||
| + | </ | ||
cpp/class_templates.1556943928.txt.gz · Last modified: 2019/05/04 03:25 (external edit)
