about summary refs log tree commit diff
path: root/cpptour/veccls.cc
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-07-21 03:21:00 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-07-21 03:21:00 +0700
commit029688e143109344989b1529259e391822abb0aa (patch)
tree556f96273a5caed7df2e70a6ba253bcc7627bd33 /cpptour/veccls.cc
parentbe6678fbca007e73d69c9a9c5cddb8241a987149 (diff)
downloadcp-029688e143109344989b1529259e391822abb0aa.tar.gz
[cpptour] Learn the basis
Diffstat (limited to 'cpptour/veccls.cc')
-rw-r--r--cpptour/veccls.cc32
1 files changed, 32 insertions, 0 deletions
diff --git a/cpptour/veccls.cc b/cpptour/veccls.cc
new file mode 100644
index 0000000..0162d23
--- /dev/null
+++ b/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;
+}