Yurttas/PL/OOL/Cplusplus/F/05/02/00/T A.cpp

  1/*
  2   Copyright(C) 1998
  3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4
  5   Permission to use, copy, modify, and distribute this
  6   software and its documentation for EDUCATIONAL purposes
  7   and without fee is hereby granted provided that this
  8   copyright notice appears in all copies.
  9
 10   date   : January 1, 1998.
 11   authorĀ : Salih Yurttas.
 12
 13   T_A.cpp
 14*/
 15
 16
 17#include "T_A.h"
 18
 19template<class T, int v>
 20T_A<T,v>::
 21T_A() : s(v),
 22        d(new T[s]) {
 23  for(int i=0; i<s; ++i)
 24    d[i] = 0;
 25}
 26
 27template<class T, int v>
 28T_A<T,v>::
 29T_A(const T_A<T,v>& o) : s(o.s),
 30                         d(new T[s]) {
 31  for(int i=0; i<s; ++i)
 32    d[i] = o.d[i];
 33}
 34
 35template<class T, int v>
 36T_A<T,v>&
 37T_A<T,v>::
 38operator=(const T_A<T,v>& o) {
 39  if(this==&o) return *this;
 40
 41  delete [] d;
 42
 43  s = o.s;
 44  d = new T[s];
 45
 46  for(int i=0; i<s; ++i)
 47    d[i] = o.d[i];
 48
 49  return *this;
 50}
 51
 52template<class T, int v>
 53T_A<T,v>::
 54~T_A() {
 55  delete [] d;
 56}
 57
 58template<class T, int v>
 59int
 60T_A<T,v>::
 61get_s() const {
 62  return s;
 63}
 64
 65template<class T, int v>
 66T
 67T_A<T,v>::
 68get_d(const int index) const { // assuming index in in valid range
 69  return d[index];             // [ exception-handling is needed ]
 70}
 71
 72template<class T, int v>
 73T*
 74T_A<T,v>::
 75get_d() const {
 76  return d;
 77}
 78
 79template<class T, int v>
 80void
 81T_A<T,v>::
 82set_d(const int size) {
 83  delete [] d;
 84
 85  s = size;
 86  d = new T[s];
 87
 88  for(int i=0; i<s; ++i)
 89    d[i] = 0;
 90}
 91
 92template<class T, int v>
 93void
 94T_A<T,v>::
 95set_d(const int index,
 96      const T value) { // assuming index is in valid range
 97  d[index] = value;    // [ exception-handling is needed ]
 98}
 99
100template<class T, int v>
101ostream& operator<<(ostream& os,
102                    const T_A<T,v>& o) {
103  for(int i=0; i<o.s; ++i)
104    os << o.d[i] << endl;
105
106  return os;
107}