0% found this document useful (0 votes)
36 views7 pages

Templates

The document contains examples of using function templates, class templates, and operator overloading with class templates in C++. Some key points: 1. Function templates allow functions to work with arguments of different types by specifying a template parameter like template <class T>. 2. Class templates define a class where the data types of data members and functions are written as template parameters, allowing classes to work with different types. 3. Operator overloading with class templates allows operators like + to be redefined to work with user-defined classes that are templates. 4. Inheritance can be achieved with class templates by deriving one template class from another template class.

Uploaded by

whonoiam1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

Templates

The document contains examples of using function templates, class templates, and operator overloading with class templates in C++. Some key points: 1. Function templates allow functions to work with arguments of different types by specifying a template parameter like template <class T>. 2. Class templates define a class where the data types of data members and functions are written as template parameters, allowing classes to work with different types. 3. Operator overloading with class templates allows operators like + to be redefined to work with user-defined classes that are templates. 4. Inheritance can be achieved with class templates by deriving one template class from another template class.

Uploaded by

whonoiam1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

//FUNCTION TEMPLATE

template <class T>


T max(T a,T b)
{
if (a>b)
return a;
else
return b;
}
void main()
{
char ch1,ch2;
cout<<”enter two characters:”;
cin>>ch1>>ch2;
cout<<max(ch1,ch2);
int a,b;
cout<<”enter a,b:”;
cin>>a>>b;
cout<<max(a,b);
float p,q;
cout<<”enter p,q:”;
cin>>p>>q;
cout<<max(p,q);
}

//multiple arguments

#inlude <iostream>
#include<string>
using namespace std;
template<class T1,class T2>
void display( T1 x, T2 y)
{
cout<<x<<" "<<y<<"\n";
}
int main()
{
display(1999, “EBG”);
display(12.34, 1234);
return 0;
}

//overloading of function template

#include<iostream.h>
template <class T>
void print( T a)
{
cout<<a;
}
template <class T>
void print( T a, int n)
{
int i;
for (i=0;i<n;i++)
cout<<a;
}
void main()
{
print(1);
print(3.4);
print(455,3);
print(“hello”,3);
}

//overloading

#include <iostream>
#include <string>
using namespace std;
template <class T>
void display(T x)
{
cout<<”template display:” << x<< “\n”;
}
void display (int x)
{
cout<<”Explicit display: “<< x <<”\n”;
}
int main()
{
display(100);
display(12.34);
display(‘c’);
return 0;
}

//class template

#include <iostream>
using namespace std;
template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
{
a=first; b=second;
}
T getmax ();
};
template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}
int main () {
mypair <int> myobject (100,
75);
cout << myobject.getmax();
return 0;
}
//Input n numbers into an array and print the element is ascending order.(array
sorting)

template <class T>


class array
{
T *a;
int n;
public:
void getdata()
{
int i;
cout<<”enter how many no:”;
cin>>n;
a=new T[n];
for (i=0;i<n;i++)
{
cout<<enter a number:”;
cin>>a[i];
}
}
void putdata()
{
for (i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void sort( )
{
T k;
int i,j;
for(i=0;i<n-1;i++)
{
for (j=0;j<n;j++)
{
if (a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
}
};
void main()
{
array <int>x;
x.getdata();
x.sort();
x.putdata();
array <float> y;
y.getdata():
y.sort();
y.putdata();
}
//multiple arguments

#include <iostream.h>
#include <conio.h>
template <class T1,class T2>
class data
{
Public:
Data(T1 a,T2 b)
{
cout<<a<<b;
}
};
void main()
{
clrscr();
data <int, float> h(2,2.5);
data <int, char> i(15,’c’);
data < float, int > j(3.12,50);
}

//program without class templates

#include <iostream>
using namespace std;

class MyClass

public:

MyClass(int a)

{
cout<< "In Integer constructor, value = "<<a<<" size is "<<sizeof(a)<<endl;
}

MyClass(double a)
{
cout<< "In Double constructor, value = "<<a<<" size is "<<sizeof(a)<<endl;
}

MyClass(char a)

cout<< "In Character constructor, value = "<<a<<" size is


"<<sizeof(a)<<endl;

};
int main(void)

MyClass obj (10);

MyClass obj1 (10.12);

MyClass obj2 ('a');

// program with class templates

#include <iostream>

using namespace std;

template <class T>

class MyClass

public:

MyClass(T a)

{
cout<< "In constructor, value = "<<a<<" size is "<<sizeof(a)<<endl;
}
};

int main(void)

MyClass<int> obj (10);

MyClass<float> obj1 (10.12);

MyClass<char> obj2 ('a');

//operator overloading with class templates

#include <iostream>

using namespace std;

template <class T>


class Num
{
private:
T number;

public:
Num()
{
number = 0;
}

void set_number()
{
cout<<"Enter a number"<<endl;
cin>>number;
}

Num operator +(Num);

void get_number()
{
cout<<number<<endl;
}
};

template <class T>


Num <T> Num<T> :: operator +(Num <T> c)
{
Num<T> temp;
temp.number = number + c.number;
return temp;
}

int main()
{
Num <int> n1, n2, n3;
n1.set_number();
n2.set_number();
n3 = n1 + n2;
n3.get_number();
}

\\inheritance

#include <iostream>

using namespace std;

template <class T>


class one
{
protected:
T x;
T y;
void display()
{
cout<<x<<y;
}
};
template <class S>
class two : public one <S>
{
T z;
public:
two (S a, S b, S c)
{
x=a; y=b; z=c;
}
void show ()
{
cout<<x<<y<<z;
}
};
int main()
{

two <int> i (2,3,4);


i.show();
two <float> f (1.1,2.2,3.3);
f.show();
return 0;
}

You might also like