cpp:class_templates
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:class_templates [2019/05/03 14:40] – created gthanos | cpp:class_templates [2020/05/25 07:45] – [Templates κλάσεων] gthanos | ||
---|---|---|---|
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 T t[], int length); | ||
+ | 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< | ||
+ | int copy_size = length > size ? size : length; | ||
+ | for(int i=0; i< | ||
+ | array[i] = t[i]; | ||
+ | } | ||
+ | |||
+ | 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; | ||
+ | | ||
+ | } | ||
+ | </ |
cpp/class_templates.txt · Last modified: 2020/05/25 06:52 (external edit)