Function Classes and Objects - PowerPoint PPT Presentation

About This Presentation
Title:

Function Classes and Objects

Description:

String operator() (const String& S) const; void set(char c) { v = c; } private: char v; String wildcard::operator() (const String& S) const { String R(S) ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 18
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Function Classes and Objects


1
Function Classes and Objects
  • Andy Wang
  • Data Structures, Algorithms, and Generic
    Programming

2
Function Objects
  • Overload operator()
  • Can take any number of parameters
  • Can return any value
  • Configurable functions with their own data
    members
  • class makeuppercase
  • public
  • void operator() (char c) const
  • c toupper(c)

3
Function Objects (2)
  • Declare a function object like any other class
  • Evaluate a function object by applying the
    operator() after the object name
  • class makeuppercase // forward declaration
  • makeuppercase muc // a function object
  • char ch
  • cin gtgt ch
  • muc(ch)

4
Two roles of operator()
  • Overloading an operator by name
  • Obtaining input parameters of the operator
    function

5
Another Example of a Function Class
  • class wildcard
  • public
  • wildcard() v(?)
  • explicit wildcard(char c) v(c)
  • String operator() (const String S) const
  • void set(char c) v c
  • private
  • char v
  • String wildcardoperator() (const String S)
    const
  • String R(S)
  • for (unsigned int i 0 i lt S.Size() )
  • if (Si v) Ri ?
  • Return R

6
Another Example of a Function Class (2)
  • include ltiostream.hgt
  • include ltxstring.hgt
  • int main()
  • String S(character)
  • wildcard k_to_?(k)
  • wildcard i_to_?(i)
  • cout ltlt k_to_?(S) ltlt endl // ?hara?ter
  • cout ltlt i_to_?(S) ltlt endl // ch?r?cter

7
Advantages of Function Objects
  • Abstraction
  • Template parameters
  • Versatility
  • Dynamically create functions with different
    behaviors
  • Performance
  • More information available to compilers to
    generate faster code

8
More on Function Objects
  • Most common use in STL Predicate Classes
  • Function classes that return a Boolean value
  • Examples LessThanltTgt, GreaterThanltTgt
  • Other examples
  • Hash objects
  • Wrapper for any function to be applied by generic
    algorithms (e.g., makeuppercase)
  • IO manipulators
  • Dynamically configurable functions

9
Predicate Class TLessThanltTgt
  • template lttypename Tgt
  • class TLessThan
  • public
  • int operator() (const T t1, const T t2)
    const
  • template lttypename Tgt
  • int TLessThanltTgtoperator() (const T t1, const
    T t2) const
  • return (t1 lt t2)

10
Predicate Class TGreaterThanltTgt
  • template lttypename Tgt
  • class TGreaterThan
  • public
  • int operator() (const T t1, const T t2)
    const
  • template lttypename Tgt
  • int TGreaterThanltTgtoperator() (const T t1,
    const T t2) const
  • return (t1 gt t2)

11
Function Class THashClassltTgt
  • template lttypename Tgt
  • class THashClass
  • public
  • unsigned int operator() (const T) const
  • template lttypename Tgt
  • unsigned int THashClassltTgtoperator() (const T
    t) const
  • return hash_function(t)

12
Simple I/O Manipulators
  • Use
  • unsigned long y
  • cout ltlt hex ltlt y // output y in hex notation
  • Implementation
  • ios operatorltlt(ios s, ios F(ios))
  • F(s)
  • return(s)
  • ios hex(ios s)
  • s.unsetf(iosoct iosdec)
  • s.setf(ioshex)
  • return s

13
I/O Manipulators with Parameters
  • Use
  • cout ltlt beep(10) // output 10 beeps
  • Implementation (non-generic)
  • class beep
  • private
  • int numbeeps
  • public
  • beep(int n) numbeeps(n)
  • ostream operator() (ostream os) const
  • for (int i 0 i lt numbeeps i)
  • os ltlt \a
  • return os

14
I/O Manipulators with Parameters (2)
  • ostream os operatorltlt(ostream os, beep bp)
  • bp(os)
  • return os

15
I/O Manipulators with Parameters Using Templates
  • Generic one-parameter manipulator class
  • templateltclass S, typename Tgt
  • class _STFO
  • public
  • _STFO(S (function) (S, T), T Tval)
    F(function), t(Tval)
  • S operator() (S s) const
  • return (F) (s, t)
  • private
  • S (F) (S, T)
  • T t

16
I/O Manipulators with Parameters Using Templates
(2)
  • Generic overload of left shift operator
  • templateltclass S, typename Tgt
  • S operatorltlt(S s, const _STFOltS, Tgt fo)
  • return fo(s)
  • Instantiation for beep(n)
  • ostream _beep(ostream s, int n)
  • for (int i 0 i lt n i)
  • s ltlt \a
  • return s
  • _STFOltostream, intgt beep(int n)
  • return _STFOltostream, intgt ((ostream ()
    (ostream, int)) _beep, n)

17
Generic Overload of Left Shift Operator
  • templateltclass S, typename Tgt
  • S operatorltlt(S s, const _STFOltS, Tgt fo)
  • return fo(s)
Write a Comment
User Comments (0)
About PowerShow.com