User Tools

Site Tools


cpp:class_templates

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
cpp:class_templates [2019/05/03 14:40] – created gthanoscpp:class_templates [2019/05/04 04:25] – [Templates κλάσεων] gthanos
Line 36: Line 36:
  
 <code cpp BoxUsage.cpp> <code cpp BoxUsage.cpp>
 +#include <iostream>
 #include "Box.hpp" #include "Box.hpp"
 #include "Student.hpp" #include "Student.hpp"
Line 45: Line 46:
   Box<int> b(15);   Box<int> b(15);
   Box<double> d(4.23);   Box<double> d(4.23);
-  Student george = {"George", 1234}; +  Student kate = {"Kate", 1234}; 
-  Box<Student> studentBox(george);+  Box<Student> studentBox(kate); 
 +  Student katherine = studentBox.get(); 
 +  katherine.setName("Katherine"); 
 +   
 +  cout << kate << endl; 
 +  cout << katherine << endl; 
 +  cout << studentBox.get() << endl; 
 +}
 </code> </code>
 +
 +===== Ένα πιο σύνθετο παράδειγμα =====
 +
 +Ας υποθέσουμε ότι θέλουμε να κατασκευάσουμε ένα στατικό πίνακα (συγκεκριμένης χωρητικότητας) για την αποθήκευση πληροφορίας. Σε αναλογία με την κλάση **Box<Τ>** θέλουμε να φτιάξουμε ένα πίνακα στον οποίο να μπορούμε να αποθηκεύσουμε διαφορετικούς τύπους δεδομένων.
 +
  
  
  
cpp/class_templates.txt · Last modified: 2020/05/25 06:52 (external edit)