Monday, February 26, 2018

Programming Challenge 16.11 - Inheritance Modification

Example Files: InheritanceModification.7z

Notice: The only file that has directly changed is the SearchableVector class. The other files remain the same, and are to be found in the Example Files archive, but are not republished here.

SearchableVector.h


#ifndef SEARCHABLE_VECTOR_H_
#define SEARCHABLE_VECTOR_H_

#include "SortableVector.h"

template <class T>
class SearchableVector : public SortableVector<T>
{
    public:
        // Constructors
        SearchableVector() : SortableVector<T>()
        { }

        SearchableVector(int size) : SortableVector<T>(size)
        { }

        SearchableVector(const SearchableVector &);
   
        // Accessor function
        void findItem(const T &);
        int sortAndSearch(const T &);
};

/* **********************************************************
            SearchableVector<T>::SearchableVector()
         - Copy Constructor
   ********************************************************** */

template <class T>
SearchableVector<T>::SearchableVector(const SearchableVector &obj) :
                           SortableVector<T>(obj)
{
}

/* **********************************************************
            SearchableVector<T>::findItem() : const T &
    This function passes the value to search for in a given
    array to the sortAndSearch function. If the value exists,
    it is output. Otherwise a message indicating that the
    value does not exist is output.
   ********************************************************** */

template<class T>
void SearchableVector<T>::findItem(const T &val)
{
    int result = 0;

    result = this->sortAndSearch(val);

    if (result == -1)
    {
        cout << val << " does not exist ...\n\n";
    }
    else
    {
        cout << val << " was found at subscript position "
              << result << "\n\n";
    }
}

/* **********************************************************
            SearchableVector<T>::sortAndSearch() const T &
    This function first sorts an array, by calling the
    SortableVector class's sortItems() function, then a binary
    search algorithm is used, to find a value in it. If the
    value is found, the position is returned. Otherwise -1 is
    returned.
    ********************************************************** */

template <class T>
int SearchableVector<T>::sortAndSearch(const T &item)
{
    SortableVector<T>::sortItems();
   
    int  firstElem = 0;
    int  midPoint = 0;
    int  lastElem = SimpleVector<T>::getArraySize() - 1;
    int  position = -1;
    bool status = false;

    while (status == false && firstElem <= lastElem)
    {
        midPoint = (firstElem + lastElem) / 2;

        if (SimpleVector<T>::operator[](midPoint) == item)
        {
            position = midPoint;
            status = true;
        }

        SimpleVector<T>::operator[](midPoint) > item ? lastElem = midPoint - 1 :
                                                                 firstElem = midPoint + 1;
    }

    return position;
}

#endif

InheritanceMod.cpp


#include "SearchableVector.h"

#include <cstdlib>
#include <ctime>

#include <iostream>
using std::cin;
using std::cout;

#include <string>
using std::string;

/* **********************************************************
            print() : const <T> &obj, const string
    Outputs the object's current contents to screen.
    ********************************************************** */

template <class T>
void print(const SearchableVector<T> &obj, const string objName)
{
    cout << "These values are currently in your " << objName << ":\n";
    cout << obj << "\n\n";
}

/* **********************************************************
                                        main()
   ********************************************************** */

int main()
{
    const int    SIZE = 16;
    const string objNames[] = { "intTable", "doubleTable", "charTable" };

    srand(static_cast<unsigned int> (time(0)));

    SearchableVector<int>    intTable(SIZE);
    SearchableVector<double> doubleTable(SIZE);
    SearchableVector<char>   charTable(SIZE);

    cout << "SORTABLE AND SEARCHABLE VECTOR DEMO\n\n";
    cout << "Now filling the arrays with random values ...\n\n";
    for (int count = 0; count < SIZE; count++)
    {
        intTable[count] = rand() % SIZE + (count * 2);
        doubleTable[count] = rand() % SIZE * (count * 2.2);
        charTable[count] = rand() % SIZE - 191;
    }

    print(intTable, objNames[0]);
    print(doubleTable, objNames[1]);
    print(charTable, objNames[2]);

    cout << "Now sorting and searching for values in the arrays:\n\n";
    intTable.findItem(22);
    doubleTable.findItem(132.0);
    charTable.findItem('I');

    cout << "These are the items now in sorted order:\n\n";
    print(intTable, objNames[0]);
    print(doubleTable, objNames[1]);
    print(charTable, objNames[2]);

    cout << "Thank you for trying this program! Have a nice day!";
    cin.get();
   return 0;
}

Example Output:





No comments:

Post a Comment