Vector with raised exceptions : Your list « Data Types « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Data Types » Your list 
2.38.5.Vector with raised exceptions
#include <iostream>
#include <assert.h> 
#include <stdlib.h>
using namespace std;
class vect {
public:
   explicit vect(int n);
   ~vect() { delete []p; }
   int  ub() const return (size - 1)}
   vect& operator=(const vect& v);  
   void print() const;
   int&  operator[](int i;        
   vect operator+(const vect& v);
private:
   int*  p;
   int   size;
};

vect::vect(int n): size(n)
{
   if (n < 1
      throw (n);
   p = new int[n];
   if (p == 0)
      throw ("FREE STORE EXHAUSTED");
}


int& vect::operator[](int i)
{
   assert(i >= && i < size);
   return p[i];
}

vect& vect::operator=(const vect& v)
{
   int s = (size < v.size? size : v.size;

   if (v.size != size)
      cerr << "copying different size arrays "
           << size << " and " << v.size << endl;
   for (int i = 0; i < s; ++i)
      p[i= v.p[i];
   return (*this);
}


void vect::print() const
{
   for (int i = 0; i <= (size-1); ++i)
      cout << p[i<< "\t";
   cout << endl;
}

vect vect::operator+(const vect& v)
{
   assert(size == v.size);
   vect  sum(size);
   for (int i = 0; i < size; ++i)
      sum.p[i= p[i+ v.p[i];
   return sum;
}



void (int m)
{
  try {
      vect  a(m), b(m);
  }
  catch(int m)
  {
     cerr << "SIZE ERROR " << m << endl;
     g(10);      
  }
  catch(const char* error)
  {
     cerr << error << endl;
     abort();
  }
}


int main()
{
   int n = -5;   

   g(n);
}
2.38.Your list
2.38.1.List of integers
2.38.2.linked list
2.38.3.Manage list of employees based on STL
2.38.4.Implementation of a safe array type vect
2.38.5.Vector with raised exceptions
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.