0% found this document useful (0 votes)
20 views

Lecture 06

C++ templates allow the creation of a generic stack class that can be used to store different data types. The template defines a Stack class with push, pop, and other methods that work on any type specified when instantiating the class. Main creates both an integer and character stack using the template and demonstrates pushing and popping values of each type. Templates avoid duplicating stack implementation code for each supported type.

Uploaded by

Saad Zaheer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 06

C++ templates allow the creation of a generic stack class that can be used to store different data types. The template defines a Stack class with push, pop, and other methods that work on any type specified when instantiating the class. Main creates both an integer and character stack using the template and demonstrates pushing and popping values of each type. Templates avoid duplicating stack implementation code for each supported type.

Uploaded by

Saad Zaheer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C++ Templates

1
C++ Templates
 We need a stack of operands and a stack
of operators.
 Operands can be integers and floating
point numbers, even variables.
 Operators are single characters.
 We would have to create classes
FloatStack and CharStack.
 Yet the internal workings of both classes is
the same.
2
C++ Templates
 We can use C++ Templates to create a
“template” of a stack class.
 Instantiate float stack, char stack, or stack
for any type of element we want.

3
Stack using templates
Stack.h:
template <class T>
class Stack {
public:
Stack();
int empty(void); // 1=true, 0=false
int push(T &); // 1=successful,0=stack overflow
T pop(void);

~Stack();
private:
int top;
T* nodes;
};
4
Stack using templates
Stack.cpp
#include <iostream.h>
#include <stdlib.h>
#include "Stack.cpp"

#define MAXSTACKSIZE 50

template <class T>


Stack<T>::Stack()
{
top = -1;
nodes = new T[MAXSTACKSIZE];
}
5
Stack using templates
Stack.cpp
template <class T>
Stack<T>::~Stack()
{
delete nodes;
}

template <class T>


int Stack<T>::empty(void)
{
if( top < 0 ) return 1;
return 0;
}
6
Stack using templates
Stack.cpp
template <class T>
int Stack<T>::push(T& x)
{
if( top < MAXSTACKSIZE ) {
nodes[++top] = x;
return 1;
}
cout << "stack overflow in push.\n";
return 0;
}

7
Stack using templates
Stack.cpp
template <class T>
T Stack<T>::pop(void)
{
T x;
if( !empty() ) {
x = nodes[top--];
return x;
}
cout << "stack underflow in pop.\n";
return x;
}
8
Stack using templates
main.cpp
#include "Stack.cpp"
int main(int argc, char *argv[]) {
Stack<int> intstack;
Stack<char> charstack;
int x=10, y=20;
char c='C', d='D';

intstack.push(x); intstack.push(y);
cout << "intstack: " << intstack.pop() << ", "
<< intstack.pop() << "\n";

charstack.push(c); charstack.push(d);
cout << "charstack: " << charstack.pop() << ",
"
<< charstack.pop() << "\n";
} 9

You might also like