How to Use Template Class or Function in C++ (Stack Implemantation)
How to Use Template Class or Function in C++ (Stack Implemantation)
1-How to use a function with different data types in c++. i mean, i have a function
that name is abs() and i want to use it just like this:
int x;foo(x); or
float x;foo(x); or
char x;foo(x)…
The solution is simple for this problem:Overloadding isn’t it?
2-Alright, if i want to store my datas in a stack and when i chosee my data type(int,char,float), compiler could create a stack that can work data type which i have choosed. Is it possible? Of course:)
So firstly, i want to solve my first problem using templates:
template <class type_name>
void foo(type_name x){
cout<<x<<endl;
}
You can call this function with different data types.For example:
foo(3);foo(“a”);foo(3.14);
What is the difference between overloading and template class?When we overload a function compiler choose a correct version of the function in runtime and we have to write the function for each data types that we have.
On the other hand,if we use templates compiler creates a placeholder for our arguments.And in runtime compiler changes it with our data type.So that we dont have to write the same codes for different data types.
I dont mean overloading is unnecessary.I just want to show the differences between overloading a function and creating a function using template class.
Next topic is “How to implement a stack data type with using template class?”.
#include <cstdlib>
#include <iostream>
template <class node_element>
class Node{
public:
node_element data;
Node * next;
};
template <class stack_type>
class Stack{
public:
Stack();
void add(stack_type);
stack_type pop();
stack_type top();
bool isempty();
void print();
int get_count(){return count;}
private:
Node<stack_type> ptr;
int count;
};
template <class stack_type>
Stack<stack_type>::Stack(){
count=0;
ptr.next=NULL;
}
template <class stack_type>
bool Stack<stack_type>::isempty(){
return (get_count()==0)?1:0;
}
template <class stack_type>
void Stack<stack_type>::add(stack_type c){
Node<stack_type>*newNode=new Node<stack_type>;
newNode->data = c;
if(isempty()){
ptr.next= newNode;
newNode->next = NULL;
}else{
newNode->next = ptr.next;
ptr.next = newNode;
}
count++;
}
template <class stack_type>
stack_type Stack<stack_type>::pop(){
if(get_count()==0){
cout<<"Stack is empty!"<<endl;
}else{
Node<stack_type> * temp = ptr.next;
ptr.next = temp->next;
stack_type data= temp->data;
delete temp;
count--;
return data;
}
}
template <class stack_type>
stack_type Stack<stack_type>::top(){
return ptr.next->data;
}
template <class stack_type>
void Stack<stack_type>::print(){
Node<stack_type> * explorer=ptr.next;
while(ptr.next!=0){
cout<<ptr.next->data<<endl;
ptr.next=ptr.next->next;
}
ptr.next=explorer;
}
int main(){
Stack<int> intStack;
intStack.add(3);
intStack.add(4);
intStack.add(5);
intStack.pop();
intStack.print();
Stack<char> charStack;
charStack.add('a');
charStack.add('b');
charStack.add('c');
charStack.print();
charStack.pop();
charStack.print();
}
Page about: How to Use Template Class or Function in C++ (Stack Implemantation), C++ Template,Stack Template,overloading vs template,using templates