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
cpp:class_templates [2019/05/03 14:40] – created gthanoscpp: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 <typename T> template <typename T>
 class Box { class Box {
Line 31: Line 34:
   return out;   return out;
 } }
 +#endif
 </code> </code>
  
Line 36: Line 40:
  
 <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 50:
   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 << "--- Printing Values ---" << endl; 
 +  cout << kate << endl; 
 +  cout << katherine << endl; 
 +  cout << studentBox.get() << endl; 
 +  cout << "--- Destroying objects ---" << endl; 
 +}
 </code> </code>
  
 +===== Ένα πιο σύνθετο παράδειγμα =====
  
 +Ας υποθέσουμε ότι θέλουμε να κατασκευάσουμε ένα στατικό πίνακα (συγκεκριμένης χωρητικότητας) για την αποθήκευση πληροφορίας. Σε αναλογία με την κλάση **Box<Τ>** θέλουμε να φτιάξουμε ένα πίνακα στον οποίο να μπορούμε να αποθηκεύσουμε διαφορετικούς τύπους δεδομένων. Η παρακάτω κλάση **Array<T>** περιέχει ένα στατικό πίνακα δεδομένων μεγέθους //size//:
  
 +<code cpp Array.hpp>
 +#ifndef __ARRAY_H__
 +#define __ARRAY_H__
 +
 +#include <iostream>
 +#include <cstring>
 +#include <cassert>
 +
 +template <typename T, int size>
 +class Array {
 +  T array[size];
 +  
 +public:
 +  Array();
 +  Array(const Array<T,size>& a);
 +  void set(T e, int index);
 +  T get(int index) const;
 +  void rmv(int index);
 +  Array<T,size>& operator=(Array<T,size>& a);
 +  T& operator[](int index);
 +  template<typename U, int k>
 +  friend std::ostream &operator<<(std::ostream& out, const Array<U,k>& t);
 +};
 +
 +template<typename T, int size>
 +Array<T,size>::Array() {
 +  // empty
 +}
 +
 +template<typename T, int size>
 +Array<T,size>::Array(const Array<T,size>& a) { 
 +  for(int i=0; i<size; i++)
 +    array[i] = a.array[i];
 +}
 +
 +template<typename T, int size>
 +void Array<T,size>::set(T e, int index) {
 +  assert(index>=0 && index<size);
 +  array[index] = e;
 +}
 +
 +template<typename T, int size>
 +T Array<T,size>::get(int index) const {
 +  assert(index>=0 && index<size);
 +  return array[index];
 +}
 +
 +template<typename T, int size>
 +void Array<T,size>::rmv(int index) {
 +  assert(index>=0 && index<size);
 +  for(int i=index+1; i<size; i++)
 +    array[i-1] = array[i];
 +}
 +
 +template<typename T, int size>
 +Array<T,size>& Array<T,size>::operator=(Array<T,size>& a) {
 +  for(int i=0; i<size; i++)
 +    array[i] = a.array[i];
 +}
 +
 +template<typename T, int size>
 +T& Array<T,size>::operator[](int index) {
 +  assert(index>=0 && index<size);
 +  return array[index];  
 +}
 +
 +template<typename T, int size>
 +std::ostream &operator<<(std::ostream& out, const Array<T,size>& t) {
 +  for(int i=0; i<size; i++) {
 +    out << t.array[i] ;
 +    if(i<size-1)
 +      out << ", ";
 +  }
 +  return out;
 +}
 +#endif
 +</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 << "--- Printing a ---\n";
 +  cout << a << endl;
 +  
 +  Array<int, 10> b(a);
 +  
 +  cout << "--- Printing b ---\n";
 +  cout << b << endl;
 +  
 +  Array<Student,4> sts;
 +  sts[0] = Student("Kate", 12345);
 +  sts[1] = Student("Nick", 12346);
 +  sts[2] = Student("Mary", 12347);
 +  sts[3] = Student("Susan", 12348);
 +  
 +  cout << "--- Printing sts ---\n";
 +  cout << sts << endl;
 +    
 +}
 +</code>
cpp/class_templates.1556894427.txt.gz · Last modified: 2019/05/03 13:40 (external edit)