From 07b39e9592829bacb04e8bbac541259c1cff917e Mon Sep 17 00:00:00 2001 From: Terrance Mortimer <terrance.mortimer.1@city.ac.uk> Date: Wed, 16 Dec 2020 15:55:24 +0000 Subject: [PATCH] Update q5.cc --- q5.cc | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 q5.cc diff --git a/q5.cc b/q5.cc new file mode 100644 index 0000000..c2664cf --- /dev/null +++ b/q5.cc @@ -0,0 +1,78 @@ +#include <iostream> + +#include <vector> + +using namespace std; // File: q5.cc + +class zample { + + vector<int> vi; + + public: + + zample(vector<int> some_vi) : vi(some_vi) {} + + const vector<int> & get_data() const { return vi; } + +}; + +// The code will define an overload for the << & >> operators. +// The << operator will be sent to the standard output as a 'string' +// The >> operator will accept input from the standard input + +ostream& operator<<(ostream& os, const zample& za) + { + // Return the number of elements, then the elements themselves + + vector<int>values = za.get_data(); + + os << values.size(); + os << ":"; + + for(std::vector<int>::size_type i = 0; i <values.size(); i++) + { + os << values[i]; + os << " "; + + } + + return os; + } + + istream& operator>>(istream& is, zample& za) + { + char c; + int n = 0; + is >> n; + is >> c; + vector<int>input(n); + for(int i = 0; i <n; i++) + { + is >> input[i]; + } + zample zb(input); + za = zb; + + return is; + } + + +int main() { + + vector<int> vi = {11, 12, 13, 14, 15}; + + zample z1(vi); + + cout << z1 << endl; /* should print: <5: 11 12 13 14 15> I.e., <size: elements> */ + + cin >> z1 >> z1; /* should be able to read “<5: 11 12 13 14 15><6: 21 22 23 24 25 26>a” (two samples on the same line, stuck together, with a character ‘a’ immediately following the 2nd one. */ + + cout << z1 << endl; /* should print: <6: 21 22 23 24 25 26> */ + + char c; + + cin >> c; cout << c << endl; /* should print ‘a’ – the char after the 2nd zample above. */ + + return 0; + +} -- GitLab