I have defined a stack class containing methods for pushing and popping values onto the stack.
In the tester file (shown below), after running it, an occur occurs & the program crashes. I know this is due to the function f, which creates an error as two pointers are pointing to the same location in memory. If i comment out the line f(s) when the function is called, the pop & push functions work correctly and the output is correct.
To fix this error, I have been asked to ; Create a copy constructor for this class to fix the above problem.
I'm not really familiar with this, so any help would be appreciated in how to do this. Thanks
Main Test file
#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;
void f(Stack &a) {
Stack b = a;
}
int main() {
Stack s(2); //declare a stack object s which can store 2 ints
s.push(4); //add int 4 into stack s
//s = [4]
s.push(13); //add int 13 into stack s
//s = [4,13]
f(s); //calls the function f which takes in parameter Stack a , and sets Stack b = to it.
//error here - as 2 pointers point to the same location in memory !
cout << s.pop() << endl; //print out top element(most recently pushed) element.
//so should output 13
return 0;
}
Header File Code
#ifndef STACK_H
#define STACK_H
class Stack {
public:
//constructor
Stack(int size);
//destructor
~Stack();
//public members (data & functions)
void push(int i);
int pop();
private:
//private members (data & functions)
int stck_size;
int* stck;
int top;
};
#endif
Stack.cpp Code
#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;
Stack::Stack(int size){
stck_size = size;
stck = new int[stck_size];
top = 0;
}
Stack::~Stack() {
delete[] stck;
}
void Stack::push(int i) {
if (top == stck_size) {
cout << "Stack overflow." << endl;
return;
}
stck[top++] = i;
}
int Stack::pop() {
if (top == 0) {
cout << "Stack underflow." << endl;
return 0;
}
top--; //decrement top so it points to the last element istead of the empty space at the top.
return stck[top];
}