Int main()
// This program demonstrates the SearchableVector template .
#include <iostream>
#include "SearchableVector.h"
using namespace std;
int main()
{
const int SIZE = 10; // Number of elements
int count; // Loop counter
int result; // To hold search results
// Create 2 SearchableVector objects
SearchableVector<int> intTable(SIZE);
SearchableVector<double> doubleTable(SIZE);
// Store values in objects.
for (count = 0; count < SIZE; count++)
{
intTable[count] = (count * 2);
doubleTable[count] = (count * 2.14);
}
// Display the values in the objects .
cout << "These values are in int Table:\n";
for (count = 0; count < SIZE; count++)
{cout << intTable[count] << " "; }
cout << endl << endl;
cout << "These values are in doubleTable:\n";
for (count = 0; count < SIZE; count++)
cout << doubleTable [count] << " ";
cout << endl;
// Search for the value 6 in intTable .
cout << "\nSearching for 6 in intTable.\n";
result = intTable.findItem(6);
if (result == -1)
cout << "6 was not found in intTable.\n";
else
cout << "6 was found at subscript " << result << endl;
//////////////////////////////////////////////
// Search for the value 12.84 in doubleTable.
cout << "\nSearching for 12.84 in doubleTable.\n" ;
//result = doubleTable.findItem(12.84); //works fine, displays else statement
result = doubleTable.findItem(12.87); //Calls line 141 & 128 of SimpleVector header, instead of the next statement.
if (result == -1)
cout<< "12.84 was not found in doubleTable.\n";
else
cout << "12.84 was found at subscript " << result << endl;
system("pause");
return 0;
}
SimpleVector.h
// SimpleVector class template declaration. Throughout the class declaration,
// the data type paramerer is used where you wish to support any data type.
// Benefit of doing a class template, is that we don't have to creater multiple classes
// for different primitive data types, that would perform error/bounds checking for
// each of them.
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream> // Needed for bad_alloe exception
#include <new> // Needed for the exit function
#include <cstdlib>
using namespace std;
template <class T>
class SimpleVector
{
private:
T *aptr; // To point to the allocated array
//*****************************************************************
// arraySize member variable is declared as an int. This is because
// it holds the size of the array, which will be an integer value,
// regardless of the data type of the array. This is also why
// the size member function returns an int.
//*****************************************************************
int arraySize; // Number of elements in the array
void memError(); // Handles memory allocation errors
void subError(); // Handles subscripts out of range
public:
// Default constructor
SimpleVector()
{ aptr = 0; arraySize = 0; }
// Constructor declaration
SimpleVector(int);
// Copy constructor declaration
SimpleVector(const SimpleVector &);
// Destructor declaration
~SimpleVector();
// Accessor to return the array size
int size() const
{ return arraySize; }
// Accessor to return a specific element
T getElementAt(int position);
// Overloaded [] operator declaration
T &operator[](const int &);
};
//************************************************************
// Constructor for SimpleVector class. Sets the size of the
// array and allocates memory for it.
//************************************************************
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
// Allocote memory for the array.
try
{
aptr = new T [s];
}
catch (bad_alloc)
{
memError();
}
// Initialize the array .
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
//*********************************************
// Copy Constructor for SimpleVector class.
//*********************************************
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
// Copy the array size.
arraySize = obj.arraySize;
// Allocate memory for the array.
aptr = new T[arraySize];
if (aptr == 0)
memError();
// Copy the elements of obj's array.
for(int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
//***************************************
// DestrUCtor for SimpleVector class.
//***************************************
template <class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}
//************************************************************
// memError function. Displays an error message and
// terminates the program when memory allocation fails.
//************************************************************
template <class T>
void SimpleVector<T>::memError()
{
cout << "ERROR:Cannot allocate memory.\n";
exit(EXIT_FAILURE);
}
//************************************************************
// subError function. Displays an error message and
// terminates the program when a subscript is out of range.
//************************************************************
template <class T>
void SimpleVector<T>::subError()
{
cout << "ERROR: Subscript out of range.\n";
exit(EXIT_FAILURE);
}
//*****************************************************
// getElementAt function. The argument is a subscript.
// This function returns the value stored at the sub-
// cript in the array.
//*****************************************************
template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
//*********************************************************
// Overloaded [] operator. The argument is a subscript.
// This function returns a reference to the element
// in the array indexed by the subscript.
//*********************************************************
template <class T>
T &SimpleVector<T>::operator[](const int &sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
#endif
SearchableVector.h
//Applying class templates to an inherited class
#ifndef SEARCHABLEVECTOR_H
#define SEARCHABLEVECTOR_H
#include "SimpleVector.h"
template <class T>
//**********************************************************
// Each time the name SimpleVector is used in the class template,
// the type, parameter T is used with it.
class SearchableVector : public SimpleVector<T>
{
public :
//*******************************************
// Default constructor
// <T>, the type parameter, must be passed,
// since SimpleVector, is a class template.
SearchableVector() : SimpleVector<T>()
{ }
// Constructor
SearchableVector(int size) : SimpleVector<T>(size)
{ }
// Copy constructor
SearchableVector(const SearchableVector &);
//***************************************************************************
// Accessor to find an item
// Accepts an argument, and performs a simple linear search to determine
// whether the argument's value is stored in the array. If the value is
// found in the array, its subscript is returned. Otherwise, -1 is returned.
int findItem(const T);
};
//******************************************************
// Copy constructor
//******************************************************
template <class T>
SearchableVector<T>::SearchableVector(const SearchableVector &obj) :
SimpleVector<T>(obj.size())
{
for(int count = 0; count < this->size(); count++)
this->operator[](count) = obj[count];
}
//******************************************************
// findItem function
// This function searches for item. If item is found
// the subscript is returned. Otherwise -1 is returned.
//******************************************************
template <class T>
int SearchableVector<T>::findItem(const T item)
{
for (int count = 0; count <= this->size(); count++)
{
if (getElementAt(count) == item)
return count;
}
return -1;
}
#endif

New Topic/Question
Reply



MultiQuote



|