User Tools

Site Tools


cpp:class_templates

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
cpp:class_templates [2020/05/18 15:33] gthanoscpp:class_templates [2020/05/25 07:52] – [Ένα πιο σύνθετο παράδειγμα] gthanos
Line 55: Line 55:
   katherine.setName("Katherine");   katherine.setName("Katherine");
      
 +  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;
 } }
 </code> </code>
Line 79: Line 81:
 public: public:
   Array();   Array();
-  Array(const T t[], int length); 
   Array(const Array<T,size>& a);   Array(const Array<T,size>& a);
   void set(T e, int index);   void set(T e, int index);
Line 93: Line 94:
 Array<T,size>::Array() { Array<T,size>::Array() {
   // empty   // empty
-} 
- 
-template<typename T, int size> 
-Array<T,size>::Array(const T t[], int length) {  
-  int copy_size = length > size ? size : length; 
-  for(int i=0; i<copy_size; i++) 
-    array[i] = t[i]; 
 } }
  
Line 161: Line 155:
 int main() { int main() {
   Array<int, ARRAY_SIZE> a;   Array<int, ARRAY_SIZE> a;
 +  
   for(int i=0; i<ARRAY_SIZE; i++)   for(int i=0; i<ARRAY_SIZE; i++)
     a[i] = i;     a[i] = i;
      
 +  cout << "--- Printing a ---\n";
   cout << a << endl;   cout << a << endl;
-  Array<int, ARRAY_SIZE> b(a);+   
 +  Array<int, 10> b(a)
 +   
 +  cout << "--- Printing b ---\n";
   cout << b << endl;   cout << b << endl;
      
-  Array<Student, ARRAY_SIZE> sts;+  Array<Student,4> sts;
   sts[0] = Student("Kate", 12345);   sts[0] = Student("Kate", 12345);
   sts[1] = Student("Nick", 12346);   sts[1] = Student("Nick", 12346);
   sts[2] = Student("Mary", 12347);   sts[2] = Student("Mary", 12347);
 +  sts[3] = Student("Susan", 12348);
      
 +  cout << "--- Printing sts ---\n";
   cout << sts << endl;   cout << sts << endl;
          
 } }
 </code> </code>
cpp/class_templates.txt · Last modified: 2020/05/25 06:52 (external edit)