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

Hello can we call the copy constructor inside the class ie? Ican\'t seem to figu

ID: 3609156 • Letter: H

Question

Hello can we call the copy constructor inside the class ie? Ican't seem to figure out how to do this.

Any help would be greatly appreciated


#include <iostream>
#include "Stack.h"

using namespace std;


//*******************************
//** Copy constructor
//*******************************
Stack::Stack( const Stack& stack)
{

Node *copy = new Node;
Node *newNode = new Node;
Node *actual = new Node;;

copy = top;
actual = stack.top;

if(actual != NULL)
   {
     newNode -> data = actual->data;
     top = newNode;
     newNode ->next = NULL;
     actual = actual ->next;
     cSize++;

     while(actual != NULL)
      {cout<<actual->data<<" ";
    Node *temp = new Node;
    temp->data = actual ->data;
    temp->next = top;
    top = temp;
    cSize++;
    actual = actual->next;
       }
   }
}


//*******************************
//* Assigment overloaded
//*******************************
const Stack operator= (const Stack & rhs)
{
// Stack(rhs);

}


Explanation / Answer

Yeah you can just impliment the copy contructor so your assignmentoverload would look like this: void Stack::operator= ( const Stack& rhs ) const { if (this != rhs) { removeAll(); rhs.copy(); } }// end = operator for removeAll(); simply pop the stack until the header points tonull and "this" is the left hand side of the operation

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