Sie sind auf Seite 1von 7

PROGRAM 1

#include<iostream>

#include<string>

using namespace std;

template<class T>

void print(T val)

cout << "Output value: " <<val<< endl;

int main()

int i=5;

double d=5.5;

string s("Hello World");

bool b=true;

print(i); // T is int

print(d); // T is double

print(s); // T is string

print(b); // T is bool

return 0;

PROGRAM 2

#include <iostream>

#include <string>

using namespace std;


template <typename T>

T Max (T a, T b) {

return a < b ? b:a;

int main () {

int i = 39;

int j = 20;

cout << "Max(i, j): " << Max(i, j) << endl;

double f1 = 13.5;

double f2 = 20.7;

cout << "Max(f1, f2): " << Max(f1, f2) << endl;

string s1 = "Hello";

string s2 = "World";

cout << "Max(s1, s2): " << Max(s1, s2) << endl;

return 0;

Program 3
#include <iostream>

using namespace std;

template <typename T>

void Swap(T &n1, T &n2)

T temp;
temp = n1;

n1 = n2;

n2 = temp;

int main()

int i1 = 1, i2 = 2;

float f1 = 1.1, f2 = 2.2;

char c1 = 'a', c2 = 'b';

cout << "Before passing data to function template.\n";

cout << "i1 = " << i1 << "\ni2 = " << i2;

cout << "\nf1 = " << f1 << "\nf2 = " << f2;

cout << "\nc1 = " << c1 << "\nc2 = " << c2;

Swap(i1, i2);

Swap(f1, f2);

Swap(c1, c2);

cout << "\n\nAfter passing data to function template.\n";

cout << "i1 = " << i1 << "\ni2 = " << i2;

cout << "\nf1 = " << f1 << "\nf2 = " << f2;

cout << "\nc1 = " << c1 << "\nc2 = " << c2;

return 0;

PROGRAM 4
#include <iostream>

#include <conio.h>

using namespace std;


template< typename t1>

void sum(t1 a) // defining template function

cout<<a<<endl;

template< typename t1,typename t2>

void sum(t1 a,t2 b) // defining template function

cout<<"Sum="<<a+b<<endl;

int main()

int a,b;

float x,y;

cout<<"Enter two integer data: ";

cin>>a>>b;

cout<<"Enter two float data: ";

cin>>x>>y;

sum(a,b); // adding two integer type data

sum(x,y); // adding two float type data

sum(a,x); // adding a float and integer type data

sum(a);

getch();

return 0;

Program 5
#include<iostream>

using namespace std;


template<typename T>

void print (T data)

cout<<data<<endl;

template<class T>

void print (T data,int n)

for(int i=0;i<n;i++)

cout<<data<<endl;

int main()

print(1);

print(1.5);

print(300,4);

print("Function Template",2);

return 0;

Program 6
#include <iostream>

using namespace std;

template <class T>

void fun(T a)

cout << "The main template fun(): " << a << endl;

}
template<>

void fun(int a)

cout << "Specialized Template for int type: " << a << endl;

int main()

char a='a';

int b=10;

float c=10.14;

fun(a);

fun(b);

fun(c);

Program 7
#include<iostream>

using namespace std;

template <typename T>

void fun(T x)

static int i = 10;

cout << ++i;

return;

int main()

{
fun<int>(1); // prints 11

cout << endl;

fun<int>(2); // prints 12

cout << endl;

fun<double>(1.1); // prints 11

cout << endl;

fun<int>(3); // prints 12

cout << endl;

fun<double>(1.1); // prints 11

cout << endl;

getchar();

return 0;

Das könnte Ihnen auch gefallen