Making a templated stack

I have created a separate class in a separate file ("stack.cpp") to help me with using a stack for my main program that evaluates postfix equations. I want to use a templated stack but I am not sure if I can make quick changes to this code to do so or if I have to start over. I also want to know if I need to change the file type.

My 3 questions:
1:
Is it possible to convert the code below to a templated stack?

2:
If question 1 is yes, it is reasonable?

3:
When creating a templated stack, do you have to save as a header file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;
#define size 100
class Stack {
public: 
	Stack() : top(-1){}
	char pop();
	void push(char ch);
	void print ();
	char input[size];
	int top;
	
};
void Stack::push(char ch){
	if (top >= size){
		cout <<"Stack is full " <<endl;
		cin.get();
		return;
	}
	top++;
	input[top] = ch;
}
char Stack::pop(){
	if (top <0) {
	return 0;
	}
	if (top > -1){
		input[top]=NULL;
	top--;
	}
}
void Stack::print () {
	if(top > -1){
	cout << input[top];
     }
}
1. Yes
2. Yes
3. Yes

Really, all you need to do is replace all instances of char with T:

1
2
3
4
5
6
7
8
9
10
11
template <class T>
class Stack {
public:
    Stack() : top(-1), input(new T[size]) { }
    ~Stack() { delete[] T; }
    char pop();
    void push(T ch);
    void print();
    T* input;
    int top;
};


I think that making your array in the constructor solves a few errors that you're likely to encounter instead of defining it in the class definition.
More about 3

You don't actually *have* to define a templated class or function in a header.

If you only plan to use the stack in one .cpp file, you can just declare it at the head of the file. But you do need to use a header if you want to use the same templated class or function in different cpp files.

The rule is that the compiler must see the whole definition of a templated class or function before it is used in a cpp file (there's no such thing as a forward definition in this case)

Andy

PS In theory you could cut and paste the stack into the top of every cpp file, but that would not be a smart move as it would make maintenance, etc. more difficult.

PPS It would be better if made your data members private.
Last edited on
> I think that making your array in the constructor solves a few errors that you're likely to encounter instead of defining it in the class definition.
I disagree. It solves nothing, but now you need to code a destructor, a copy constructor and an assignment operator.

The only problem that you may have is if T has not default constructor. In which case you ought to use allocators (placement new).
But if you are going to do that, you could simply use std::stack

> PS In theory you could cut and paste the stack into the top of every cpp file
that's what an #include does
The alternative is to use explicit instantiation.
Last edited on
First of all, you guys are incredible. Thank you so much. Also, I noticed that the first reply added in a deconstructor(i think)

~Stack() { delete[] T; }

This is pure house keeping correct? Just removes everything created once the program stops running?

Also, T* is a pointer, correct? What exactly is its' purpose?
Topic archived. No new replies allowed.