0

I am pretty new to c++, and have never worked with stack or templated classes before. I have started this program, but I am not done, I was wondering if I am at least on the right track, because I get a ton of compile errors.

The assignment:
Implement the stack data structure as a templated class using a dynamically allocated array. The top of the stack should be stored in a private data member that will either contain the subscript of the node currently on the top of the stack or "-1" if the stack is empty. You should fully test all methods implemented for your stack class using a menu driven test-driver program.

Any help and/or information you can give will be greatly appreciated... Thanks

Stack.h file - this code was given by my professor

#ifndef STACK_H
#define STACK_H

const int MAX_SIZE = 10;

class FullStack
{};

class EmptyStack
{};

template <class ItemType>

class StackType
{
        public:
                StackType (int size = MAX_SIZE);
                ~StackType();
                StackType (const StackType &right);
                void Clear ();
                bool IsFull () const;
                bool IsEmpty () const;
                void Push (ItemType item);
                ItemType Pop ();
                ItemType Top ();
                void Print ();
                StackType& operator = (const StackType &right);

        private:
                ItemType *items;
                int stackSize;
                int top;
};
#endif

Stack.cpp - What I have done so far...

#include "Stack.h"
#include <iostream>
using namespace std;

template<class ItemType>
StackType<ItemType>::StackType(int size)
{
        top = -1;
        size = MAX_SIZE;
        stackSize = size;
        items = new int [stackSize];
}

template<class ItemType>
StackType<ItemType>::~StackType ()
{
        delete [] items;
}

template<class ItemType>
StackType& StackType<ItemType>::operator = (const StackType &right)
{
        if (&right != this)
        {
                if (stackSize != right.stackSize)
                {
                        delete [] items;
                        stackSize = right.stackSize;
                        items = new ItemType[stackSize];
                }
                top = right.top;

                for (int x = 0; x < stackSize; x++)
                {
                        items[x] = right.items[x];
                }
        }
        return *this;
}

template<class ItemType>
StackType::StackType<ItemType>(const StackType &right)
{
        stackSize = right.stacksize;
        top = right.top;
        items = new int [stackSize];

        for (int x = 0; x < stackSize; x++)
        {
                items[x] = right.items[x];
        }
}

template<class ItemType>
void StackType<ItemType>::Clear()
{
        top = -1;
}

template<class ItemType>
bool StackType<ItemType>::IsEmpty() const
{
        return (top == -1);
}

template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
        return (top == stackSize-1);
}

template<class ItemType>
void StackType<ItemType>::Push(item)
{
        if (IsFull())
        {
                throw FullStack();
        }
        else
        {
                top++;
                items[top] = item;
        }
}

template<class ItemType>
ItemType StackType<ItemType>::Pop()
{
        if (IsEmpty())
        {
                throw EmptyStack();
        }
        else
        {
                top--;
        }
}

template<class ItemType>
ItemType StackType<ItemType>:: Top() const
{
        if (IsEmpty())
        {
                throw EmptyStack();
        }
        return items[top];
}

template<class ItemType>
void StackType<ItemType>::Print()
{
        int count;
        count = stackSize;

        if (StackSize >= -1)
        {
                cout << "This stack contains: ";
                while (count >= -1)
                {
                        cout << items[count]<< " ";
                }
                cout << endl;
        }
        else
        {
                throw EmptyStack();
        }
}

** -Note ** I had no clue about the Copy constructor function StackType (const StackType& right) So I just copied what was in our book just for a filler

Client Code- where most of the compile errors are from

#include <iostream>
#include "Stack.h"
using namespace std;

int main()
{
        StackType stack;

        float item;
        int num;


        cout << "How many items would you like in this stack (1 - 10): ";
                cin >> size;

        if (size > 0 && size <= 10)
                size = size;
        else
        {
                cout << endl << "Not a valid number, using default of 10";
                size = MAX_SIZE;
        }

        while (num != 0)
        {
                cout << "Please select from the following options:" << endl;
                cout << '\t' << "1  - Push an item" << endl;
                cout << '\t' << "2  - Pop an item" << endl;
                cout << '\t' << "3  - Get the top item" << endl;
                cout << '\t' << "4  - Determine if stack is full" << endl;
                cout << '\t' << "5  - Determine if stack is empty" << endl;
                cout << '\t' << "6  - Clear all items from stack" << endl;
                cout << '\t' << "7  - Print the stack" << endl;
                cout << '\t' << "8  - Copy stack" << endl;
                cout << '\t' << "9  - Assign one stack to another" << endl;
                cout << '\t' << "0  - Quit program" << endl;
                cout << endl;
                cout << '\t' << "Selection: ";
                        cin >> num;
                cout << endl;

                if (num == 1)
                {
                        cout << "Enter an integer to push into the stack: ";
                        cin >> item;
                        try
                        {
                                stack.Push (item);
                                cout << endl << item << " successfully pushed" << endl;
                        }
                        catch (FullStack exceptionObject)
                        {
                                cerr << endl << "Stack is full - unable to push item onto stack" << endl;
                        }
                }

                else if (num == 2)
                {
                        try
                        {
                                stack.Pop ();
                                cout << endl << top << " successfully popped" << endl;
                        }
                        catch (EmptyStack exceptionObject)
                        {
                                cerr << endl << "Stack is empty - unable to pop an item" << endl;
                        }
                }

                else if (num == 3)
                {
                        try stack.Top ();
                        {
                                cout << endl << items[top] << " is currently on top of the stack" << endl;
                        }
                        catch (EmptyStack exceptionObject)
                        {
                                cout << endl;
                                cerr << "Cannot retrieve integer from top of list - list is currently empty" << endl;
                        }
                }

                else if (num == 4)
                {
                        if (stack.IsFull() == true)
                        {
                                cout << endl << "The stack is currently full" << endl;
                        }
                        else
                        {
                                cout << endl << "The stack is NOT currently full" << endl;
                        }
                }

                else if (num == 5)
                {
                        if (stack.IsEmpty () == true)
                        {
                                cout << endl << "The stack is currently empty" << endl;
                        }
                        else
                        {
                                cout << endl << "The stack is NOT currently empty" << endl;
                        }
                }

                else if (num == 6)
                {
                        stack.Clear();
                        cout << endl << "The stack was successfully cleared" << endl;
                }

                else if (num == 7)
                {
                        try
                        {
                                cout << endl:
                                stack.Print ();
                                cout << endl;
                        }
                        catch (EmptyStack exceptionObject)
                        {
                                cerr << endl << "There are no items in this stack" << endl;
                        }
                }

                else if (num == 8)
                {
                        StackType (const StackType &right);
                        int y = x;

                        cout << endl << "Old Stack: ";
                        while (x >= -1)
                        {
                                cout << items[x] << " ";
                                x--;
                        }
                        cout << endl << "New Stack: ";
                        while (y >= -1)
                        {
                                cout << right.items[y] << " ";
                                y--;
                        }
                        cout << endl;
                }

                else if (num == 9)
                {
                        StackType operator = (const StackType &right)

                        int y = x;

                        cout << endl << "Old Stack: ";
                        while (x >= -1)
                        {
                                cout << items[x] << " ";
                                x--;
                        }
                        cout << endl << "New Stack: ";
                        while (y >= -1)
                        {
                                cout << right.items[y] << " ";
                                y--;
                        }
                        cout << endl;
                }

                else if (num < 0 || num > 9)
                {
                        cout << endl << "Invalid input, please try again" << endl;
                }
        }
        return 0;
2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by mitrmkar
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.