1#include <iostream>
2using namespace std;
3
4template <typename T>
5void Swap(T &n1, T &n2)
6{
7 T temp;
8 temp = n1;
9 n1 = n2;
10 n2 = temp;
11}
12
13int main()
14{
15 int i1 = 1, i2 = 2;
16 float f1 = 1.1, f2 = 2.2;
17 char c1 = 'a', c2 = 'b';
18
19 cout << "Before passing data to function template.\n";
20 cout << "i1 = " << i1 << "\ni2 = " << i2;
21 cout << "\nf1 = " << f1 << "\nf2 = " << f2;
22 cout << "\nc1 = " << c1 << "\nc2 = " << c2;
23
24 Swap(i1, i2);
25 Swap(f1, f2);
26 Swap(c1, c2);
27
28 cout << "\n\nAfter passing data to function template.\n";
29 cout << "i1 = " << i1 << "\ni2 = " << i2;
30 cout << "\nf1 = " << f1 << "\nf2 = " << f2;
31 cout << "\nc1 = " << c1 << "\nc2 = " << c2;
32
33 return 0;
34}
35
1// function template
2#include <iostream>
3using namespace std;
4
5template <class T>
6T GetMax (T a, T b) {
7 T result;
8 result = (a>b)? a : b;
9 return (result);
10}
11
12int main () {
13 int i=5, j=6, k;
14 long l=10, m=5, n;
15 k=GetMax<int>(i,j);
16 n=GetMax<long>(l,m);
17 cout << k << endl;
18 cout << n << endl;
19 return 0;
20}
1// template specialization
2#include <iostream>
3using namespace std;
4
5// class template:
6template <class T>
7class mycontainer {
8 T element;
9 public:
10 mycontainer (T arg) {element=arg;}
11 T increase () {return ++element;}
12};
13
14// class template specialization:
15template <>
16class mycontainer <char> {
17 char element;
18 public:
19 mycontainer (char arg) {element=arg;}
20 char uppercase ()
21 {
22 if ((element>='a')&&(element<='z'))
23 element+='A'-'a';
24 return element;
25 }
26};
27
28int main () {
29 mycontainer<int> myint (7);
30 mycontainer<char> mychar ('j');
31 cout << myint.increase() << endl;
32 cout << mychar.uppercase() << endl;
33 return 0;
34}
1#include <bits/stdc++.h>
2using namespace std;
3template <class T>
4class Vector{
5 public:
6 T *arr;
7 T size;
8
9 Vector(int size){
10 this->size = size;
11 arr = new T[size];
12 }
13 T dotProduct(Vector &v){
14 T d = 0;
15 for(int i = 0; i<size; i++){
16 d += this->arr[i] * v.arr[i];
17 }
18 return d;
19 }
20};
21int main(){
22 Vector<int> v1(3);
23 v1.arr[0] = 1;
24 v1.arr[1] = 2;
25 v1.arr[2] = 3;
26 Vector<int> v2(3);
27 v2.arr[0] = 4;
28 v2.arr[1] = 5;
29 v2.arr[2] = 6;
30 int a = v1.dotProduct(v2);
31 cout<<a<<endl;
32 Vector<float> v3(3);
33 v3.arr[2] = 3.2;
34 v3.arr[0] = 1.2;
35 v3.arr[1] = 2.2;
36 Vector<float> v4(3);
37 v4.arr[0] = 4.4;
38 v4.arr[1] = 5.4;
39 v4.arr[2] = 6.4;
40 float a1 = v3.dotProduct(v4);
41 cout<<a1<<endl;
42
43 return 0;
44}
1// function template II
2#include <iostream>
3using namespace std;
4
5template <class T>
6T GetMax (T a, T b) {
7 return (a>b?a:b);
8}
9
10int main () {
11 int i=5, j=6, k;
12 long l=10, m=5, n;
13 k=GetMax(i,j);
14 n=GetMax(l,m);
15 cout << k << endl;
16 cout << n << endl;
17 return 0;
18}
1// sequence template
2#include <iostream>
3using namespace std;
4
5template <class T, int N>
6class mysequence {
7 T memblock [N];
8 public:
9 void setmember (int x, T value);
10 T getmember (int x);
11};
12
13template <class T, int N>
14void mysequence<T,N>::setmember (int x, T value) {
15 memblock[x]=value;
16}
17
18template <class T, int N>
19T mysequence<T,N>::getmember (int x) {
20 return memblock[x];
21}
22
23int main () {
24 mysequence <int,5> myints;
25 mysequence <double,5> myfloats;
26 myints.setmember (0,100);
27 myfloats.setmember (3,3.1416);
28 cout << myints.getmember(0) << '\n';
29 cout << myfloats.getmember(3) << '\n';
30 return 0;
31}
1template <class T>
2class mypair {
3 T values [2];
4 public:
5 mypair (T first, T second)
6 {
7 values[0]=first; values[1]=second;
8 }
9};
1template <class myType>
2myType GetMax (myType a, myType b) {
3 return (a>b?a:b);
4}
1// class templates
2#include <iostream>
3using namespace std;
4
5template <class T>
6class mypair {
7 T a, b;
8 public:
9 mypair (T first, T second)
10 {a=first; b=second;}
11 T getmax ();
12};
13
14template <class T>
15T mypair<T>::getmax ()
16{
17 T retval;
18 retval = a>b? a : b;
19 return retval;
20}
21
22int main () {
23 mypair <int> myobject (100, 75);
24 cout << myobject.getmax();
25 return 0;
26}