Operator Overloading (2) - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Operator Overloading (2)

Description:

Operator Overloading (2) Operator function can be implemented as class member function Then the left most (or only) operand must be an class object( or a reference to ... – PowerPoint PPT presentation

Number of Views:116
Avg rating:3.0/5.0
Slides: 7
Provided by: Hong129
Category:

less

Transcript and Presenter's Notes

Title: Operator Overloading (2)


1
Operator Overloading (2)
  • Operator function can be implemented as class
    member function
  • Then the left most (or only) operand must be an
    class object( or a reference to a class object)
  • If the left operand must be a object of a
    different class or build-in type, this operator
    function must be implemented as a non-member
    function( as we did for gtgt, ltlt)

2
Arithmetic operators
  • , - , , /, , -, , /
  • In header
  • class Complex
  • friend ostream operatorltlt
  • (ostream , const Complex )
  • friend istream operatorgtgt
  • (istream, Complex )
  • Complex operator(const Complex c,)const
  • ...

3
implementation
  • Complex Complexoperator(
  • Complex c)
  • Complex sum
  • sum.real real c.real
  • sum.img img c.img
  • return sum

4
  • In header
  • Complex operator-(const Complex c)
  • Implementation
  • Complex Complexoperator-(const complex c)
  • return Complex(real-c.real, imgc.img)

5
  • In header
  • Complex operator(const Complex c)
  • Implementation
  • Complex Complexoperator(const Complex c)
  • Complex mul
  • mul.real realc.real-imgc.img
  • mul.img realc.img imgc.img
  • return mul

6
  • In header
  • Complex operator/(const Complex c)
  • Implementation
  • Complex Complexoperator(const Complex c)
  • Complex div
  • double dem
  • dem c.realc.real c.imgc.img
  • div.real (realc.realimgc.img)/dem
  • div.img (imgc.img -realc.img )/dem
  • return div
  • Completed Complex class
  • Header File Implementation file Driver file
Write a Comment
User Comments (0)
About PowerShow.com