Function and Class - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Function and Class

Description:

Note that each member function definition outside the class begins with the header ... makes function f1() a friend of every template class instantiated from the ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 24
Provided by: ahmadrh
Category:

less

Transcript and Presenter's Notes

Title: Function and Class


1
Function and Class Templates
2
Introduction - Template enables us to specify
with a single code segment, an entire range of
related functions called template functions or an
entire range of related classes called template
classes - For example, we can write a single
function template for an array that - sorts an
array of integers - sorts an array of doubles -
or even an array of a complex type such as
employee class
3
Function Template - Suppose we want to write a
function that prints the contents of an
array. - If this array is an array of integers,
a possible implementation can be as
follows void printArray (int array, int
arraySize) for (int i0
iltarraySize i) cout ltlt arrayi ltlt
- where array is the name of the array
and arraySize refers to the size of the array.
4
- Note that the prototype of this function can
be written as void printArray(int,
int) - Can we call the above function to print
other types of arrays such as an array of double,
or an an array of char, or an array of class
employee, and so on? - The answer is that with
Traditional C language, this is not possible.
However, with object-oriented tools it is
possible.
5
- In traditional C, we need to take care of two
issues - First, we need to have a separate
print function, one for each type of the array we
use in our program - Second, if all of these
arrays are under one project (program) their
names must be different. - we can not have two
function of the same name in Traditional C
language - For example, if we have three
arrays of types int, char, double, we may need to
have three functions such as void
printIntArray(int, int) void
printDoubleArray(double, int) void
printCharArray(char, int)
6
Overloading the functions - Remember the concept
of overloading in C. - In C, we can overload
the functions. This means, we can have several
functions with the same name but different
signature (their return type and/or parameters
have different types, or number of parameters can
be different). - If we overload our printArray
function we can have three functions one for each
different type. void printArray(int,
int) void printArray(double, int) void
printArray(char, int)
7
Using function Template - With overloading, we
do not need to give each function a distinct
name however, we still need to have one function
for each type of the array. - If we use a
template function, to print the array, we only
need to write one function to take care of any
form of array. - Based on the argument types
provided explicitly or inferred from calls to the
template function, the compiler generates
separate object-code functions to handle each
type of call appropriately.
8
Syntax - for our printArray function, the syntax
is as follows templateltclass Tgt void
printArray (T array, int arraySize)
for (int i0 iltarraySize i) cout ltlt
arrayi ltlt - All function template
definitions begin with the keyword template
followed by a list of formal type parameters to
the function template enclosed in angle bracket
(lt gt) - Each formal type parameter must be
proceeded by the keyword class or a typename
ltSee Example 1gt
9
  • Class Template
  • Class template are called parameterized types
    because they require one or more type parameter
    to specify how to customize a generic class
    template to form a specific template class
  • Before we get into the discussion of template
    class, we need to explain the concept of a data
    structure called stack
  • Stack can be of any data type. We can have a
    stack of integers, a stack of doubles, a stack of
    Employee class or stack of Student class.
  • The following is a typical declaration of a stack
    class including the implementation of the methods
  • ltSee Example 2gt

10
class Stack public Stack() bool push(const
int) bool pop(int) private int size int
top int stack3
bool Stackpop(int popValue) if
(topgt0) popValue stacktop top-- re
turn true return false
bool Stackpush(const int pushValue) if
(top1 lt size) top stacktop
pushValue return true return false
StackStack() size 3 top -1
11
  • What is the limitations of our Stack class?
  • One of the limitation of our stack class is that
    all objects of the type Stack class must be
    integer stack.
  • What about objects of type stack of doubles or
    stack of chars, or even more complex classes such
    as stack of Employees or stack of Students?
  • One solution is to create one stack class for
    each desirable type. For example, one for stack
    of char, another for stack of int, another for
    stack of double and so on.
  • - This is not a feasible solution because we need
    to create a new class every time we need an
    object stack of a new type.

12
  • Using template class
  • Another solution is to use template class.
  • If we make the stack to be a class template, our
    stack class become the basis for creating many
    stacks classes such as stacks of int, stacks of
    double, and so on.
  • The next slide shows how to modify our Stack
    class to become a class template for any type of
    Stack class.

13
templateltclass Tgt class Stack public Stack()
bool push(const T) bool pop(T) private int
size int top T stack3
template ltclass Tgt bool StackltTgtpop(T
popValue) if (topgt0) popValue
stacktop top-- return
true return false
template ltclass Tgt bool StackltTgtpush(const T
pushValue) if (top1 lt size) top
stacktop pushValue return
true return false
template ltclass Tgt StackltTgtStack() size
3 top -1
14
  • In general to produce a variety of template
    classes, we simply need to write one class
    template definition.
  • Each time we need a new type specific
    instantiation, we can declare the associated
    object and the compiler writes the source-code
    for the template class we require.
  • In the main program, we now can use the same
    class to create two different types of stacks
  • void main()
  • Stackltdoublegt doubleStack
  • Stackltintgt intStack
  • .
  • ltSee Example 3gt

15
- Note that each member function definition
outside the class begins with the header
template ltclass Tgt - The binary scope
resolution operator is used with the class
template name StackltTgt to tie the member function
definition to the class template's scope
16
- One of the limitations of the previous example
is that all stacks have one size, size 10 - We
can slightly change our implementation to be able
to instantiate stacks of different sizes with
different types - In the previous example, the
stack class template section used only one type
parameter in the template header - It is also
possible to use non-type parameters. - For
example, the template header in the previous
example can be modified to take a non-type
parameter as follows template ltclass T, int
elementsgt
17
- Then a declaration such as Stack ltdouble,
100gt myStack would instantiate a stack of
double with the capacity of 100 elements. - the
complete class header and the implementations of
the methods are shown in the next slide ltSee
Example 4gt
18
templateltclass T, int elementsgt class
Stack public Stack() bool push(const
T) bool pop(T) private int size int
top T stackelements
template ltclass T, int elementsgt bool
StackltT,elementsgtpop(T popValue) if
(topgt0) popValue stacktop top-- re
turn true return false
template ltclass T, int elementsgt bool
StackltT,elementsgtpush(const T pushValue) if
(top1 lt size) top stacktop
pushValue return true return false
template ltclass T, int elementsgt StackltT,
elementsgtStack() size elements top
-1
19
Class template and Template Classes - Template
classes of a class template are the set of
classes generated at run time for that class
template - For example, in our example, for the
class template stack, we may create three
different objects like Stack ltdouble,10gt
doubleStack Stack ltint,20gt intStack Stack
ltchar,30gt charStack - Instantiation of objects
doubleStack, charStack, and intStack causes the
following classes be generated
20
class Stack public Stack() bool push(const
int) bool pop(int ) private int
size int top int stack20
class Stack public Stack() bool push(const
char) bool pop(char ) private int
size int top char stack30
class Stack public Stack() bool push(const
double) bool pop(double ) private int
size int top double stack10
21
Templates and Inheritance - A class template
can be derived from a template class - A class
template can be derived from a non-template
class - A template class can be derived from a
class template - A non-template class can be
derived from a class template
22
Template and friends function - Inside a class
template for class X that has been declared with
template ltclass T gt class X - a friend
declaration of the form friend void f1()
makes function f1() a friend of every template
class instantiated from the preceding class
template - a friend declaration of the
form friend void Af1() makes method f1()
of class A a friend of every template class
instantiated from the preceding class template -
a friend declaration of the form friend class
Y makes every method of class Y a friend of
every template class instantiated from the
preceding class template
23
Template and friends function - Inside a class
template for class X that has been declared with
template ltclass T gt class X - a friend
declaration of the form friend void
f2(XltTgt) for a particular type T such as
float makes function f2(Xltfloatgt) a friend of
Xltfloatgt only - Or a friendship declaration of
the form friend class ZltTgt when a template
class is instantiated with a particular type for
T such as float all members of class Zltfloatgt
become friends of template class Xltfloatgt.
Write a Comment
User Comments (0)
About PowerShow.com