0% found this document useful (0 votes)
54 views15 pages

Object-Oriented Programming: Templates

The document discusses templates in C++. Templates allow classes and functions to be generalized for different data types. A template class can be used to create classes like Vector that work for any numeric data type. The document provides examples of template classes for Vectors that can store integers or floats. It also discusses class templates with multiple type parameters and member function templates defined outside the class.

Uploaded by

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

Object-Oriented Programming: Templates

The document discusses templates in C++. Templates allow classes and functions to be generalized for different data types. A template class can be used to create classes like Vector that work for any numeric data type. The document provides examples of template classes for Vectors that can store integers or floats. It also discusses class templates with multiple type parameters and member function templates defined outside the class.

Uploaded by

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

Object Oriented Programming Lecture 4

2nd class Asst. Lecture Omar Nowfal

Templates:
Templates enable us to define generic classes and functions and thus provide support for
generic programming. A template can be used to create a family of classes or functions.
For example, a class template for an array class would enable us to create arrays of various data
types such as int array and float array. Templates for a function , say mul( ) ,that would help us
to create various versions of mul( ) for multiplying int , float and double types values. The
templates are sometimes called parameterized classes or functions.

Class Templates:
The general form of class template is:

Example:
The following program is represents the integer vector:
#include <iostream>
#include <conio.h>
using namespace std;
class Vector
{
  int *V;
  int Size;
public:
  Vector(int M) :Size(M)
  {

49 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

    V = new int[Size]();
  }
  void operator=(int *);
  void output();
  int operator*(Vector); //Scaler Product
};

void Vector::output()
{
  for(int i=0;i<Size;i++)
    cout<<V[i]<<"   ";
  cout<<"\n"<<endl;
}

int Vector::operator*(Vector A)
{
  int Sum=0;
  for (int i=0;i<Size;i++)
    Sum+=this‐>V[i]*A.V[i];
  return Sum;
}

void Vector::operator=(int *S)
{
  for(int i=0;i<Size;i++)
    V[i]=S[i];
}

void main()
{
  int X[3]={1,2,3};
  int Y[3]={4,5,6};
  Vector V1(3);
  Vector V2(3);
  [Link]();
  [Link]();
  V1=X;

50 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

  V2=Y;
  [Link]();
  [Link]();
  cout<<"The Scalar Product of X & Y = "<<V1*V2;
  getch();
}

The output of the program above is;


0 0 0
0 0 0

1 2 3
4 5 6
The Scalar product of X & Y = 32

No if we want to build the above program in General form (except int or float or double variable)
we must use Template Class.
The following program represent the vector program but in General form:
#include <iostream>
#include <conio.h>
using namespace std;
template <class T>
class Vector
{
  T *V;
  int Size;
public:
  Vector(int S):Size(S)
  {
    V=new T[Size]();
  }
 
void operator=(T *A)
{
    for (int i=0;i<Size;i++)
      *(V+i)=A[i];

51 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

}
 
void output()
{
    for (int i=0;i<Size;i++)
      cout<<V[i]<<"   ";
    cout<<"\n"<<endl;
}
 
T operator*(Vector A)
{
    T Sum=0;
    for (int i=0;i<Size;i++)
      Sum+=this‐>V[i]*A.V[i];
    return Sum;

};

void main()
 {
   int X[3]={1,2,3};
   int Y[3]={4,5,6};
   Vector<int> V1(3);
   Vector<int> V2(3);
   [Link]();
   [Link]();
   V1=X;
   V2=Y;
   [Link]();
   [Link]();
   cout<<"The scaler product of V1 & V2 = "<<V1*V2<<endl;
  float A[3]={1.1, 2.2, 3.3};
  float B[3]={4.4, 5.5, 6.6};
  Vector<float> V3(3);
  Vector<float> V4(3);
  [Link]();
  [Link]();
  V3=A;

52 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

  V4=B;
  [Link]();
  [Link]();
  cout<<"The scaler product of V3 & V4 = "<<V3*V4<<endl;
  getch();
 }

The output of this program is:


0 0 0
0 0 0

1 2 3
4 5 6
The scalar product of V1 & V2 = 32

0 0 0
0 0 0

1.1 2.2 3.3


4.4 5.5 6.6
The scalar product of V3 & V4 = 38.72

Note: a class created from a class template is called a template class. The syntax for defining
an object of template class is:
class_name <type> object_name(arg_list);

Class Template With Multiple Parameters:


To use more than one generic data type in class template. They are declared as a comma-
separated list within the template specification as shown below:

53 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

Example:  The following example shown how to decelerate more than one variable as a generic
variable (generaic data):
#include <iostream>
#include <conio.h>
using namespace std;
template <class A, class B>
class Test
{
  A N1;
  B N2;
public:
  Test(A X, B Y):N1(X), N2(Y)
  {}
  void show()
  {
    cout<<"N1 = "<<N1<<"\t";
    cout<<"N2 = "<<N2<<endl;
  }
};

void main()
{
  Test<int, float> A1(5,5.5);
  Test<int, int> A2(10,20);
  Test <float, char> A3(3.9,'C');

54 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

  cout<<"A1: ";[Link]();
  cout<<"A2: ";[Link]();
  cout<<"A3: ";[Link]();
  getch();
}

And when we execute this program we get the following result:


A1: N1 = 5 N2 = 5.5
A2: N1 = 10 N2 = 20
A3: N1 = 3.9 N2 = C

Member Function Template:


At the previous examples we use a template class with member function built inside the
class. To implement a member function template (outside the class):

Example: How to rewrite the example of the Vector but all the member functions are build
outside of the class?
#include <iostream>
#include <conio.h>
using namespace std;
template <class T>
class Vector
{
  T *V;
  int Size;

55 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

 
public:
  Vector(int);
  void operator=(T *);
  void output();
  T operator*(Vector);
};

template <class T>
Vector<T>::Vector(int S)
{
  Size=S;
  V=new T[Size]();
}

template <class T>
void Vector<T>::operator=(T *A)
{
  for(int i=0;i<Size;i++)
    *(V+i)=A[i];
}

template <class T>
void Vector<T>::output()
{
  for(int i=0;i<Size;i++)
    cout<<V[i]<<"   ";
  cout<<"\n"<<endl;
}

template <class T>
T Vector<T>::operator*(Vector A)
{
  T Sum=0;
  for(int i=0;i<Size;i++)
    Sum+=this‐>V[i]*A.V[i];
  return Sum;

56 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

void main()
{
  cout<<"Integer Vectors:"<<endl;
  int X1[3]={1,2,3};
  int X2[3]={4,5,6};
  Vector<int> V1(3);
  Vector<int> V2(3);
  cout<<"The created Vector V1, V2 are:"<<endl;
  [Link]();
  [Link]();
  V1=X1;
  V2=X2;
  cout<<"Vector V1, V2 with values are:"<<endl;
  [Link]();
  [Link]();
  cout<<"\nThe scaler product of V1 and V2 is:"<< V1*V2<<endl;
  cout<<"\nFloat Vector:"<<endl;
  float X3[3]={1.1, 2.2, 3.3};
  float X4[3]={4.4, 5.5, 6.6};
  Vector<float> V3(3);
  Vector<float> V4(3);
  cout<<"The created Vector V3, V4 are:"<<endl;
  [Link]();
  [Link]();
  V3=X3;
  V4=X4;
  cout<<"Vector V3, V4 with values are:"<<endl;
  [Link]();
  [Link]();
  cout<<"\nThe scaler product of V3 and V4 is:"<< V3*V4<<endl;
  getch();
}

This program certainly give the same result at the pervious program.

57 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

non –T template arguments:


 We can use non-template arguments with template arguments.
 The general form of non_T is:
template <class T, data_type Var1, …..>
 The following example is print an array (1D) of varying type:
Example:
#include <iostream>
#include <conio.h>
using namespace std;
template <class T, int Size>
class Aray
{
  T *V;
public:
  Aray()
  {
    V=new T[Size]();
  }
  void operator=(T *);
  void print();
};

template <class T, int Size>
void Aray<T,Size>::operator=(T *A)
{
  for(int i=0;i<Size;i++)
    *(V+i)=*(A+i);
}

template <class T, int Size>
void Aray<T,Size>::print()
{
  for(int i=0;i<Size;i++)
    cout<<V[i]<<"   ";
  cout<<"\n"<<endl;

58 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

void main()
{
  int X[10]={1,2,3,4,5,6,7,8,9,10};
  float Y[5]={5.5,6.6,7.7,8.8,9.9};
  Aray <int,10> A1;
  cout<<"A:"<<endl;
  [Link]();
  A1=X;
  [Link]();
  cout<<"\nB:"<<endl;
  Aray<float,5> A2;
  [Link]();
  A2=Y;
  [Link]();
  getch();
}

The output of this program is:


A:
0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10

B:
0 0 0 0 0
5.5 6.6 7.7 8.8 9.9

59 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

Function Templates:
We can also define function template to create family of function‘s with different arg’s
types. The general form of Template Function is:

Example: Write C++ program to sort the numbers using Bubble’s Sort in Generic form.
#include <iostream>
#include <conio.h>
using namespace std;
template <class T>
void Swap (T &X, T &Y)
{
  T Temp;
  Temp=X;
  X=Y;
  Y=Temp;
}

template <class T>
void Bubble(T *V,int S)
{
  for (int i=0;i<S‐1;i++)
    for (int j=0;j<S‐1‐i;j++)
      if(V[j+1]<V[j])
        Swap(*(V+j+1),*(V+j));
}

   

60 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

void main()
{
  cout<<"Integer Values:"<<endl;
  int X[6]={5,9,55,4,2,8};
  for(int i=0;i<6;i++)
    cout<<X[i]<<"   ";
  cout<<"\n"<<endl;
  Bubble<int>(X,6);
  for(int i=0;i<6;i++)
    cout<<X[i]<<"   ";
  cout<<"\n"<<endl;
  cout<<"\nFloating Values:"<<endl;
  float Y[6]={5.9, 54.3, 21.3, 45.6, 94.0, 10.2};
  for(int i=0;i<6;i++)
    cout<<Y[i]<<"   ";
  cout<<"\n"<<endl;
  Bubble<float>(Y,6);
  for(int i=0;i<6;i++)
    cout<<Y[i]<<"   ";
  cout<<"\n"<<endl;
  getch();
}

This program give the following result:


Integer Values:
5 9 55 4 2 8
2 4 5 8 9 55

Floating Values:
5.9 54.3 21.3 45.6 94 10.2
5.9 10.2 21.3 45.6 54.3 94

61 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

Function Template with Multiple arg’s:


We can use multiple generic data types in the function arg’s). The general form of using
multiple data types in function is:

Example: The following example shows how to decelerate multiple data type in generic form
of function:
#include <iostream>
#include <conio.h>
using namespace std;
template <class T1, class T2>
void disp(T1 A, T2 B)
{
  cout<<"The first number = "<<A<<endl;
  cout<<"The second number = "<<B<<endl;
  cout<<"\n"<<endl;
}
void main()
{
  disp<int,float>(5,6.3);
  disp<float,int>(4.2,88);
  disp<char,char>('A','B');
  getch();
}

The output of this program is:

62 
 
Object Oriented Programming Lecture 4
2nd class Asst. Lecture Omar Nowfal

The first number = 5


The second number = 6.3

The first number = 4.2


The second number = 88

The first number = A


The second number = B

63 
 

You might also like