Design and test a reference array, Data Structure & Algorithms

Assignment Help:

Instructions

  • Design and test a reference array. Reference array stores the references to user supplied objects of different types. Just think it as a heterogeneous array which can contain elements of different types.
  • Exceptions must be used to handle all type of errors that may be encountered while using the array. The main errors are:
    • No object is found while trying to access an object from the array at a given position
    • The index may be beyond the array limits.
  • The necessary classes for the problems and also the driver main function are given. You have to provide the implementations of the member functions whose actual code is missing.

// ArrayImp.h

#include

#include

enum SortOrder { Asc, Desc};

class ArrayImp{

                protected:

                                size_t  size;

                                void ** vp;

                                ArrayImp( size_t sz);

                                ArrayImp( const ArrayImp& cp);

                                ~ ArrayImp();

                                ArrayImp& operator=(const ArrayImp & cp);

                                void * Get( size_t  index);

                                void * Put(size_t  index, const void * what);

                                void Print() const;

};

ArrayImp:: ArrayImp(size_t sz){

                if (sz > 0){

                                size = sz;

                                vp = new void* [size];

                                for ( int I = 0; I < size; i++)

                                                vp[i] = 0;

                }else{

                                vp = 0;

                                size 0;

                }

}

ArrayImp:: ArrayImp( const ArrayImp& cp){

                if ( cp.size > 0){

                                size = cp.size;

                                vp = new void * [size];

                                for ( int I = 0; i< size; i++)

                                                vp[i] = cp.vp[i];

                } else{

                                size = 0;

                                vp = 0;

                }

}

ArrayImp&  ArrayImp ::operator=(const ArrayImp & cp){

                                //  your responsibility

}

void * ArrayImp:: Get( size_t  index){

                return  vp[index];

}

void * ArrayImp:: Put(size_t  index, const void * what){

                void * oldVal = vp[index];

                vp[index] = const_cast (what);

                return oldVal;

};

void * ArrayImp:: ArrayImp:: Print() const{

                cout<<"Contents:"<

                for ( int I = 0; i< size; i++)

                                cou<

                cout<

};

// 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;

}


Related Discussions:- Design and test a reference array

Decision tree - id3 algorithm, Decision Tree - ID3 algorithm: Imagine ...

Decision Tree - ID3 algorithm: Imagine you only ever do one of the following four things for any weekend:   go shopping   watch a movie   play tennis   just

Algorithm, write an algorithm for the gpa of six students

write an algorithm for the gpa of six students

Binary trees, A binary tree is a special tree where each non-leaf node can ...

A binary tree is a special tree where each non-leaf node can have atmost two child nodes. Most important types of trees which are used to model yes/no, on/off, higher/lower, i.e.,

Implementation of a binary tree, Like general tree, binary trees are implem...

Like general tree, binary trees are implemented through linked lists. A typical node in a Binary tree has a structure as follows struct NODE { struct NODE *leftchild; i

Explain the sum of subset problem, a. Explain the sum of subset problem. Ap...

a. Explain the sum of subset problem. Apply backtracking to solve the following instance of sum of subset problem: w= (3, 4, 5, 6} and d = 13. Briefly define the method using a sta

The theta-notation, This notation bounds a function to in constant factors....

This notation bounds a function to in constant factors. We say f(n) = Θ(g(n)) if there presents positive constants n 0 , c 1 and c 2 such that to the right of n 0 the value of f

Dynamic memory management, How memory is freed using Boundary tag method in...

How memory is freed using Boundary tag method in the context of Dynamic memory management? Boundary Tag Method to free Memory To delete an arbitrary block from the free li

Program of implementation of stack using arrays, include int choice, st...

include int choice, stack[10], top, element; void menu(); void push(); void pop(); void showelements(); void main() { choice=element=1; top=0; menu()

ERM, Hi, can you give me a quote for an E-R diagram

Hi, can you give me a quote for an E-R diagram

Representation of arrays?, A representation of an array structure is a mapp...

A representation of an array structure is a mapping of the (abstract) array with elements of type T onto the store which is an array with elements of type BYTE. The array could be

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd