Docsity

Syntax for Overloading Operators - Introduction to Programming - Lecture Slides, Slides of Computer Programming

Partial preview of the text

Download Syntax for Overloading Operators - Introduction to Programming - Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 31

Operator

Overloading

Complex

Number

complex c1 , c2 , x ;

x = cadd ( c1 , c2 ) ;

Operators  The complete list of C++ operators that are overloaded is as follows

      • / % ^ & | ~! = < > += -= *= /= %= ^= &= |= << >> >>= <<= = = != <=

= && | | ++ - - -> * , -> [ ] ( ) new new[ ] delete delete [ ] Docsity.com

a + b

Example

Return_type operator + (Argument_List)

{

// Body of function

}

a * b + c ;

Example

class Complex

{

private : double real ; double imag ; public : // member function

} Docsity.com

Example

Complex c1 , c2 ;

c1 = c2 ;

Is equivalent to

c1.real = c2.real ;

c1.imag = c2.imag ;

Complex Complex :: operator + ( Complex c )

{

Complex temp ; temp.real = real + c.real ; temp.imag = imag + c.imag ; return temp ;

}

Example

Complex x , y , z ;

z = x + y ;

Complex operator + ( double d ) ;

z = x + y ;

z = x + d ;