};
// AExection.h
class ArrayException{
private:
size_t index; // index causing exception
public
ArrayException(size_t pos){ index = pos;}
virtual const char * GetDescription() const = 0;
size_t GetExceptionIndex() const{
return index;
}
};
class NoObjectException: public ArrayException{
public:
NoObjectException( size_t pos): ArrayException(pos){}
const char * GetDescription() const {
return "ERROR: No object at the specified index position";
}
};
class RangeException: public ArrayException{
protected:
size_t size;
public:
RangeException( size_t pos, size_t aSize): ArrayException(pos), size(aSize){}
const char * GetDescription() const {
return "ERROR: Index out of bound";
}
size_t GetArraySize() const { return size;}
}
// SafeArray.h
#include
#include< ArrayImp.h >
#include< AExection.h>
const size_t MAX = 256;
template
class SafeArray: private ArrayImp{
public:
SafeArray ( int sz = MAX);
SafeArray( const SafeArray & cp);
SafeArray & operator=(const SafeArray & cp);
~ SafeArray();
int GetSize(){ return size;}
T& Get(size_t pos) throw (NoObjectException, RangeException);
T& Put(size_t pos, const T & thisObj) throw (RangeException);
T& operator[](size_t index);
void Print() const;
};
// main driver program
#include
#include
#include< ArrayImp.h >
#include< AExection.h>
#include
int main(){
SafeArray x(10), y(10);
int a= 10, b = -100;
x.Put(0,a);
x.Put(1,b);
x.Print();
x[1] = x[0];
y = x;
y.Print();
try{
x[11] = x[3];
}
catch(const NoObjectException & exp){
cout<<"Caught: NoObjectException Exception"<
cout<<"Index Error : " <
};
catch(...){
cout<<"Caught an Exception<
};
cout<<"Trying to access beyond arrya limit"<
try{
x[11] = x[12];
}
catch(const ArrayException& exp){
cout<<
cout<<"Index Error : " <
};
catch(...){
cout<<"Caught an Exception<
};
return 0;
}