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

This program is perfectly run, but I need to write copy constructor. Please help

ID: 654974 • Letter: T

Question

This program is perfectly run, but I need to write copy constructor. Please help to write the copy constructor.

///// HEADER FILE/////////

///// stack.h /////////////

//Interface File for a Stack Class

//This is the header file stack.h. This is the interface for the class Stack,

//which is a class for a stack of symbols.

#ifndef STACK_H

#define STACK_H

namespace csc161

{

    struct StackFrame

    {

        char data;

        StackFrame *link;

    };

typedef StackFrame* StackFramePtr;

class Stack

    {

    public:

        Stack( );

        //Initializes the object to an empty stack.

      Stack(const Stack& a_stack);

        //Copy constructor.

        ~Stack( );

        //Destroys the stack and returns all the memory to the freestore.

        void push(char the_symbol);

        //Postcondition: the_symbol has been added to the stack.

        char pop( );

        //Precondition: The stack is not empty.

        //Returns the top symbol on the stack and removes that

        //top symbol from the stack.

        bool empty( ) const;

        //Returns true if the stack is empty. Returns false otherwise.

    private:

        StackFramePtr top;

    };

}//csc161

#endif //STACK_H

////// stack.cpp//////////

//DISPLAY 13.19 Implementation of the Stack Class

//This is the implementation file stack.cpp.

//This is the implementation of the class Stack.

//The interface for the class Stack is in the header file stack.h.

#include <iostream>

#include <cstddef>

#include "stack.h"

using namespace std;

namespace csc161

{

    //Uses cstddef:

    Stack::Stack() : top(NULL)

    {

       //Body intentionally empty.

    }

    Stack::Stack(const Stack& a_stack) {

    //Copy constructor

    }

    Stack::~Stack( )

    {

        char next;

        while (! empty( ))

           next = pop( );//pop calls delete.

    }

//Uses cstddef:

    bool Stack::empty( ) const

    {

        return (top == NULL);

    }

void Stack::push(char the_symbol) {

       StackFramePtr temp_ptr;

       temp_ptr = new StackFrame;

       temp_ptr->data = the_symbol;

       temp_ptr->link = top;

       top = temp_ptr;

    }

//Uses iostream:

    char Stack::pop( )

    {

        if (empty( ))

        {

            cout << "Error: popping an empty stack. ";

           exit(1);

        }

        char result = top->data;

        StackFramePtr temp_ptr;

        temp_ptr = top;

        top = top->link;

delete temp_ptr;

return result;

    }

}// csc161

////// stackApp.cpp///////

//Program Using the Stack Class

//Program to demonstrate use of the Stack class.

#include <iostream>

#include "stack.h"

using namespace std;

using namespace csc161;

int main( )

{

    Stack s;

    char next, ans;

do

    {

        cout << "Enter a word: ";

        cin.get(next);

        while (next != ' ')

        {

            s.push(next);

            cin.get(next);

        }

cout << "Written backward that is: ";

        while ( ! s.empty( ) )

            cout << s.pop( );

        cout << endl;

cout << "Again?(y/n): ";

        cin >> ans;

        cin.ignore(10000, ' ');

    }while (ans != 'n' && ans != 'N');

return 0;

}

Explanation / Answer

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

namespace stacksavitch
{
    //Uses cstddef:
    Stack::Stack( ) : top(NULL)
    {
        //Body intentionally empty.
    }

    Stack::Stack(const Stack& a_stack)
         //The definition of the copy constructor is Self-Test Exercise 11.
    Stack::~Stack( )
    {
        char next;
        while (! empty( ))
           next = pop( );//pop calls delete.
    }


    //Uses cstddef:
    bool Stack::empty( ) const
    {
        return (top == NULL);
    }


    void Stack::push(char the_symbol)
          //The rest of the definition is Self-Test Exercise 10.
    //Uses iostream:
    char Stack::pop( )
    {
        if (empty( ))
        {
            cout << "Error: popping an empty stack. ";

exit(1);
        }

        char result = top->data;

        StackFramePtr temp_ptr;
        temp_ptr = top;
        top = top->link;

        delete temp_ptr;

        return result;
    }
}//stacksavitch

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