0% found this document useful (0 votes)
16 views6 pages

Lab 3 Templates - 2023

The document discusses templates in C++. It provides examples of function and class templates, explaining that templates allow defining functions and classes with generic types that can be substituted at compile time. The exercises include identifying errors in template code, determining the output of a template function, implementing a binary search template function, and writing a MyVector template class with various member functions.

Uploaded by

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

Lab 3 Templates - 2023

The document discusses templates in C++. It provides examples of function and class templates, explaining that templates allow defining functions and classes with generic types that can be substituted at compile time. The exercises include identifying errors in template code, determining the output of a template function, implementing a binary search template function, and writing a MyVector template class with various member functions.

Uploaded by

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

Lebanese University Programming with C++

Faculty of Engineering Semester V


__________________________________________________________________________________

Name: _Ali Hassan Makki__________________ ID___6478______ Date:


12/25/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 ;
}

Exercise 2 : What is the output of the following program?


template<class T>
void mistery(const T *a, const int c)
{
for(int i=0; i< c; i++)
cout << a[i] << “ ** “ ;
cout << endl;
}
int main() {

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**

mistery(c, size-2); //output:22**


33**
44**
cout << endl;
return 0;
}

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;

capacity is the total storage of the vector;

currentElement is the number of elements currently present in the vector.


Write the main() function with two instances of MyVector class

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

You might also like