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
Next revisionBoth sides next revision
cpp:class_templates [2019/05/04 04:28] – [Ένα πιο σύνθετο παράδειγμα] gthanoscpp:class_templates [2019/05/14 10:08] – [Templates κλάσεων] gthanos
Line 4: Line 4:
  
 <code cpp Box.hpp> <code cpp Box.hpp>
 +#ifndef _BOX_HPP_
 +#define _BOX_HPP_
 +
 template <typename T> template <typename T>
 class Box { class Box {
Line 31: Line 34:
   return out;   return out;
 } }
 +#endif
 </code> </code>
  
Line 62: Line 66:
  
 <code cpp Array.hpp> <code cpp Array.hpp>
- 
 #ifndef __ARRAY_H__ #ifndef __ARRAY_H__
 #define __ARRAY_H__ #define __ARRAY_H__
Line 139: Line 142:
 std::ostream &operator<<(std::ostream& out, const Array<T,size>& t) { std::ostream &operator<<(std::ostream& out, const Array<T,size>& t) {
   for(int i=0; i<size; i++) {   for(int i=0; i<size; i++) {
-    out << t.array[i] << " ";+    out << t.array[i] 
 +    if(i<size-1) 
 +      out << "";
   }   }
   return out;   return out;
Line 146: Line 151:
 </code> </code>
  
 +<code cpp ArrayUsage.cpp>
 +#include "Array.hpp"
 +#include "Student.hpp"
  
 +#define ARRAY_SIZE 10
  
 +using namespace std;
  
 +int main() {
 +  Array<int, ARRAY_SIZE> a;
 +  for(int i=0; i<ARRAY_SIZE; i++)
 +    a[i] = i;
 +  
 +  cout << a << endl;
 +  Array<int, ARRAY_SIZE> b(a);
 +  cout << b << endl;
 +  
 +  Array<Student, ARRAY_SIZE> sts;
 +  sts[0] = Student("Kate", 12345);
 +  sts[1] = Student("Nick", 12346);
 +  sts[2] = Student("Mary", 12347);
 +  
 +  cout << sts << endl;
 +    
 +}
 +</code>
cpp/class_templates.txt · Last modified: 2020/05/25 06:52 (external edit)