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

From ZCubes Wiki
Jump to navigation Jump to search
  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 T0,
 20         class T1>
 21T_A<T0,T1>::
 22T_A() : s(0),
 23        d0(NULL),
 24        d1(NULL) {
 25}
 26
 27template<class T0,
 28         class T1>
 29T_A<T0,T1>::
 30T_A(const T_A& o) : s(o.s),
 31                    d0(new T0[s]),
 32                    d1(new T1[s]) {
 33  for(int i=0; i<s; ++i) {
 34    d0[i] = o.d0[i];
 35    d1[i] = o.d1[i];
 36  }
 37}
 38
 39template<class T0,
 40         class T1>
 41T_A<T0,T1>&
 42T_A<T0,T1>::
 43operator=(const T_A<T0,T1>& o) {
 44  if(this==&o) return *this;
 45
 46  delete [] d0;
 47  delete [] d1;
 48
 49  s = o.s;
 50  d0 = new T0[s];
 51  d1 = new T1[s];
 52
 53  for(int i=0; i<s; ++i) {
 54    d0[i] = o.d0[i];
 55    d1[i] = o.d1[i];
 56  }
 57
 58  return *this;
 59}
 60
 61template<class T0,
 62         class T1>
 63T_A<T0,T1>::
 64~T_A() {
 65  delete [] d0;
 66  delete [] d1;
 67  d0 = NULL;
 68  d1 = NULL;
 69  s = 0;
 70}
 71
 72template<class T0,
 73         class T1>
 74int
 75T_A<T0,T1>::
 76get_s() const {
 77  return s;
 78}
 79
 80template<class T0,
 81         class T1>
 82T0
 83T_A<T0,T1>::
 84get_d0(const int index) const { // assuming index in in valid range
 85  return d0[index];             // [ exception-handling is needed ]
 86}
 87
 88template<class T0,
 89         class T1>
 90T0*
 91T_A<T0,T1>::
 92get_d0() const {
 93  return d0;
 94}
 95
 96template<class T0,
 97         class T1>
 98void
 99T_A<T0,T1>::
100set_d0(const T0 value) {
101  for(int i=0; i<s; ++i)
102    d0[i] = value;
103}
104
105template<class T0,
106         class T1>
107T1
108T_A<T0,T1>::
109get_d1(const int index) const { // assuming index in in valid range
110  return d1[index];             // [ exception-handling is needed ]
111}
112
113template<class T0,
114         class T1>
115T1*
116T_A<T0,T1>::
117get_d1() const {
118  return d1;
119}
120
121template<class T0,
122         class T1>
123void
124T_A<T0,T1>::
125set_d1(const T1 value) {
126  for(int i=0; i<s; ++i)
127    d1[i] = value;
128}
129
130template<class T0,
131         class T1>
132void
133T_A<T0,T1>::
134set_d0_d1(const int size,
135          const T0 t0_value,
136          const T1 t1_value) {
137  delete [] d0;
138  delete [] d1;
139
140  s = size;
141  d0 = new T0[s];
142  d1 = new T1[s];
143
144  for(int i=0; i<s; ++i) {
145    d0[i] = t0_value;
146    d1[i] = t1_value;
147  }
148}
149
150template<class T0,
151         class T1>
152ostream&
153operator<<(ostream& os,
154           const T_A<T0,T1>& o) {
155  for(int i=0; i<o.s; ++i)
156    os << o.d0[i] << endl;
157
158  for(int i=0; i<o.s; ++i)
159    os << o.d1[i] << endl;
160
161  return os;
162}