CPP Snippets - templates - with generalized data types and stack class implementation with tempaltes

template with data types.

#include
using namespace std;


template
T add(T a,T b)
{

T c =a+b;
return c;
}
int main()
{

int a=4,b=5;
float x=1.6,y=1.0;

int c = add(a,b);
float z = add(x,y);
cout<
return 0;
}



-virtual-machine:~/CPP/templates$ cat template_class.cc 
 template with class.


#include
using namespace std;

template
class Stack
{
private:
T *a;
int top;

public:
Stack(int num)
{
a = new T[num];
}

void pushback(T);
void pop();

};
template void Stack::pushback(T t)
{
a[top++] = t;
}

template void Stack::pop()
{
a[top--]='\0';
}
int main()
{

Stack integer(100);
Stack String(10); 
string string1="ten";

integer.pushback(10);
String.pushback(string1);


return 0;
}


No comments :

Post a Comment