From 029688e143109344989b1529259e391822abb0aa Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 21 Jul 2019 03:21:00 +0700 Subject: [cpptour] Learn the basis --- cpptour/veccls.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 cpptour/veccls.cc (limited to 'cpptour/veccls.cc') 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 + +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; +} -- cgit 1.4.1