Lab Assign 7
Lab Assign 7
Assignment No. 07
Problem Statement:
Write a C++ program to create a class template to represent generic vectors. Include
following functions: To create a vector, to modify the value of given vector, multiply vector
by a scalar value, Display vector.
Prerequisites:
Hardware Requirement: Desktop Computer / laptop computer.
Software Requirement: Operating System with GCC
Theory:
A C++ template is a powerful feature added to C++. It allows you to define the
generic classes and generic functions and thus provides support for generic
programming. Generic programming is a technique where generic types are used as
parameters in algorithms so that they can work for a variety of data types.
Templates can be represented in two ways:
1. Function templates
2. Class templates
1. Function Templates:
We can define a template for a function. For example, if we have an add() function,
we can create versions of the add function for adding the int, float or double type
values.
Generic functions use the concept of a function template. Generic functions define a
set of operations that can be applied to the various types of data.
Syntax:
// body of function.
Where
Ttype: It is a placeholder name for a data type used by the function. It is used within the
function definition. It is only a placeholder that the compiler will automatically replace this
placeholder with the actual data type.
class: A class keyword is used to specify a generic type in a template declaration.
Function Templates with Multiple Parameters
We can use more than one generic type in the template function by using the comma to
separate the list.
Syntax
template <class T1, class T2, > return_type function_name (arguments of type T1, T2
)
// body of function.
In the above syntax, we have seen that the template function can accept any number of
arguments of a different type.
2. CLASS TEMPLATE
We can define a template for a class. For example, a class template can be created for
the array class that can accept the array of various types such as int array, float array
or double array.
Class Template can also be defined similarly to the Function Template. When a class
uses the concept of Template, then the class is known as generic class.
Syntax :
template<class_type>
class class_name
{
….
Ttype is a placeholder name which will be determined when the class is instantiated. We
can define more than one generic data type using a comma-separated list. The Ttype can
be used inside the class body.
Now, we create an instance of a class
class_name<type> ob;
type: It is the type of the data that the class is operating on.
ob: It is the name of the object.
C++ Vector
A vector is a sequence container class that implements dynamic array, means size
automatically changes when appending elements. A vector stores the elements in
contiguous memory locations and allocates the memory as needed at run time.
An array follows static approach, means its size cannot be changed during run time while
vector implements dynamic array means it automatically resizes itself when appending
elements.
Syntax:
vector<string> v1;
v1.push_back("vector ");
v1.push_back("tutorial");
cout<<*itr;
return 0;