Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dear everyone, I am trying to implement a stack of any type and I overcome a pro

ID: 3554263 • Letter: D

Question

Dear everyone,

I am trying to implement a stack of any type and I overcome a problem when I wrote my deep copy constructor. I have below the code and the result with and without a copy constructor. Also, I have a test driver on main. Can you guys help me find out my debug or can you guys help me code better to get a better result. Any help is very appraciated.

Thank

#ifndef _STACK_H_

#define _STACK_H_

template

class Stack

{

private:

        ItemType item[size];

        int top;

public:

        // Create an empty stack.

        Stack() { top = -1; }

/***************************************

              Stack(Stack &obj)

              {

                     setTop(obj.getTop());

              }

              ItemType getTop()

              {

                     return top;

              }

              void setTop(ItemType w)

              {

                     top=w;

              }

****************************************/

        void Push(ItemType x)

        {

                    

                     item[++top] = x;

             

        }

              void MakeEmpty( )

              {

                     top = -1;

              }

              bool IsEmpty()

              {

                     if(top == -1) return 1;

                     return 0;

              }

              bool IsFull()

              {

              if(top == size) return 1;

              return 0;

              }

              int length()

              {

                     int i;

                     for(i = 0; i<=top;)

                     {

                           i++;

                     }

                    

                     return i;

             

              }

              void Print()

              {

                     int i;

                     cout<<"Stack: "<

                     for(i=0;i<=top;i++)

                     {

                           cout<

                     }

                     cout<

                    

              }

              void Pop(ItemType &x)

              {

                     x = item[top];

                     top--;

              }

        bool empty() { return top == -1; }

              ~Stack()

              {

                     delete[] item;

              }

             

};

#endif

#include

#include

#include "Stack.h"

using namespace std;

void main()

{

       Stack IntStack;

       int x;

              IntStack.Pop(x);

              IntStack.Push(11);

              IntStack.Push(22);

              cout << "int length 1 = " << IntStack.length() << endl;

              IntStack.Pop(x);

              IntStack.Push(33);

              cout << "int length 2 = "<< IntStack.length() << endl;

              cout << "The int stack contains:"<< endl;

              IntStack.Print();

              IntStack.Push(44);

              IntStack.Push(55);

              IntStack.Push(66);

              if(IntStack.IsFull() == false)

                     cout << "The int stack is not full !"<< endl;

              else

                     cout << "The int stack is full !" << endl;

              Stack IntStack2(IntStack);

              cout << "The int stack2 contains: " << endl;

              IntStack2.Print();

              IntStack2.MakeEmpty();

              cout << "The int stack3 contains: "<< endl;

              IntStack2.Print();

   system("pause");

}

Result With implementing my Copy Constructor

Result WITHOUT implementing my Copy Constructor

#ifndef _STACK_H_

#define _STACK_H_

template

class Stack

{

private:

        ItemType item[size];

        int top;

public:

        // Create an empty stack.

        Stack() { top = -1; }

/***************************************

              Stack(Stack &obj)

              {

                     setTop(obj.getTop());

              }

              ItemType getTop()

              {

                     return top;

              }

              void setTop(ItemType w)

              {

                     top=w;

              }

****************************************/

        void Push(ItemType x)

        {

                    

                     item[++top] = x;

             

        }

              void MakeEmpty( )

              {

                     top = -1;

              }

              bool IsEmpty()

              {

                     if(top == -1) return 1;

                     return 0;

              }

              bool IsFull()

              {

              if(top == size) return 1;

              return 0;

              }

              int length()

              {

                     int i;

                     for(i = 0; i<=top;)

                     {

                           i++;

                     }

                    

                     return i;

             

              }

              void Print()

              {

                     int i;

                     cout<<"Stack: "<

                     for(i=0;i<=top;i++)

                     {

                           cout<

                     }

                     cout<

                    

              }

              void Pop(ItemType &x)

              {

                     x = item[top];

                     top--;

              }

        bool empty() { return top == -1; }

              ~Stack()

              {

                     delete[] item;

              }

             

};

#endif

#include

#include

#include "Stack.h"

using namespace std;

void main()

{

       Stack IntStack;

       int x;

              IntStack.Pop(x);

              IntStack.Push(11);

              IntStack.Push(22);

              cout << "int length 1 = " << IntStack.length() << endl;

              IntStack.Pop(x);

              IntStack.Push(33);

              cout << "int length 2 = "<< IntStack.length() << endl;

              cout << "The int stack contains:"<< endl;

              IntStack.Print();

              IntStack.Push(44);

              IntStack.Push(55);

              IntStack.Push(66);

              if(IntStack.IsFull() == false)

                     cout << "The int stack is not full !"<< endl;

              else

                     cout << "The int stack is full !" << endl;

              Stack IntStack2(IntStack);

              cout << "The int stack2 contains: " << endl;

              IntStack2.Print();

              IntStack2.MakeEmpty();

              cout << "The int stack3 contains: "<< endl;

              IntStack2.Print();

   system("pause");

}

Result With implementing my Copy Constructor

Result WITHOUT implementing my Copy Constructor

Explanation / Answer

Dear,

#include<iostream>
using namespace std;

template<class T>
class IntStack
{
private:
   T *stackArray; // Pointer to the stack array
   int stackSize;    // The stack size
   int top;          // Indicates the top of the stack

public:
   // Constructor
   IntStack(int);

   // Copy constructor
   IntStack(const IntStack &);

   // Destructor
   ~IntStack();

   // Stack operations
   void push(T);
   void pop(T &);
   bool isFull() const;
   bool isEmpty() const;
};

//***********************************************
// Constructor                                  *
// This constructor creates an empty stack. The *
// size parameter is the size of the stack.     *
//***********************************************
template<class T>
IntStack<T>::IntStack(int size)
{
   stackArray = new T[size];
   stackSize = size;
   top = -1;
}

//***********************************************
// Copy constructor                             *
//***********************************************
template<class T>
IntStack<T>::IntStack(const IntStack &obj)
{
   // Create the stack array.
   if (obj.stackSize > 0)
      stackArray = new T[obj.stackSize];
   else
      stackArray = NULL;

   // Copy the stackSize attribute.
   stackSize = obj.stackSize;

   // Copy the stack contents.
   for (int count = 0; count < stackSize; count++)
      stackArray[count] = obj.stackArray[count];

   // Set the top of the stack.
   top = obj.top;
}

//***********************************************
// Destructor                                   *
//***********************************************
template<class T>
IntStack<T>::~IntStack()
{
   delete [] stackArray;
}

//*************************************************
// Member function push pushes the argument onto *
// the stack.                                     *
//*************************************************
template<class T>
void IntStack<T>::push(T num)
{
   if (isFull())
   {
      cout << "The stack is full. ";
   }
   else
   {
      top++;
      stackArray[top] = num;
   }
}

//****************************************************
// Member function pop pops the value at the top     *
// of the stack off, and copies it into the variable *
// passed as an argument.                            *
//****************************************************
template<class T>
void IntStack<T>::pop(T &num)
{
   if (isEmpty())
   {
      cout << "The stack is empty. ";
   }
   else
   {
      num = stackArray[top];
      top--;
   }
}

//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise.                     *
//***************************************************
template<class T>
bool IntStack<T>::isFull() const
{
   bool status;

   if (top == stackSize - 1)
      status = true;
   else
      status = false;

   return status;
}

//****************************************************
// Member funciton isEmpty returns true if the stack *
// is empty, or false otherwise.                     *
//****************************************************
template<class T>
bool IntStack<T>::isEmpty() const
{
   bool status;

   if (top == -1)
      status = true;
   else
      status = false;

   return status;
}

int main()

{

char catchVar1; // To hold values popped off the stack

double catchVar2;

   // Define a stack object to hold 5 values.

   //IntStack<int> stack(5);

   IntStack<char> stack1(5);

   IntStack<double> stack2(5);

   // Push the values O,L,L,Eand H onto the stack.

   cout << "Pushing O ";

   stack1.push('O');

   cout << "Pushing L ";

   stack1.push('L');

   cout << "Pushing L ";

   stack1.push('L');

   cout << "Pushing E ";

   stack1.push('E');

   cout << "Pushing H ";

   stack1.push('H');

  

// Pop the character values off the stack.

   cout << "Popping... ";

   stack1.pop(catchVar1);

   cout << catchVar1 << endl;

   stack1.pop(catchVar1);

   cout << catchVar1 << endl;

   stack1.pop(catchVar1);

   cout << catchVar1 << endl;

   stack1.pop(catchVar1);

   cout << catchVar1 << endl;

   stack1.pop(catchVar1);

   cout << catchVar1 << endl;

   // Push the values 5.5,4.4,3.3,2.2and1.1 onto the stack.

   cout << "Pushing 5.5 ";

   stack2.push(5.5);

   cout << "Pushing 4.4 ";

   stack2.push(4.4);

   cout << "Pushing 3.3 ";

   stack2.push(3.3);

   cout << "Pushing 2.2 ";

   stack2.push(2.2);

   cout << "Pushing 1.1 ";

   stack2.push(1.1);

   

    // Pop the double values off the stack.

   cout << "Popping... ";

   stack2.pop(catchVar2);

   cout << catchVar2 << endl;

   stack2.pop(catchVar2);

   cout << catchVar2 << endl;

   stack2.pop(catchVar2);

   cout << catchVar2 << endl;

   stack2.pop(catchVar2);

   cout << catchVar2 << endl;

   stack2.pop(catchVar2);

   cout << catchVar2 << endl;

   system("pause");

   return 0;

}

//End main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote