cpp:class_templates
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| cpp:class_templates [2019/05/03 14:40] – created 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 36: | Line 40: | ||
| <code cpp BoxUsage.cpp> | <code cpp BoxUsage.cpp> | ||
| + | #include < | ||
| #include " | #include " | ||
| #include " | #include " | ||
| Line 45: | Line 50: | ||
| Box< | Box< | ||
| Box< | Box< | ||
| - | Student | + | Student |
| - | Box< | + | 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 << "--- 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.1556894427.txt.gz · Last modified: 2019/05/03 13:40 (external edit)
