y
nl
O
se
U
tre
en
Session: 10 C
h
ec
Generics
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited.
y
nl
Explain Generics
O
se
List ways of implementing Generics
U
Explain Template Function
tre
en
Explain Template Class
C
h
List advantages and disadvantages of Generics
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 2
y
nl
O
Generics allow creating data members and functions,
whose the type is not specified at compile time.
se
U
tre
The type of the value of a generic function or variable
en
is decided later based on value supplied by user.
C
h
ec
pt
It allows the programmer to define the behavior of the
rA
class without knowing the data type.
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 3
y
nl
The figure shows an example of generics.
O
se
U
tre
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 4
y
nl
It does not have a specific type attached to the
O
parameters or even the return type.
se
The type for the parameters is decided based on the
U
value passed by the user at runtime.
tre
Syntax
en
<access-modifier> <T> <method-name>
(<T> <parameter-name>,..) C
h
{
ec
// processing statements
pt
<return-value (or expression)>
rA
}
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 5
y
nl
Template class consists of a generic type declaration.
O
se
U
The generic type introduces a type variable named ‘T’
tre
which can be used anywhere inside the class.
en
Syntax
C
<access-modifier> class <class-name> <T>
h
ec
{
<data-members>
pt
rA
<methods>
Fo
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 6
y
nl
Generics allow a programmer to create classes with
O
flexibility in applying the data type.
se
U
The data type is decided when the object is created
tre
and not when the class is created.
en
Generics allow overloading of template functions with
parameters of unspecified types. C
h
ec
The main benefit of generics is late binding of the
pt
types.
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 7
y
nl
The most common use of generics is the creation of
O
typed Collections such as List, Stack, and Array
se
U
The compiler determines the type associated with the
tre
template that can perform all the required functions
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 8
y
nl
Some compilers have poor support for templates.
O
se
Some compilers lack in clear description when they
U
detect a template error.
tre
The compiler usually generates additional code for
en
each template type.
C
h
Nested templates are not supported by all compilers.
ec
pt
The use of ‘less-than’ and ‘greater-than’ signs as
rA
delimiters causes problems.
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 9
y
nl
Generics provides a way of creating general purpose
O
tools for applying them to specific situations by making
se
relevant changes.
U
tre
Generics is also known as templates in C++.
en
A template function is a method that does not have a
C
specific type attached to the parameters or even the
h
ec
return type.
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 10
y
nl
A template class introduces a type variable 'T' which
O
can be used as a type with the data members and
se
methods of a class.
U
tre
Generics allow overloading of template functions with
en
parameters of unspecified types.
C
The language C++ uses the keyword ‘template’ to
h
ec
indicate that a class is a template class using generics.
pt
The most common use of generics is the creation of
rA
typed Collections such as List, Stack, and Array.
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 11