No trochu som to Rule of three postudoval, tak dufam ze to je aspon o nieco lepsie :-)
// Stack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Stack.h"
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
Stack::Stack()
{
Stack::sizeOfStack = 5;
Stack::numbersInStack = 0;
Stack::st = new int[Stack::sizeOfStack];
}
Stack::Stack(const Stack &n) : // copy constructor
st(new int[n.numbersInStack]),
numbersInStack(n.numbersInStack),
sizeOfStack(n.sizeOfStack)
{
std::copy(n.st,n.st+n.numbersInStack,st);
}
Stack& Stack::operator=(const Stack &source) { // copy assignment constructor
int *newData = new int[source.numbersInStack];
numbersInStack = source.numbersInStack;
sizeOfStack = source.sizeOfStack;
std::copy(source.st, source.st + source.numbersInStack, newData);
delete[] st;
st = newData;
return *this;
}
Stack::~Stack() { // destructor
delete[] Stack::st;
}
void Stack::push(int num) {
Stack::st[numbersInStack++] = num;
if (numbersInStack == sizeOfStack) {
sizeOfStack *= 2;
int *newSt = new int[sizeOfStack];
std::copy(st, st + numbersInStack, newSt);
delete[] st;
st = newSt;
}
}
int Stack::size() {
return Stack::numbersInStack;
}
bool Stack::isEmpty() {
return Stack::numbersInStack == 0;
}
int Stack::pop() {
assert(!(Stack::numbersInStack == 0));
return Stack::st[--numbersInStack];
}
void Stack::print() {
for (int i = 0; i < Stack::numbersInStack; ++i)
std::cout << Stack::st[i] << " ";
std::cout << std::endl;
}
#ifndef STACK
#define STACK
#include <algorithm>
class Stack {
private:
unsigned short sizeOfStack;
unsigned short numbersInStack;
int *st = NULL;
public:
bool isEmpty();
int pop();
void print();
void push(int num);
int size();
Stack();
Stack(const Stack &n);
~Stack();
Stack &operator=(const Stack &source);
};
#endif