Loading....
Coupon Accepted Successfully!

 

Class Templates Revisited

As explained earlier, the template mechanism can be used safely in every concept of C++ programming such as functions and classes. We have also learned how to define class templates. Class templates are frequently used for storage of data and can be very powerfully implemented with data structures such as stacks and linked lists. So far, our conventional style is to create a stack or linked list that manipulates or stores only a single type of data; for example, int. Suppose we want to store a float data type; we need to create a new class to handle the float data type and this can be repeated for various data types. However, the template mechanism saves our work of re-defining codes by allowing a single template-based class or function to work with different data types.
 
Reusability is one the facilities provided by inheritance, in which a class can be reused. Templates also provide reusability. Inheritance allows the reuse of a previously defined class, whereas using the template repetitive definition of functions can be avoided and a single function can be used for multiple data types. Hence, the template-based function code is reused instead of defining multiple functions.
 
17.14 Write a program to create template-based stack. Store integer and float in it.
#include<constream.h>
#include<iostream.h>
#include<stdlib.h>
template <class S>
class stack
{
S num[5];
int head;
public:
stack() {head=-1;}
void push ( S d)
{
if (head==4) cout<<endl<<“Stack is Full”;
else
{
head++;
num[head]=d;
}
}
S pop()
{
if (head==-1) {cout<<“\n Stack is empty”;
return NULL;
}
else
{
S d=num[head];
head--;
return d;
}
}
};
void main()
{
clrscr();
stack <int>s1;
for (int j=10;j<=60;j+=10)
s1.push(j);
for ( j=0;j<=5;j++)
cout<<endl<<s1.pop();
stack <float>s2;
for ( j=1;j<=6;j++)
s2.push(.1+j );
for ( j=0;j<=5;j++)
cout<<endl<<s2.pop();
}
OUTPUT
Stack is Full
50
40
30
20
10
Stack is empty
0
Stack is Full
5.1
4.1
3.1
2.1
1.1
Stack is empty
0
  
Explanation: As shown in the output, the stack class created can store any type of data. In this program, it stores integers and float type of data. The stack can store five elements. The member function push() is used to add elements in the stack, and the pop() function is used to remove elements from the stack. Both functions display messages when the stack is either full or empty. When we perform the push() operation beyond the limit, that is, pushing elements more than the stack capacity, the message displayed will be “Stack is full.” When the pop() operation is performed beyond the limit, that is, in an attempt to erase the element when there is no element stored in the stack, the message will be “Stack is empty.” In the function main(), s1 and s2 are template-type objects. Using these objects, integer and float values are added and displayed, respectively. The order in which the elements are pushed into the stack is exactly opposite (reverse) to the order in which they are popped from the stack.




Test Your Skills Now!
Take a Quiz now
Reviewer Name