Using exceptions with templates. : template class « Class « C++






Using exceptions with templates.

  
#include <iostream>
using namespace std;
const int DefaultSize = 10;
class xBoundary {};

template <class T>
class Array
{
public:
    Array(int itsSize = DefaultSize);
    Array(const Array &rhs);
    ~Array() { delete [] pType;}

    Array& operator=(const Array<T>&);
    T& operator[](int offSet);
    const T& operator[](int offSet) const;

    int GetitsSize() const { return itsSize; }

    friend ostream& operator<< (ostream&, const Array<T>&);

    class xSize {};

 private:
    int *pType;
    int  itsSize;
 };

 template <class T>
 Array<T>::Array(int size):
 itsSize(size)
 {
    if (size <10 || size > 30000)
       throw xSize();
    pType = new T[size];
    for (int i = 0; i<size; i++)
      pType[i] = 0;
 }

 template <class T>
 Array<T>& Array<T>::operator=(const Array<T> &rhs)
 {
    if (this == &rhs)
       return *this;
    delete [] pType;
    itsSize = rhs.GetitsSize();
    pType = new T[itsSize];
    for (int i = 0; i<itsSize; i++)
       pType[i] = rhs[i];
 }
 template <class T>
 Array<T>::Array(const Array<T> &rhs)
 {
    itsSize = rhs.GetitsSize();
    pType = new T[itsSize];
    for (int i = 0; i<itsSize; i++)
       pType[i] = rhs[i];
 }

 template <class T>
 T& Array<T>::operator[](int offSet)
 {
    int size = GetitsSize();
    if (offSet >= 0 && offSet < GetitsSize())
       return pType[offSet];
    throw xBoundary();
    return pType[0];
 }

 template <class T>
 const T& Array<T>::operator[](int offSet) const
 {
    int mysize = GetitsSize();
    if (offSet >= 0 && offSet < GetitsSize())
       return pType[offSet];
    throw xBoundary();
 }

 template <class T>
 ostream& operator<< (ostream& output, const Array<T>& theArray)
 {
    for (int i = 0; i<theArray.GetitsSize(); i++)
       output << "[" << i << "] " << theArray[i] << endl;
    return output;
 }
int main(){
    try
    {
       Array<int> intArray(9);
       for (int j = 0; j< 100; j++)
        {
           intArray[j] = j;
           cout << "intArray[" << j << "] okay..." << endl;
        }
     }
     catch (xBoundary)
     {
        cout << "Unable to process your input!\n";
     }
     catch (Array<int>::xSize)
     {
        cout << "Bad Size!\n";
     }

     cout << "Done.\n";
     return 0;
}
  
    
  








Related examples in the same category

1.template class with type parameter
2.template class with generic parameter
3.Demonstrate a very simple safe pointer class.
4.A generic safe-array class that prevents array boundary errors.
5.Get storage off stack for array
6.template extending
7.sequence template
8.Template Version of Generic binary sorted Tree.
9.template class with two generic parameters
10.generic stack template class
11.Passing by reference and using virtual functions in exceptions.
12.Using Non-Type Arguments with Generic Classes
13.Using Default Arguments with Template Classes
14.Generic Classes: demonstrates a generic stack.
15.An Example with Two Generic Data Types
16.Applying Template Classes: A Generic Array Class
17.Explicit Class Specializations for generic template class