Passing by reference and using virtual functions in exceptions. : template class « Class « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Class » template classScreenshots 
Passing by reference and using virtual functions in exceptions.
  
#include <iostream>
using namespace std;
const int DefaultSize = 10;

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

    Array& operator=(const Array&);
    int& operator[](int offSet);
    const int& operator[](int offSetconst;

    int GetitsSize() const return itsSize; }

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

    class xBoundary {};
    class xSize
    {
    public:
       xSize(int size):itsSize(size) {}
       ~xSize(){}
       virtual int GetSize() { return itsSize; }
       virtual void PrintError() 
       
           cout << "Size error. Received: ";
           cout << itsSize << endl; 
       }
    protected:
       int itsSize;
    };

    class xTooBig : public xSize
    {
    public:
       xTooBig(int size):xSize(size){}
       virtual void PrintError() 
       
           cout << "Too big! Received: ";
           cout << xSize::itsSize << endl; 
       }
    };

    class xTooSmall : public xSize
    {
    public:
       xTooSmall(int size):xSize(size){}
       virtual void PrintError() 
       
           cout << "Too small! Received: ";
           cout << xSize::itsSize << endl; 
       }
    };

    class xZero  : public xTooSmall
    {
    public:
       xZero(int size):xTooSmall(size){}
       virtual void PrintError() 
       
           cout << "Zero!!. Received: " ;
           cout << xSize::itsSize << endl; 
       }
    };

    class xNegative : public xSize
    {
    public:
       xNegative(int size):xSize(size){}
       virtual void PrintError() 
       
           cout << "Negative! Received: ";
           cout << xSize::itsSize << endl; 
       }
    };

 private:
    int *pType;
    int  itsSize;
 };

 Array::Array(int size):
 itsSize(size)
 {
    if (size == 0)
       throw xZero(size);
    if (size > 30000)
       throw xTooBig(size);
    if (size <1)
       throw xNegative(size);
    if (size < 10)
        throw xTooSmall(size);

     pType = new int[size];
     for (int i = 0; i<size; i++)
       pType[i0;
  }

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

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

   int main()
   {

      try
      {
         Array intArray(9);
         for (int j = 0; j< 100; j++)
         {
            intArray[j= j;
            cout << "intArray[" << j << "] okay...\n";
         }
      }
      catch (Array::xBoundary)
     {
        cout << "Unable to process your input!\n";
     }
     catch (Array::xSize& theException)
     {

        theException.PrintError();
     }
     catch (...)
     {
        cout << "Something went wrong!\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.Using exceptions with templates.
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.