Function Overloading - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Function Overloading

Description:

Of different sets of parameters (at least as far as their types are concerned) ... Must be friend of the class. So it is in class definition. class Complex ... – PowerPoint PPT presentation

Number of Views:2042
Avg rating:3.0/5.0
Slides: 19
Provided by: hon799
Category:

less

Transcript and Presenter's Notes

Title: Function Overloading


1
Function Overloading
  • Can enables several function
  • Of same name
  • Of different sets of parameters (at least as far
    as their types are concerned)
  • Used to create several functions of the same name
    that perform similar tasks but on different data
    types

2
Square function
  • int square(int x)
  • return xx
  • double square(double y)
  • return yy
  • float square(float z)
  • return zz

3
Use overloaded function
  • Complier will search for the match function
    prototype
  • int main()
  • int x7
  • double y7.5
  • coutltltsquare of ltltxltlt is
  • ltltsquare(x)ltltendl
  • coutltltSquare of ltltyltltis
  • ltltsquare(y)ltltendl

4
Friend function and friend class
  • A friend function of a class is defined outside
    that class scope
  • Has right to access private members of the class
  • Using friend functions can enhance performance
  • Often appropriate when a member function cannot
    be used for certain operations

5
Declare a function as friend
  • To declare a function as a friend of a class,
    precede the function prototype in class
    definition with key word friend
  • To declare a class Two as a friend of One, place
  • friend class Two
  • in the definition of class One

6
Friendship
  • Friendship is granted, not taken
  • Friendship is not symmetric
  • If B is friend of A, I.e. A declared B as friend,
    you cannot infer A is friend of B unless B
    declare A as friend
  • Friendship is not transitive
  • A is friend of B, and B is friend of C, you
    cannot say that A is friend of C

7
Example
  • class Count
  • friend void setX(Count , int)
  • public
  • Count()x0
  • void print() const coutltltxltltendl
  • private
  • int x

8
Example continue
  • void setX(Count c, int val)
  • c.x val
  • int main()
  • Count counter
  • counter.print()
  • setX(counter, 8)
  • return 0

9
Use this pointer
  • Every object has access to its own address
    through a pointer called this
  • This pointer is not part of object
  • This pointer is passed into object (by compiler)
    as implicit first argument on every non-static
    member function

10
  • class Test
  • int x
  • public
  • Test( int 0)
  • void print() const
  • Testtest(int a) x a
  • void Testprint() const
  • coutltlt x ltltx
  • ltlt\nthis-gtx ltltthis-gtx
  • ltlt\n(this).x ltlt(this).x ltltendl

11
  • int main()
  • Test t(12)
  • t.print()
  • return 0

Output
x 12 this-gtx 12 (this).x 12
12
Operator Overloading
  • Enable Cs operators to work with class object
  • ltlt, gtgt, , -, , /
  • This operators perform differently depending on
    their context in integer, floating
  • These operators are overloaded
  • C allow programmer to overload most operators

13
Can we make Complex class easier to use as follow?
  • int main()
  • Complex x,y,z
  • coutltltType in two complex numbers\n
  • cingtgtx gtgty
  • z x y
  • coutltlt x ltlt ltltyltlt ltltzltltendl

Type in two complex numbers (2,3) (3,4) (2,3)
(3,4) (5,7)
14
Yes, Overload the operators
  • Operator are overloaded by writing a function
    definition( header and body)
  • Function name become the keyword operator
    followed by the symbol for the operator being
    overloaded
  • operator would be used to overload the
    addition operator()
  • Precedence and associativity of an operator
    cannot be changed by overloading

15
Where to define overloading operator
  • As non member function
  • Must be friend of the class
  • So it is in class definition
  • class Complex
  • friend ostream operatorltlt
  • (ostream, const Complex )
  • ...

Function name
Return type
parameters
16
Implementation
  • ostream operatorltlt(ostream out, const
    Complex c)
  • outltlt(ltltc.realltlt,ltltc.imgltlt)
  • return out
  • // enables coutltltxltlty

17
Overload operator gtgt
  • In class definition
  • class Complex
  • friend ostream operatorltlt
  • (ostream , const Complex )
  • friend istream operatorgtgt
  • (istream, Complex )
  • ...

18
implementation
  • istream operatorgtgt( istream
  • input,Complex c)
  • input.ignore()
  • inputgtgtc.real
  • input.ignore()
  • inputgtgtc.img
  • input.ignore()
  • return input
Write a Comment
User Comments (0)
About PowerShow.com