Acknowledgement

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 33

3

Acknowledgement

We acknowledge all the teachers and lab-


assistants who helped us in our case study on the topic
- “Genericity”. We also thank our college administration
and specially the Bsc.I.T department to provide us the
internet facility which proved to be quiet useful in
finding the relevant information for our case study.
Without the above mentioned help and support our
case study would not have been completed in due time
and in a proper way.

Thanking you,

Neha
Chavan [SYBscIT-18]
& Nishant
Nair [SYBscIT-84]

3
Viva College of Arts, Commerce
and Science.

This is to certify that the work entered in


this document is the work of kumari-Neha Chavan
(Roll no.18) & kumar- Nishant Nair(Roll no.84) of
S.Y.Bsc.I.T and they have successfully completed their
case study on the topic ‘Genericity’ in due time with all
the relevant data.

3
Introduction

Generic programming is a style of computer programming in


which algorithms are written in terms of to-be-specified-later types
that are then instantiated when needed for specific types provided as
parameters. This approach permits writing common functions or types
that differ only in the set of types on which they operate when used,
thus reducing duplication. Software entities created using generic
programming are known as generics

Generic programming refers to features of certain statically typed


programming languages that allow some code to effectively
circumvent the static typing requirements. For instance in C++, a
template is a routine in which some parameters are qualified by a type
variable. Since code generation in C++ depends on concrete types, the
template is specialized for each combination of argument types that
occur in practice. Generic programming is commonly used to
implement containers such as lists and hash tables and functions such
as a particular sorting algorithm for objects specified in terms more
general than a concrete type

The term "generic" has also been used differently in programming


contexts. For example, in Forth the compiler can execute code while
compiling and one can create new compiler keywords and new
implementations for those words on the fly. It has few words that
expose the compiler behaviour and therefore naturally offers generic
programming capacities which, however, are not referred to as such in
most Forth texts. The term has been used in functional programming,
specifically in Haskell-like languages, which use a structural type
system where types are always parametric and the actual code on
those types is generic. In these uses generic programming still serves
the similar purpose of code-saving and the rendering of an abstraction.

3
Generic programming in object-oriented
languages
When creating containers of objects it is possible to write specific
implementations for each datatype contained, even if the code is
virtually identical except for different datatypes. In C++, this
duplication of code can be circumvented by defining a template class:

template<typename T>
class List
{
/* class contents */
};

List<Animal> list_of_animals;
List<Car> list_of_cars;
Above, T is a placeholder for whatever type is specified when the list
is created. These "containers-of-type-T", commonly called generics,
are a generic programming technique allowing a class to be reused
with different datatypes as long as certain contracts such as subtypes
and signature are kept

Generics can also be used for type-independent functions as in the


Swap example below:

template<typename T>
void Swap(T & a, T & b) //"&" passes parameters
by reference
{
T temp = b;
b = a;
a = temp;
}

string hello = "world!", world = "Hello, ";


Swap( world, hello );

3
cout << hello << world << endl; //Output is
"Hello, world!"

template
Templates are a feature of the C++ programming language that
allow functions and classes to be operate with generic types. This
allows a function or class to work on many different data types
without being rewritten for each one.

Templates are of great utility to programmers in C++, especially


when combined with multiple inheritance and operator overloading.
The C++ Standard Library provides many useful functions within a
framework of connected templates.

Templates are generally considered an improvement over macros


for these purposes. Templates are type-safe. Templates avoid some of
the common errors found in code that makes heavy use of function-
like macros. Perhaps most importantly, templates were designed to be
applicable to much larger problems than macro.

Templates are classified into two types:

 Function Template
 Function and Function Template

 Bubble Sort Function Template

 Overloaded Function Template

 Multiple Argument Function Template

 User Defined Template Argument

 Class Template
3
 Template Argument

 Class Template

 Inheritance of Class Template

 Class Template with Overloaded Operators

Function template

There are several functions of considerable importance which have


to be used frequently with different data types. The limitation of such
functions is that they operate only on a particular data type. It can be
overcome by defining that function as a function templates or generic
function. A function template specifies how an individual function can
be constructed.

The c++ template features enables substitutions of a single piece


of code for all these overloaded functions with a single template
function as follows

template<class T>
void swap ( T & x , T & y) // by reference
{
Tt; //template type
temporary variable used in swapping
t = x;
x = y;
y = t;
}

3
Such functions are known as function templates. When swap
operation is requested on operands of any data type, the compiler
creates a function internally without the user intervention and invokes
the same

 Syntax of Function Template


A function template is prefixed with the keyword template and
a list of template type arguments. These template-type arguments are
called generic data types, since their exact representation (memory
requirement and data representation) is not known in the declaration
of the function template. It is known only at the point of a call to a
function template. The syntax of declaring the function template is
shown in figure below

template
keyword data type
atleast one argument
must be template type

template < class T, ……>


ReturnType FuncName ( arguments )
{
……// body of template function
……
}

Syntax of Function Template

The syntax of a function template is similar to normal function


except that it uses variables whose data types are not know until a call
to it is made. A call to a template function is similar to that of a

3
normal function and the parameters can be of any data-type. When the
compiler encounters a call to such functions, it identifies the data type
of the parameter and creates a function internally and makes a call to
it. The internally created function is unknown to the user. The
program gswap.cpp makes use of templates and avoids the
overhead of rewriting functions having body of the same pattern, but
operating on different data types.

// gswap.cpp : generic function for swapping


#include < iostream.h>
template<class T>
void swap ( T & x , T & y) // by reference
{
Tt; //template type
temporary variable used in swapping
t = x;
x = y;
y = t;
}
void main( )
{
char ch1, ch2;
cout << “ Enter two character <ch1, ch2> :
“;
cin >> ch1 >> ch2 ;
swap(ch1,ch2); //compiler creates and calls
swap (char &x, char &y) ;
cout << “ On Swapping <ch1, ch2>: “ << ch1
<< “ “ << ch2 << end1;
int a, b;
cout << “ Enter two integer <a, b > : “;

3
cin >> a >> b;
swap( a, b) ; // compiler creates and calls
swap ( int &x, int&y );
cout << “ On Swapping <a, b>: “ << a <<
“ “ << b << end1;
float c, d;
cout << “ Enter two floats <c, d> : “ ;
cin >> c >> d;
swap( c, d ); // compiler creates and calls swap
( float &x, float &y );
cout << “ On Swapping <c, d>: “ << c <<
“ “ << d;
}

RUN
Enter two character <ch1, ch2 >: R K
On Swapping <ch1, ch2>: K R
Enter two integer <a, b > : 10 5
On Swapping <a, b>: 5 10
Enter two floats <c, d> : 20.5 99.5
On Swapping <c, d>: 99.5 20.5

Function and Function template

Function templates are not suitable for handling all data type, and
hence, it is necessary to override function template by using normal
functions for specific data types. When a statement such as

3
max (str1, str2)
is executed, it will not produce the desired result. The above call
compares memory addresses of strings instead of their contents. The
logic for comparing strings is different from comparing integer or
floating-point data type. It requires the function having the definition :

char * max (char*a, char*b)


{
return (strcmp( a, b) >
0 ? a : b );
}

If the program has both the function and function template with the
same name, first, the compiler selects the normal function, if it
matches with the requested data type, otherwise, it creates a function
using a function template. This is illustrated in the program
max2.cpp.maximum of standard and derived data type items

#include<iostream.h>
#include<string.h>
template<class T>
T max(Ta, Tb)
{
If(a>b)
return a;
else
return b;
}
//specifically for string data types
char*max(char*a,char*r)

3
{
if(strcmp(a,b)>0)
return a;
else
return b;
}
void main()
{
//max with character data types
char ch,ch1,ch2;
cout<<”Enter two character <ch1,ch2>:”;
cin>>ch1>>ch2;
ch=max(ch1,ch2);
cout<<”max(ch1,ch2):”<<ch<<end1;
// max with integer data types
int a, b, c;
cout << “ Enter two integer <a, b>: “;
cin >> a >> b;

c = max( a, b);
cout << “max (a, b): “ << c << end1;
// max with string data types
char str1[20], str2[20];
cout << “ Enter two string <str1, str2>: “;
cin >> str1>> str2;
cout << “max (str1, str2): “ << max (str1,
str2);
}
RUN

Enter two character <ch1, ch2>: A Z

3
max(ch1,ch2) : Z
Enter two integer <a, b>:
max( a, b): 6
Enter two string <str1, str2>: NEHA NISHANT
max (str1, str2): NEHA

Bubble Sort Function Template

Sorting is the most commonly used operating particularly in


data processing applications. These applications require function to
sort data elements of different data types. Such functions can be
declared as function template and can be used to sort data items of any
type. The program bsort.cpp illustrates the declaration of function
template for bubble sort and its use on integer and floating point data
types.

// bsort.cpp : template function for bubble- sort


#include <iostream.h>
enum Boolean { false, true };
template <class T>
void swap ( T & x, T & y ) // by reference
{
Tt; //template type temporary
variable used in swapping
t = x;
x = y;
y = t;
}
template <class T>
void BubbleSort ( T & SortData, int Size )
3
{
boolean swapped = true ;
for ( int i = 0; ( i<size-1)&&swaped; i++)
{
swapped = false ;
for (int j = 0; ( j<size-1) && swaped ; j+
+)
if ( SortData [j] > SortData [ j + 1 ] )
{
swapped = true ;
swap ( SortData [j] > SortData
[j+1]);
}
}
}
void main ( void )
{
int IntNums[25]
float FloatNums[25];
int I , size;
cout << “ Program to sort elements…. “ <<
end1 ;
//Integer numbers sorting
cout <<” Enter the size of the integer
vector < max – 25 > : “ ;
for ( )
cin >> size ;
cout<<”enter the elements of the integer
vector ….”<<end1;
for(i=0; i< size; i++)

3
cin>>IntNums[i]<<” “;
Bubblesort( IntNums,size );
cout<<”Sorted Vector”<<end1;
for(i=0; i< size; i++)
cout<<IntNums[i]” “;
// Floating point numbers sorted
cout<<”\nEnter the elements of the float
vector…”<max-25>
for( i=0 ; i < size; i++)
cin > > size;
cout<<”Enter the elements of float vector
…”<<end1;
for( i =0 ; i< size ; i++)
cin >>floatnums [i];
Bubblesort ( FloatNums, size );
cout << “ Sorted Vector : “ << end1;
for ( i = 0 ; i < size ; i++)
cout << FloatNums [i] << “ “ ;
}

RUN
Program to sort elements….
Enter the size of the integer vector < max – 25 > :
enter the elements of the integer vector …
8
4
1
6
Sorted Vector :
1 4 6 8
Enter the elements of float vector <max – 25 > : 3
Enter the elements of float vector
3
8.5
3.2
8.9
Sorted vector :
3.2 8.5 8.9
Overload Function Templates

The functions templates can also be overloaded with multiple


declarations. It may be overloaded either by (other) template functions
of the same name. Similar to overloading of normal functions,
overloaded functions must differ either in terms of normal of
parameters or their type. The program tprint.cpp illustrates the
overloading of function templates:

//tprint.cpp: overloaded template functions


#include<iostream.h>
template<class T>
void print ( T data ) // single template
argument
{
cout << data << end1;
}
template<class T>
void print( T data, int nTimes ) //template and
standard argument
{
for ( int i = 0; i < nTimes; i++ )
cout << data << end1;
}
void main ( )

3
{
print( 1 );
print( 1.5 );
print( 520,2 );
print( “OOP is Great”,3 );
}
RUN
1
1.5
520
520
Oop is great
Oop is great
Oop is great

Multiple Arguments Function


Template

So far, all the function templates dealt with a single generic


argument. Declaration of a function template for functions having
multiple parameters of different types requires multiple generic
arguments. The program multiple.cpp illustrates the need for multiple
template arguments.

//multiple.cpp use of multiple temple arguments


struct A
{
int x;
int y;
};
Struct B

3
{
int x;
double y;
};
template < class T >
void Assign_A ( T a, T b, A & S1 )
{
S1.x = a;
S1.y = b;
}
template < class T >
void Assign_B( T a, T b, A & S2 )
{
S2.x = a;
S2.y = b;
}
void main( void )
{
A S1;
B S2;
void Assign_A( 3, 4, S1);
void Assign_B( 3, 3.1415, S2); //Error : no
match for void Assign_B
( int, double, B)
}

User Defined Template Arguments


In addition to primitive data type, user defined types can be passed
to function templates. Its declaration is same as the function template

3
processing standard data types as illustrated in the program
student.cpp.
// student.cpp : student record and template
with user defined data types
#include <iostream.h>
struct stuRec
{
char name[30];
int age;
char collegeCode;
};
template <class T >
void Display ( T & t )
{

cout << t << end1;


}

ostream & operator << ( ostream & out , stuRec


& s)
{
out << “ Name : “ << s.name << end1;
out << “ Age : “ << s.age << end1;
out << “ College Code : “ << s.collegecode
<< end1;
return out;
}

void main ( void )


3
{
stuRec s1;
cout << “Enter the student record details
…” << end1;
cout << “ Name : “; cin >> s.name;
cout << “ Age : “; cin >> s.age;
cout << “College Code : “; cin >> s.
collegecode;
cout << “The student record : ” << end1;
cout << “ Name : “; Display ( s1.name );
cout << “ Age : “; Display ( s1.age );
cout << “College Code : “;
Display ( s1. Collegecode ) ; //it in turn
calls operator <<
defined above
cout << “The student record : ” << end1;
Display ( s1 );

RUN
Enter the student record details …
Name : NEHA
Age : 19
College Code : A
The student record :
Name : NEHA
3
Age : 19
College Code : A

class Templates

Similar to function, classes can also be declared to operate on


different data types. Such classes are called class templates. A class
template specifies how individual classes can be constructed similar to
normal class specification. These classes model a generic class which
supports similar operations for different data types. A generic stack
class can be created, which can be used for sorting data of type
integer, real, double, etc. Consider an example of a stack (modeling
last-in-first-out data structure) to illustrate the need and benefits of
class templates. The class declaration for stacks of type character,
integer

The syntax of declaring class template and defining objects using the
same is shown in figure. The definition of class template implies
defining template data and member functions.

keyword template data types T1,


T2,….

template < class T1, class T2, …………>


class ClassName

3
{
// data items of template type T1, T2,
……
T1 data1;
………
// functions of template arguments T1,
T2,……….
Void func1 ( T1 a, T2 &b);
…………
T func1 ( T1 *x, T2 *y);
};

The prefix template < class T > specifies that a template is being
declared, and that a type-name T will be used in declaration. In
other words, DataStack is a parameterized class with T as its
generic data type.

Any call to the template functions and classes, needs to be


associated with a data type or a class. The compiler then instantiates a
copy of template function or template class for the data type specified.
The syntax for creating objects using the class template is shown
below:

A statement to create an object of type stack that can hold integers is


as follows:

3
DataStack < int > stack_int; //stack of
integers datatype to be
substituted for template
datatype

ClassName <char> object1;


ClassName <int> object2;
………………………………….
ClassName <some_other_class> object5;
Syntax for class template
instantiation

Similarly, objects those hold characters, floats and doubles can be


created by the following statements:

DataStack <char >


stack_char; //stack of characters
DataStack < float >
stack_float; //stack of floats
DataStack < double >
stack_double; //stack of doubles

However, the usage of these stack objects is similar to those of normal


objects. For instance, to push the integer value 10 into the stack_int
object, the following statement is used:

stack_int.push (10);

3
Template Argument
A template can have character strings, function names, and
constant expressions in addition to template type arguments. Consider
the following class template to illustrate, how the compiler handles
the creation of objects using class template

template < class T1, int size>


class myclass
{
T arr[size];
};
The value supplied for a non template type argument must be constant
expressions; its value must be known at compile time. When the
objects of the class template are created using a statement such as

myclass <float, 10> new1;


The compiler creates the following class:

class myclass
{
Float arr [10] ;
};
Again is a statement such as,
myclass <int, 5> new2;
is encountered for creating the object new2, the compiler creates the
following class:

3
class myclass
{
int arr [5];
};

Class template with Multiple


Arguments
The template class is instantiated by specifying predefined data type
or user defined data type classes. The data type is specified in angular
braces< >. The syntax for instantiating class template is as follows:

TemplateClassName < type > instance;


TemplateClassName < type1, type2 >
instance(argument);
The instantiation specifies the object of specified data type. If a
different data type is to be specified, a new declaration statement must
be used.

The declaration of template classes with multiple arguments is


similar to the function template with multiple arguments. However,
the arguments need not be of template type. These may include
character strings, addresses of objects and functions with external
linkage, static class members and constant expressions. Consider the
following declaration:

template <class T, unsigned SIZE >


class stackN
{
protected:
T array [SIZE];
unsigned int top;

3
public:
Stack20 ( ) { top = 0; }
void Push ( const T & elem ) { Array
[ top++ ] = elem }
T Pop ( void ) { return Array [ --top ]; }
int GetSize ( void ) const { return top
+1}
T & GetTop (void ) {return Array [top];}
};
The declaration of the class template StackN is preceded by,
template < class T, unsigned SIZE >

As before, except that it has two arguments. The second argument is


an (typed) unsigned argument. Making SIZE an argument of the
template class StackN rather than to its objects, infers that sizes of
class StackN is known at compile time so that class StackN can be
fully declared at compile time. The class template StackN with a
variable stack size can be instantiated by specifying the size in the
argument list. This makes a template, such as StackN, useful for
implementing general purpose data structure. The above declaration
provides the user freedom to define many instances of class StackN,
each operating on different data type and of variable size. The
following statements define objects of the class template StackN for
storing integers and characters respectively.

StackN <int, 20> intstk;


StackN <char, 50> chrstk;
A known type argument in template class ( second argument in the
above case) must be a constant expression (evaluated at the compile
time) of the appropriate type.

3
Inheritance of class template

A combination of template and inheritance can be used in


development hierarchal data structures such as container class. A base
class in a hierarchy represents a commonality of methods and
properties. Use of template with respect to inheritance involves the
following:

 Derive a class template from a base class, which is a template


class.

 Derive a class template from a base class, which is a template


class, add more template member in the derived class.
 Derive a class from a base class which is not a template and add
template members to that class.
 Derive a class from a base class which is a template class and
restrict the template feature, so that the derived class and its
derivatives do not have the template feature.
The template features provided in the base classes can be restricted
by specifying the type, when the class is derived. All the argument
lists of the base class have to be replaced by predefined types. In such
a case, the derived class does not inherit the template feature, but is
just a class of specified data type stated at the point of inheritance
declaration. The syntax for declaring derived classes from template
based base classes is shown in figure

template < class T1, ……….>


class BaseClass
{

3
//template type data and functions
data type for template in
};
base class
template < class T1,………>
Class DerivedClass : public BaseClass < T1,
……..>
{
//template type data type and functions
};
Syntax for inheriting template base class

The class deriving a template type base class can be a normal class
or a class-template. If a new derived class is a normal class, the data
type of template argument to the base class must be specified at the
point of derivation. Otherwise, template arguments type specified at
the point of instantiation of a class template can also be passed.
Consider an example of declaring the template class Vector . It
inherits all the properties from the base template class sVector .
The derived template class Vector is still a static vector containing
twenty elements. Member functions that perform insert, delete and
search are added to the derived class. The member function have the
prefix <template class T >, since the derived class operates
on the undeclared type T. The specification of a new template class
created by inheriting another template-based base class is given
below:
<template class T >
Class Vector : public sVector < T >
{
…….
read( );
…….
};

3
The member functions defined with its class body have the same
syntax as member of non-template-type classes. However , member
function defines outside the body of a class, for instance, has the
following specification:
<template class T >
void Vector < T > : : read ( )
{
……// body of the read ( )
}
Note that, the member functions of a class-template are treated as
function-template type members. The class Vector can be
instantiated as follows:
Vector < int > v1;
In case, the int specified in angular brackets in first assigned to
generic data type in the Vector class and then the same is also passed
to its base class.
A derived class of a template based base class is not
necessarily template derived class. That is, the non-
template-based derived classes can also be created from the template
based base classes. In this case, the undefined template argument T
has to be specified during derivation, for instance, as follows:
class Vector : public sVector < int >
{
……..
};
It creates a new class called Vector from the template-based base
class sVector . The int is passed as the template argument type to
the base class.

class template with overloaded operators

3
The class template can also be declared for a class having
operator overloaded member functions. The syntax for declaring
operator overloaded functions is the same as class template members
and overloaded functions. The class template with operator
overloading will allow to truly extend the language and at the same
time retaining the readability of object manipulating code. The
program complex.cpp illustrates the overloading of the +operator in
the class template complex. In this case, the members of the complex
number ( real and imaginary ) can be any of the standard data types.

//complex.cpp : template class for operator


overloaded complex class
# include<iostream>
template<class T>
class complex
{
private:
T real; //real part of
complex number
T imag; //imaginary part of
complex number
public:
complex ( ) //no argument
constructor
{
real = imag=0.0;
}
Void getdata( ) // read complex
number
{
cout << “Real Part ? “ ;

3
cin >> real ;
cout << “Imag Part ? “ ;
cin >> imag ;
}
complex operator + ( complex
c2 ); // complex addition
void outdata ( char * msg ) //
display complex number
{
cout << msg << “ ( “ << real ;
cout << “ , “ << imag << “ ) “ <<
end1;
}
};
template<class T>
complex <T> complex< T > : : operator +
( complex < T > c2 );
{
complex < T > temp; // object
temp of complex class

temp.real = real + c2.real ; // add real


parts
temp.imag = imag + c2.imag; // add
imaginary parts
return (temp); // return
complex object
}
void main ( )
{

3
complex < int > c1, c2, c3; // integer
complex objects
cout << “Addition of integer complex
objects…..” << end1;
cout << “Enter complex number c1 ….”
<< end1;
c1.getdata( );
cout << “Enter complex number c2 ….”
<< end1;
c2.getdata( );
c3 = c1 +c2; //integer
addition
c3.outdata ( “c3 = c1 +c2: “ ) ; // display
result
complex < float > c4, c5, c6; // integer
complex objects
cout << “Addition of float complex
objects…..” << end1;
cout << “Enter complex number c4 ….”
<< end1;
c4.getdata( );
cout << “Enter complex number c5 ….”
<< end1;
c5.getdata( );
c6 = c4 +c5 ; // floating
addition
c6.outdata ( “c6 = c4 +c5: “ ) ; // display
result
}
RUN

3
Addition of integer complex objects…..
Enter complex number c1 …
Real Part ? 1
Imag Part ? 2
Enter complex number c2 …
Real Part ? 3
Imag Part ? 4
c3 = c1 +c2: ( 4, 6)
Addition of float complex objects…
Enter complex number c4 …
Real Part ? 1.5
Imag Part ? 2.5
Enter complex number c5 …
Real Part ? 2.4
Imag Part ? 3.7
c6 = c4 +c5: ( 3.9, 6.2 )

You might also like