about summary refs log tree commit diff
path: root/lang/cpptour/veccls.cc
blob: 0162d231c9050ffe5124557ee28119ca1c381bc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

class Vector {
public:
  Vector (int s) : elem {new double[s]}, sz {s} {}  // construct a Vector
  double& operator[] (int i) { return elem[i]; }    // random access
  int size () { return sz; }
private:
  double* elem; // pointer to the elements
  int sz;       // the number of elements
};

double
read_and_sum (int s)
{
  Vector v (s);
  for (int i = 0; i != v.size (); ++i)
    cin >> v[i];

  double sum = 0;
  for (int i = 0; i != v.size (); ++i)
    sum += v[i];
  return sum;
}

int
main ()
{
  cout << read_and_sum (5) << endl;
}