Yurttas/PL/OOL/Cplusplus/F/03/02/02/00/15/V.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   V.cpp
14*/
15
16
17#include "V.h"
18
19V::
20V(const int n,
21  const int v) : size(n),
22                 data(new int[n]) {
23  for(int i=0; i<size; ++i)
24    data[i] = v;
25}
26
27V::
28V(const V& a) : size(a.size),
29                data(new int[size]) {
30  for(int i=0; i<size; ++i)
31    data[i] = a.data[i];
32}
33
34V::
35~V() {
36  delete [] data;
37}
38
39V&
40V::
41operator=(const V& a) {
42  if(this==&a) return *this;
43
44  delete [] data;
45
46  size = a.size;
47  data = new int[size];
48
49  for(int i=0; i<size; ++i)
50    data[i] = a.data[i];
51
52  return *this;
53}
54
55int&
56V::
57operator[](const int i) {
58  return data[i];
59}
60
61V
62operator+(const V& a,
63          const V& b) {
64  if(a.size!=b.size) {
65    cerr << "not valid!" << endl;
66    exit(1);
67  }
68
69  V c(a);
70
71  for(int i=0; i<a.size; ++i)
72    c.data[i] = a.data[i] + b.data[i];
73
74  return c;
75}
76
77bool
78operator==(const V& a,
79           const V& b) {
80  if(a.size!=b.size)
81    return false;
82
83  for(int i=0; i<a.size; ++i)
84    if(a.data[i] != b.data[i]) return false;
85
86  return true;
87}
88
89ostream&
90operator<<(ostream& os,
91           const V& a) {
92  int n = a.size;
93
94  for(int i=0; i<n; ++i)
95    os << a.data[i] << endl;
96  os << endl;
97
98  return os;
99}