Yurttas/PL/OOL/Cplusplus/F/05/01/02/03/I D 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   I_D_A.cpp
 14*/
 15
 16
 17#include "I_D_A.h"
 18
 19I_D_A::
 20I_D_A() : s(0),
 21          i_a(NULL),
 22          d_a(NULL) {
 23}
 24
 25I_D_A::
 26I_D_A(const I_D_A& o) : s(o.s),
 27                        i_a(new int[s]),
 28                        d_a(new double[s]) {
 29  for(int i=0; i<s; ++i) {
 30    i_a[i] = o.i_a[i];
 31    d_a[i] = o.d_a[i];
 32  }
 33}
 34
 35I_D_A&
 36I_D_A::
 37operator=(const I_D_A& o) {
 38  if(this==&o) return *this;
 39
 40  delete [] i_a;
 41  delete [] d_a;
 42
 43  s = o.s;
 44  i_a = new int[s];
 45  d_a = new double[s];
 46
 47  for(int i=0; i<s; ++i) {
 48    i_a[i] = o.i_a[i];
 49    d_a[i] = o.d_a[i];
 50  }
 51
 52  return *this;
 53}
 54
 55I_D_A::
 56~I_D_A() {
 57  delete [] i_a;
 58  delete [] d_a;
 59  i_a = NULL;
 60  d_a = NULL;
 61  s = 0;
 62}
 63
 64int
 65I_D_A::
 66get_s() const {
 67  return s;
 68}
 69
 70int
 71I_D_A::
 72get_i_a(const int index) const { // assuming index in in valid range
 73  return i_a[index];             // [ exception-handling is needed ]
 74}
 75
 76int*
 77I_D_A::
 78get_i_a() const {
 79  return i_a;
 80}
 81
 82void
 83I_D_A::
 84set_i_a(const int i_value) {
 85  for(int i=0; i<s; ++i)
 86    i_a[i] = i_value;
 87}
 88
 89double
 90I_D_A::
 91get_d_a(const int index) const { // assuming index in in valid range
 92  return d_a[index];             // [ exception-handling is needed ]
 93}
 94
 95double*
 96I_D_A::
 97get_d_a() const {
 98  return d_a;
 99}
100
101void
102I_D_A::
103set_d_a(const double d_value) {
104  for(int i=0; i<s; ++i)
105    d_a[i] = d_value;
106}
107
108void
109I_D_A::
110set_i_d(const int size,
111        const int i_value,
112        const double d_value) {
113  delete [] i_a;
114  delete [] d_a;
115
116  s = size;
117  i_a = new int[s];
118  d_a = new double[s];
119
120  for(int i=0; i<s; ++i) {
121    i_a[i] = i_value;
122    d_a[i] = d_value;
123  }
124}
125
126ostream&
127operator<<(ostream& os,
128           const I_D_A& o) {
129  for(int i=0; i<o.s; ++i)
130    os << o.i_a[i] << endl;
131
132  for(int i=0; i<o.s; ++i)
133    os << o.d_a[i] << endl;
134
135  return os;
136}