Copy constructor for Stack4 - write a \"copy constructor\" for the Stack class.
ID: 651657 • Letter: C
Question
Copy constructor for Stack4
- write a "copy constructor" for the Stack class.
- please fill in the implementation for the "copy constructor"
-Keep in mind we have 1 .H file and 2 .CPP files
//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
------------------------------------------------------------
//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
#include
#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
-------------------------------------------------------------------------------
//Program Using the Stack Class
//Program to demonstrate use of the Stack class.
#include
#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
hope it wil helps you very much.......
#include <iostream>
using namespace std;
class Stack
{
private:
int *p;
int top,length;
public:
Stack(int = 0);
~Stack();
void push(int);
int pop();
void display();
};
Stack::Stack(int size)
{
top=-1;
length=size;
if(size == 0)
p = 0;
else
p=new int[length];
}
Stack::~Stack()
{
if(p!=0)
delete [] p;
}
void Stack::push(int elem)
{
if(p == 0) //If the stack size is zero, allow user to mention it at runtime
{
cout<<"Stack of zero size"<<endl;
cout<<"Enter a size for stack : ";
cin >> length;
p=new int[length];
}
if(top==(length-1)) //If the top reaches to the maximum stack size
{
cout<<" Cannot push "<<elem<<", Stack full"<<endl;
return;
}
else
{
top++;
p[top]=elem;
}
}
int Stack::pop()
{
if(p==0 || top==-1)
{
cout<<"Stack empty!";
return -1;
}
int ret=p[top];
top--;
length--;
return ret;
}
void Stack::display()
{
for(int i = 0; i <= top; i++)
cout<<p[i]<<" ";
cout<<endl;
}
int main()
{
Stack s1; //We are creating a stack of size 'zero'
s1.push(1);
s1.display();
s1.push(2);
s1.push(3);
s1.push(4);
s1.push(5);
s1.display();
s1.pop();
s1.display();
s1.pop();
s1.display();
s1.pop();
s1.display();
s1.pop();
s1.display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.