0% found this document useful (0 votes)
13 views4 pages

Lab Assign 7

The document outlines an assignment for creating a C++ class template to represent generic vectors, including functions for vector creation, modification, scalar multiplication, and display. It explains the concept of C++ templates, differentiating between function and class templates, and provides syntax examples for both. Additionally, it discusses the characteristics of vectors compared to arrays, emphasizing the dynamic nature of vectors in memory allocation.

Uploaded by

forgeapex50
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)
13 views4 pages

Lab Assign 7

The document outlines an assignment for creating a C++ class template to represent generic vectors, including functions for vector creation, modification, scalar multiplication, and display. It explains the concept of C++ templates, differentiating between function and class templates, and provides syntax examples for both. Additionally, it discusses the characteristics of vectors compared to arrays, emphasizing the dynamic nature of vectors in memory allocation.

Uploaded by

forgeapex50
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
You are on page 1/ 4

K. K.

Wagh Institute of Engineering Education and Research, Nashik


(Autonomous from Academic Year 2022-23)
F. Y. B. Tech. (E & TC)
Pattern 2022 Semester II
2300109A : Programming in C++

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:

template < class Ttype> ret_type func_name(parameter_list)

// 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;

where class_name: It is the name of the class.

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.

Difference between vector and array:

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:

Consider a vector 'v1'. Syntax would be: vector<object_type> v1;


Example #include<iostream> #include<vector> using namespace std; int main()

vector<string> v1;

v1.push_back("vector ");

v1.push_back("tutorial");

for(vector<string>::iterator itr=v1.begin(); itr!=v1.end(); ++itr)

cout<<*itr;

return 0;

Conclusion : Hence we have understood how to use templates and vector.

You might also like