about summary refs log tree commit diff
path: root/lang/cpptour/veccls.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lang/cpptour/veccls.cc')
-rw-r--r--lang/cpptour/veccls.cc32
1 files changed, 32 insertions, 0 deletions
diff --git a/lang/cpptour/veccls.cc b/lang/cpptour/veccls.cc
new file mode 100644
index 0000000..0162d23
--- /dev/null
+++ b/lang/cpptour/veccls.cc
@@ -0,0 +1,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;
+}