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

Syntax for overloading operators, Overload operators, Complex Number, Complete list of C operators, Friend Function, User Defined Data types, String are the key points of this lecture.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming
Lecture 31
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

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 ;