Template Class, Multiple types in main

I need main.cpp to test the int type which is already implemented, and then double, and string. Now I have tested it by changing int to double for s1, but how can I make it go through the three types??

FiniteStack.h

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef FINITESTACK_H
#define FINITESTACK_H

#include <iostream>
#include <cstdlib>

using namespace std; 

template <typename C> 
class FiniteStack {
  private:
	 C *items; 
	 int size; 
	 int top; 
  public:
    class StackEmptyException{ };; // empty inner class for exception handling
    class StackFullException{ };;// empty inner class for exception handling
	FiniteStack();
	FiniteStack(int size); 
	//FiniteStack(const FiniteStack<C>&); 
    ~FiniteStack();
    void push(C) ;
    C pop();
    bool isEmpty();
    bool isFull();
};

template <typename C>
FiniteStack<C>::FiniteStack() {
  top = 0;
  size = 10;
  items = new int[10]; //default size
}
template <typename C> 
FiniteStack<C>::FiniteStack(int size){
  top = 0;
  this->size = size;
  items = new C[size];
}

// destructor: free all the memory allocated for the stack
template <typename C> 
FiniteStack<C>::~FiniteStack() {
  delete [] items;
}

// push a data onto the stack
template <typename C>
void FiniteStack<C>::push(C data) {
  if (isFull())
    throw StackFullException();
  items[top] = data;
  top++; // to combine two lines ==> items[top++] = data
}

// pop the data from the stack
template <typename C>
C FiniteStack<C>::pop() {
  if (isEmpty())
    throw StackEmptyException();
  --top;
  return items[top]; // to combine two lines ==> return items[--top];
}

// is stack empty?
template <typename C> 
bool FiniteStack<C>::isEmpty() {
  if (top == 0) return true;
  else return false;
}

// is stack full?
template <typename C> 
bool FiniteStack<C>::isFull() {
  if (top == size) return true;
  else return false;
}
#endif 


main.cpp

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;

#include "FiniteStack.h"

int main() {
	FiniteStack<int> s1(5);

  // Stack empty test
	if (s1.isEmpty()) {
      cout << "s1 is empty at the beginning." << endl;
  } else {
      cout << "s1 must be empty. Something's wrong!" << endl;
  }
  
    s1.push(1);
    s1.push(2);	
    s1.push(3);
    s1.push(4);
    s1.push(5);

  
  // Stack full test
	if (s1.isFull()) {
      cout << "s1 is full after five push() calls." << endl;
  } else {
      cout << "s1 must be full. Something's wrong!" << endl;
  }
  
  // pop() test: reverses the items
  cout << "Expected: 5 4 3 2 1 -->" << endl;
  for (int i = 0; i < 5; i++) {
      cout << s1.pop() << endl; 
  }
  
  // Stack empty test
  if (s1.isEmpty()) {
      cout << "s1 is empty after five pop() calls." << endl;
  } else {
      cout << "s1 must be full. Something's wrong!" << endl;
  }

  // StackFullException test
  cout << "Expected: StackFullException --> ";
  try {
    s1.push(1);
    s1.push(2);
    s1.push(3);
    s1.push(4);
    s1.push(5);
    s1.push(6);
  } catch (FiniteStack<int>::StackEmptyException) {
    cout << "Exception: cannot pop, stack is empty" << endl;
  } catch (FiniteStack<int>::StackFullException) {
    cout << "Exception: cannot push, stack is full" << endl;
  }
  
  // StackEmptyException test
  cout << "Expected: StackEmptyException --> ";
  try {
    s1.pop(); s1.pop(); s1.pop(); s1.pop(); s1.pop(); s1.pop();

  } catch (FiniteStack<int>::StackEmptyException) {
    cout << "Exception: cannot pop, stack is empty" << endl;
  } catch (FiniteStack<int>::StackFullException) {
    cout << "Exception: cannot push, stack is full" << endl;
  }
  
  return 0;

}
You could move that stuff into a templated test function and pass the parameters in a vector. e.g.
1
2
3
4
5
6
7
template <typename T>
void test(const std::vector<T> &v)
{
    FiniteStack<T> s(v.size());

    // Stack empty test
    //... 

then you call that stuff from main()
Topic archived. No new replies allowed.