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:46] – [Ένα πιο σύνθετο παράδειγμα] 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> | <code cpp Array.hpp> | ||
Line 75: | Line 81: | ||
public: | public: | ||
Array(); | Array(); | ||
- | Array(const T t[], int length); | ||
Array(const Array< | Array(const Array< | ||
void set(T e, int index); | void set(T e, int index); | ||
Line 89: | Line 94: | ||
Array< | Array< | ||
// empty | // empty | ||
- | } | ||
- | |||
- | template< | ||
- | Array< | ||
- | int copy_size = length > size ? size : length; | ||
- | for(int i=0; i< | ||
- | array[i] = t[i]; | ||
} | } | ||
Line 157: | Line 155: | ||
int main() { | int main() { | ||
Array< | Array< | ||
+ | | ||
for(int i=0; i< | for(int i=0; i< | ||
a[i] = i; | a[i] = i; | ||
| | ||
+ | cout << "--- Printing a ---\n"; | ||
cout << a << endl; | cout << a << endl; | ||
- | Array< | + | |
+ | | ||
+ | |||
+ | cout << "--- Printing b ---\n"; | ||
cout << b << endl; | cout << b << endl; | ||
| | ||
- | Array< | + | Array< |
sts[0] = Student(" | sts[0] = Student(" | ||
sts[1] = Student(" | sts[1] = Student(" | ||
sts[2] = Student(" | sts[2] = Student(" | ||
+ | sts[3] = Student(" | ||
| | ||
+ | cout << "--- Printing sts ---\n"; | ||
cout << sts << endl; | cout << sts << endl; | ||
| | ||
} | } | ||
</ | </ |
cpp/class_templates.1556945203.txt.gz · Last modified: 2019/05/04 03:46 (external edit)