Lab 3 Templates - 2023
Lab 3 Templates - 2023
LAB 3. Templates
Theory:
Templates provide the capability to define one function or one class with a generic type that the
compiler can substitute for a concrete type.
The definition for function template begins with the keyword template followed by a list of
parameters
template< typename T> or template< class T>
A template function may have more than one parameter. In this case, place the parameters inside the
brackets separated by commas, such as <typename T1, typename T2, typename T3>
The syntax for class templates is the same as that for function templates.
The constructors and functions in the class are templates themselves. So , you have to place the
template prefix before the constructor and function header.
Exercise 1: Read the code below and determine if there is an error(s) in the code. Underline the error and
write the correct code next to it . If the code does not contain error(s) , write “no error”.
#include <iostream>
template < class T>
void print(T left, T right)
{
cout <<”Printing arguments: “<< left <<” ** “ << right;
}
int main()
{
cout << endl;
print(3, 5.8) ;
print(double(3),5.8) ;
cout<< endl ;
return 0 ;
}
1
const int size = 5;
int i[size]= { 22,33,44,55,66};
char c[size] = {‘c’, ‘d’, ‘g’, ‘p’, ‘q’};
mistery(i , size);//output : c**
d**
g**
p**
q**
Exercise 3 : Write and test a program that defines a function template that implements a binary search of a
sorted array of objects.
A search function should be passed the array a , the object key to be found, and the bounds on the array
index that define the scope of the search. If the object is found, its index in the array should be returned;
otherwise, the function should return -1 to signal that the object is not found.
Write your code below and the output obtained. Screenshots are allowed.
2
Exercise 4 : Write a generic class MyVector using a template with the following functions:
• void push(T data): This function takes one element and inserts it at the end.
• void push(T data, int index): It inserts data at the specified index. If the number of
elements is equal to the capacity, that means there is no more space to insert more elements, you need to
double the capacity. Copy the old array’ elements to the new array and delete the old one.
• T get(int index): It is used to get the element at the specified index.
• void pop(): It deletes the last element.
• int size(): It returns the size of the vector i.e, the number of elements in the vector.
• int getCapacity(): It returns the capacity of the vector.
• void print(): It is used to print array elements.
Write a default constructor to initialize an initial capacity of 1 element and allocating storage using dynamic
allocation : array = new T[1]; capacity = 1; currentElement = 0;
array is the integer pointer which stores the address of MyVector object
T* array;
MyVector<int> v;
MyVector<char> v1;
Enter few elements for each and run the class functions for both.
Write your code below and the output obtained. Screenshots are allowed.
3
4
5
6