I've recently taken a course in Data Structures in Java and I can write various structures and implement them in various ways in Java easily. I'm currently transferring this knowledge to the world of C++ which is a little different. I currently wrote a header file for a Stack interface (like you would writing an interface in Java for Stack) and I want to implement it in various ways (linked lists, array, vector, etc.) so I can master the idea of implementing structures in any language. The problem I'm currently having with C++ is understanding the notion of using pointers in my Stack and references (E&) plus making sure I can write various source implementations of Stack.h. Here is my current stack header...
/*
* File: stack.h
* -------------
* Interface file of the Stack class. Follows a last-in/ first-out (LIFO) order. Includes contracts for the method bodies of Stack.
*/
#ifndef _stack_h
#define _stack_h
/*
* declaration of Stack using template with type E for any data type or object using the Stack structure.
*
*/
template <typename E>
class Stack {
public:
//constructor blank
Stack();
//destructor
~Stack();
//constructor with integer size
Stack(int);
//number of items in stack.
int size() const;
//is stack empty?
bool empty() const;
//top element of stack
const E& top() const throw(StackEmpty);
//push e onto the stack.
void push(const E& e);
//remove top element from stack
void pop() throw(StackEmpty);
//pop and return.
E popReturn();
//returns a string form of stack.
std::string toString();
};
// Error exception
class StackEmpty : public RuntimeException {
public:
StackEmpty(const std::string& err) : RuntimeException(err) {}
};
#endif
Sorry for the formatting! :) Currently I'm working on the Array and Linked List implementation of this stack. I understand that a header file creates a link between files it is included in. I want to insure that when I create a stack for testing I can use both implementations I wrote with this header. I'm also not sure if I should use the keyword virtual or not to make an official interface. I know in java when you declare an implementation for Stack you would use
Stack test = new ArrayStack();
Is that the same for C++ to use a globalized header file and use different implementations of this Stack? Also this code is burrowed from a Data Structures book in C++ but sadly the authors do not say to make these interfaces a header file or not, and where to include that error checking exception for an empty stack. I just placed it in this file. C++ hasn't been a great language to me but I know that if I wanna build larger projects like Compilers, games, audio/ video players, OS, and writing an IDE for a new language that this and C are important to master. Please give me any insight to my current situation if possible I would greatly appreciate it. If anyone also can explain pointers and references in C++ I would greatly accept the knowledge. I believe the E& is a reference but the book didn't specify. Thank you! :)
P.S. this is what I thought would work for C++ for using different implementations of a header....
#include "stack.h"
Stack test = new ArrayStack();
Stack test2 = new LinkedStack();