Using class template we can write a class whose members use template parameters as types.
Class
Templates
•
Using class template we can write a
class whose members use template parameters as types.
•
The syntax of class template declaration
is
template<class Type >
class classname
{
… //body of class
…
};
•
In above example Type can be of any data type. Then template class member function
is defined. The complete program using class template is as given below.
#include <iostream>
using namespace std;
template<class T >
class compare { //writing
the class as usual
Ta, b;//note we have used data type as T
public:
Compare (T first, T second)
{
a=first;
b=second;
}
T max ();//finds the maximum element among two
};
//template class member function definition
//here the member function of template class is max
template<class T>
T Compare <T> ::max ()
{
T val;
if(a>b)
val=a;
else
val=b;
return val;
}
int main()
{
Compare <int> obj1 (100, 60);//comparing two integers
Compare <char> obj2('p','t');://comparing two characters
cout<<"\n
maximum(100,60) = "<obj1.max();
cout<<"\n maximum('p','t') = "<obj2.max();
retrun 0;
}
maximum(100,60) = 100
maximum('p','t') = t
•
In above program, Compare is a class in which two variable a and b are declared
for comparison. The function max is used to find the maximum number among the
two. The T is used to indicate the data type. If we create an object of type
integer by
Compare <int> obj1(100,60)
then
two integer values will be compared. Similarly one can compare two characters,
two real values by declaring appropriate objects.
•
Function templates are those functions which can handle different data types
without separate code for each of them. For a similar operation on several
kinds of data types, a programmer need not write different functions.
Using
class template we can write a class whose members use template parameters as
types.
Example:1
Implement
circular queue with proper insertion and deletion operations using class
templates.
Solution :
#include<iostream>
using namespace std;
#define size 10
/*‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
Class Definition using class template
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒*/
template<class T>
class Queue
{
T Que[size];
int front, rear;
public:
Queue()//constructor defined
{
front = ‒1;
rear = 0;
}
void init();
void insert(T ch);
T delet();
void display();
};
template <class T>
void Queue<T>::init()
{
for (int i = 0;
i< size; i++)
Que[i] = 0;
}
/*‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
insert function
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒*/
template<class T>
void Queue<T>::insert(T item)
{
if (front = = (rear + 1) % size)
{
cout<<"Queue
is full\n";
}
else
{
//setting front
pointer for a single element in Queue
if(front = = ‒1)
front = rear= 0;
else
rear = (rear +
1)% size;
Que[rear] = item;
}
}
/*‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
delet function
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒*/
template<class T>
T Queue<T>::delet()
{
T val;
if (front = = ‒1)
{
cout <<
"Queue is empty\n";
return 0; // return
null on empty Queue
}
val = Que[front]://item to be deleted
if (front = = rear)//when single element is present
{
front = rear = ‒1;
}
else
front = (front + 1) % size;
return val;
}
/*‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
Display function
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒*/
template <class T>
void Queue<T>::display()
{
int i;
while (i!= rear)
med to indvan
{
cout << Que[i] << "
";
i = (i + 1) % size;
}
cout << Que[i] << endl;
}
Object Oriented Programming: Chapter 5: Templates and Exception Handling : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Class Templates
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation