Yurttas/PL/OOL/Cplusplus/F/03/02/02/00/05/V.cpp

Revision as of 23:10, 6 November 2013 by MassBot1 (talk | contribs) (Created page with "<syntaxhighlight lang="cpp" line start="1" enclose="div">/* Copyright(C) 1998 All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc.. Perm...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 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) {
29  size = a.size;
30  data = new int[size];
31
32  for(int i=0; i<size; ++i)
33    data[i] = a.data[i];
34}
35
36V::
37~V() {
38  delete [] data;
39}
40
41V&
42V::
43operator=(const V& a) {
44  if(this==&a) return *this;
45
46  delete [] data;
47
48  size = a.size;
49  data = new int[size];
50
51  for(int i=0; i<size; ++i)
52    data[i] = a.data[i];
53
54  return *this;
55}
56
57int&
58V::
59operator[](const int i) {
60  return data[i];
61}
62
63V&
64operator+(V& a,
65          V& b) {
66  if(a.size!=b.size) {
67    cerr << "not valid!" << endl;
68    exit(1);
69  }
70
71  for(int i=0; i<a.size; ++i)
72    a.data[i] += b.data[i];
73
74  return a;
75}
76
77ostream&
78operator<<(ostream& os,
79           const V& a) {
80  int n = a.size;
81
82  for(int i=0; i<n; ++i)
83    os << a.data[i] << endl;
84  os << endl;
85
86  return os;
87}