Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Templates in C++

Tags:

c++

templates

I have a homework that asks me to do the following:

Design a template class named FlexArray which offers flexible array indexes. The user of the class can set the lower index and upper index when the object is declared.

Examples of user code:

FlexArray a (1,5); // lower index is 1 and the upper index is 5 FlexArray b(-5, 10); // lower index is -5 and the upper index is 10

Provide the following functions for your class:

  1. default constructor
  2. parameterized constructor in which the user specified lower index and upper index
  3. destructor
  4. copy constructor
  5. assignment operator
  6. overloaded [ ] operator with similar semantics to [ ] already used with built-in arrays.

Errors on PRE conditions may be handled with assert statements or try/catch blocks. There is no resizing of the array offered. Subscripts must be in range.

The book is really no help for creating templates. I was hoping for someone to provide me with some direction on this problem and to see if my code is on the right track. I've tried to start this problem by the very limited scope of the book and different resources online and got:

#ifndef H_templates
#define H_templates

#include <iostream>
using namespace std;

template <typename T>           
class FlexArray
{  public:                  
    FlexArray();            // POST: empty FlexArray
    FlexArray(LI,UI);               // POST: Parameterized Constructor
    ~FlexArray();           // POST: destructor
    CopyArr(Array* sourceArray, Array* destinationArray, size);             // POST: Copies array

    //Overloading the assignment operators to add arrays(?) Unsure if
    // this is what is meant by the original question
    FlexArray operator+
      (const FlexArray& otherFlexArray) const;
      //Overload the operator +
    FlexArray operator-
      (const FlexArray& otherFlexArray) const;
      //Overload the operator -
    FlexArray operator[]
      (const FlexArray& otherFlexArray) const;
      //Overload the operator []

private:
    T FlexArray[size];      // Flex array   
    int size;               // number of items Array
    int LI;                 //Lower Index
    int UI;                 //Upper Index
};

template <typename T>  
FlexArray<T>::FlexArray ()  
// POST: empty FlexArray
{    size = 0;  }

template <typename T>  
FlexArray<T>::~FlexArray()  
// POST: destructor
{    }
template <typename T>  
FlexArray<T>::CopyArr( Array* sourceArray, Array* destinationArray, size)   
//Pre: Takes 3 arguments, the original array, the array to copy too, and, the size of array
// POST: Copies the array
{   
    for(int i=0; i<size; i++){
        sourceArray[i] = destinationArray[i]
    }
}

#endif
like image 634
Nick Avatar asked Oct 21 '11 19:10

Nick


1 Answers

You're off to a good start. A couple of things to point out.

The assignment asks for a default constructor, BUT also states that resizing the array is not supported. These two requirements logically conflict -- your assumption (to use size=0) seems logical, but then this default constructed object will always be empty. Not a huge problem, just a logical disconnect in the requirements.

A paramaterized constructor taking the upper and lower bounds. You've started on this as:

FlexArray(LI,UI);               // POST: Parameterized Constructor

However, LI and UI will need types. Since you have to support negative indices, this should be a signed type, like int.

A copy-constructor, is a constructor that takes an object of the same type. You have not declared one of these. It should have the form:

FlexArray(const FlexArray&);

The assignment operator is the = operator that allows you to do this:

FlexArray a, b;
b = a;

You haven't declared one of these. It should take the form:

FlexArray& operator=(const FlexArray&);

The implementation will be similar to the copy-constructor (in fact, the copy-constructor could simply be implemented in terms of the assignment operator).

An overloaded [] operator. You have declared one of these, but it's not really in the correct form -- doesn't take the appropriate argument types. The usage will look like this:

FlexArray arr(-5, 10);

// This is a call to operator[]
arr[3] = value;

Given that, try to think about what argument type it should take.

Now, on to the functional requirements. Given an upper and lower bound, you have to create an array that can be indexed using those boundaries. Think about what you need to know in order to do that. I will suggest that you need to know the difference of the upper and lower bound (this will be the SIZE of your array). You should check in the constructor that the upper bound is greater than the lower bound, or you cannot effectively create this array.

Now, to actually build your array of objects, you will need to dynamically allocate some memory for them. You have a try at this with:

T FlexArray[size];      // Flex array

But this has some problems. First off, I don't think you can name it FlexArray as this will collide with the name of your class. Secondly, this requires that size is a compile time constant, which runs afoul of our requirements. So, you will need to dynamically allocate the internal array of T objects (using new if you haven't learned about smart pointers yet). Remember to deallocate this array of objects in your destructor.

Now functionally, how will the [] work? Requirement is to do bounds checking, so given an index you have to know if it is too low (outside the lower bound) or too high (outside the upper bound) and raise an appropriate error. Now you have a dynamically allocated (0-based) array of T objects -- given an index that is user specified you need to find the appropriate object to return. Think about how to make that happen.

Also, you've declared + and - operators, though the requirement does not specify those should be there. I would suggest taking them out. The + operator implies that the array will be resized (which is contradicted by the requirements), and the - is ambiguous, what does it mean to subtract two arrays? Both of these functions could be valid, but for this assignment are unnecessary.

No more hints :)

like image 110
Chad Avatar answered Oct 12 '22 16:10

Chad