Include intindcollh int size howmany i data size

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: just say Applied Programming Let's see how we use the collection. #include "IntIndColl.h" // // int size, howmany, i, data; size, howmany specify size of array needed cout << "Size of array needed? "; cin >> size; declare it IntIndColl arr(size); // a safe array Array1 = Array2; Applied Programming // fill a part of the array cout << "How much of array is occupied?"; cin >> howmany; >> howmany for (i=0;i<howmany;i++) { cin >> data; arr.Store(i,data);// use store function } // test the retrieve function cout << "Which item do you want? "; cin >> i; data = arr.Retrieve(i); data arr.Retrieve(i cout << "arr[" << i << "] = " << data << endl; << endl Applied Programming Applied Programming How about letting us reference the array elements How in the usual way? in W e want to overload the operator . We What operator? int& IntIndColl::operator(int index) IntIndColl::operator(int { if ((index < 0) || (index > size-1)) { cerr << "\nERROR: index out of range\n"; exit(1); } return collection[index]; return collection[index } Applied Programming How about letting us reference the array elements How in the usual way? in W e want to overload the operator . We What operator? int& IntIndColl::operator(int index) { if ((index < 0) || (index > size-1)) { cerr << "\nERROR: index out of range\n"; exit(1); } return collection[index]; return collection[index } Applied Programming W hy are we now returning int& iinstead of just Why int nstead int? int Because we can eliminate both store and retrieve Because in one go. in A reference can appear on either side of an reference assignment, so our function returns a reference whose value can be altered. whose arr[i] = arr[j]; arr[j Applied Programming Finally, we'll also need a copy constructor so that Finally, our ADT can be passed to functions by value....
View Full Document

Ask a homework question - tutors are online