Yurttas/PL/OOL/Cplusplus/F/03/02/02/00/13/V.cpp
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 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
62V::
63operator+(const V& a) {
64 if(size!=a.size) {
65 cerr << "not valid!" << endl;
66 exit(1);
67 }
68
69 for(int i=0; i<size; ++i)
70 data[i] += a.data[i];
71
72 return *this;
73}
74
75ostream&
76operator<<(ostream& os,
77 const V& a) {
78 int n = a.size;
79
80 for(int i=0; i<n; ++i)
81 os << a.data[i] << endl;
82 os << endl;
83
84 return os;
85}