Tuesday 19 November 2013

Templates Practice

Hey Guys,
I will strongly suggest you to practice this thing. I thought that I "knew" how to build a template but everything changes when you have to actually sit and do it, those little mistakes will come up and you will be able to fix them before a test comes up (at least that's what I say to myself to be more confident :) ).
The point here is: Have you done the workshop for this subject? (https://scs.senecac.on.ca/~oop344/pages/workshops/w8.html) I found it very useful and hopefully will help you out to clear some thoughts.
Here is the answer built for this scenario and most important: Reviewed by professor!

//Design and code a template named Array for classes that manage one-dimensional, contiguously stored lists of objects of type E. 
//The objects are by default of type double. 

template <class E = double>
class Array{
private:
   //Upon instantiation, an Array object receives the number of type E objects in the array.
   E* data;
   unsigned int arraySize;
   E dummy;
protected:
public:

   /*
   Since the template dynamically allocates space for the specified number of elements,
   your design includes a copy constructor, assignment operator and a destructor.
   */

   //Constructor:
   Array(int);
   //Destructor:
   ~Array();
   //Copy constructor:
   Array(const Array&);
   //Assignment operator:
   Array& operator = (const Array);

   unsigned int size() const;//an inline query that returns the number of elements in the array.
   E& operator[](int i);
   /*- an inline subscript operator that returns a reference to element i, 
   where the first element is at index 0. 
   If the value of i is outside array bounds, your operator returns a reference to 

   a dummy element of type E. */

  //CHALLENGE:
   friend void sort(Array<E>& a); // sorts the elements in an Array object. Receives a references to an Array object and sorts the elements in ascending order

};

template<class E>

Array<E>::Array(int n) : arraySize(n)
{
   data = new E[n];
}

template<class E>
Array<E>::~Array()
{
   delete [] data;
}

//Copy constructor:
template<class E>
Array<E>:: Array(const Array& b) : data(0)
{
    *this = b;
}

//Assignment operator:
template <class E>
Array<E>& Array<E>::operator = (const Array b)
{
   arraySize = b.size();

    if(data)
      delete [] data;

   data = new E[arraySize];
              
   for(int i = 0; i < size; i++)
       data[i] = b.data[i];
}

//an inline query that returns the number of elements in the array.
template <class E>
unsigned int Array<E>::size() const
{
   return arraySize;
}

   /*- an inline subscript operator that returns a reference to element i, 
   where the first element is at index 0. 
   If the value of i is outside array bounds, your operator returns a reference to 

   a dummy element of type E. */
template <class E>
E& Array<E>::operator[](int i)
{
   if( i >=0 && i < arraySize)
   {
      return data[i];
   }
   else
      return dummy;
}

//// sorts the elements in an Array object. Receives a references to an Array object and sorts the elements in ascending order
template <class E>
void sort(Array<E>& a)
{
   for(int i = 0; i < a.size(); i++)
   {
      for(int j = 0; j < a.size() - 1; j++)
      {
         if(a[j] > a[j+1])
         {
            auto t = a[j];
            a[j] = a[j+1];
            a[j+1] = t;
         }
      }

   }
}

No comments:

Post a Comment