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

I\'m having a hard time initializing my copyconstrutor. This is my static stack

ID: 3768033 • Letter: I

Question

I'm having a hard time initializing my copyconstrutor. This is my static stack template class code:

template
class Stack
{
private:
   T *stackArray;
   int stackSize;
   int top;

public:
   // Constructor
   Stack(int);

   // Copy constructor
   Stack(const Stack&);

   // Destructor
   ~Stack();

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

//***************************************************
// Constructor *
//***************************************************

template
Stack::Stack(int size)
{
   stackArray = new T[size];
   stackSize = size;
   top = 1;
}

//***************************************************
// Copy constructor *
//***************************************************


template
Stack::Stack(const Stack &obj)
{
   // Create the stack array.
   if (obj.stackSize > 0)
       stackArray = new T[obj.stackSize];
   else
       stackArray = nullptr;

   // 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;
}

My class name is:

Stack calc(size);

how would I create a copy constructor for that?

Explanation / Answer

I have updated the Template Stack Class definition externally by adding

template<class T>
Stack<T>::Stack(int size)
{
   // Definition is fine..
}

template<class T>
Stack<T>::Stack(const Stack<T>& obj)
{
   // Definition is fine..
}

created main method for the Copy Constructor calling and written the comments

int main()
{
   Stack calc(10); // Passing Stack size of 10 elements
   Stack size(calc); // Copy Constructor Concept assign calc object tio size objcet of stack class

   return 1;
}


Final Code for the Stack Template Class is below

#include <iostream>
using namespace std;

template<class T>
class Stack
{
private:
   T *stackArray;
   int stackSize;
   int top;
public:
   // Constructor
   Stack(int size);

   // Copy constructor
   Stack(const Stack& );

   // Destructor
   ~Stack();

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

template<class T>
// Updated External copy constructor calling syntax..I have added Stack<T>:: earlier it is Stack::
Stack<T>::Stack(int size)
{
   stackArray = new T[size];
   stackSize = size;
   top = 1;
}

template<class T>
// Updated External copy constructor calling syntax..I have added Stack<T>:: earlier it is Stack::
Stack<T>::Stack(const Stack &obj)
{
   // Create the stack array.
   if (obj.stackSize > 0)
       stackArray = new T[obj.stackSize];
   else
       stackArray = nullptr;

   // 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;
}

int main()
{
   Stack calc(10); // Passing Stack size of 10 elements
   Stack size(calc); // Copy Constructor Concept assign calc object tio size objcet of stack class

   return 1;
}

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