Int applied programming applied programming

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: . int Applied Programming Applied Programming // destructor ~IntIndColl(); // store in index position a value void Store(int,int); void Store(int,int // retrieve from index position int Retrieve(int) const; // Here's the ADT in interface form. File name is IntIndColl.h File IntIndColl.h copy an array void Copy(const IntIndColl&); void Copy(const #ifndef INTINDCOLL_H_ #define INTINDCOLL_H_ class IntIndColl class IntIndColl { public: // // constructor - use default argument constructor so only one needed IntIndColl(int sz = 100); Applied Programming private: // // // pointer to the array itself int *collection; total size of array total int size; }; #endif How big is an instance? Applied Programming W e'll see later the use of operators instead of We'll functions. Applied Programming functions. Now for the implementation. File IntIndColl.cpp. File IntIndColl.cpp #include "IntIndColl.h" #include <cstdlib> #include <iostream> using namespace std; IntIndColl::IntIndColl(int sz) // note no default argument value here // has to be in one place or the other { size = sz; // should check for +ve collection = new int[sz]; collection int[sz } Applied Programming IntIndColl::~IntIndColl() { delete collection; } int IntIndColl::Retrieve(int index) const { if ((index < 0) || (index > size-1)) IntIndColl::void Store(int index, int value) index, int { if ((index < 0) || (index > size-1)) { cerr << "\nERROR: index out of range\n"; exit(1); } collection[index] = value; } Not quite as cosy as array[index] = value { cerr << "\nERROR: index out of range\n"; exit(1); } return collection[index]; return collection[index } and again not as nice as just array[index] Applied Programming void IntIndColl::Copy(const IntIndColl& source) void IntIndColl::Copy(const { if (source.size > size) { cerr << "\nERROR: coll. too large\n"); exit(1); } for (int i=0;i<source.size;i++) collection[i] = source.collection[i]; source.collection[i } There are alternative copy ideas. And can't we...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online