0% found this document useful (0 votes)
41 views34 pages

Introduction To Programming

Operator overloading allows operators like + and - to be used with user-defined types like classes. To overload an operator, a member function is defined with the same name as the operator, along with appropriate arguments. For example, to overload + for a Complex class, a Complex operator+(Complex &) function would be defined to add two complex numbers. Overloaded operators allow user-defined types to be used in expressions and code in the same way as built-in types like integers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views34 pages

Introduction To Programming

Operator overloading allows operators like + and - to be used with user-defined types like classes. To overload an operator, a member function is defined with the same name as the operator, along with appropriate arguments. For example, to overload + for a Complex class, a Complex operator+(Complex &) function would be defined to add two complex numbers. Overloaded operators allow user-defined types to be used in expressions and code in the same way as built-in types like integers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Introduction to Programming

Lecture 31
Operator
Overloading
Today’s Lecture
– Operators
– Syntax for overloading
operators
– How to overload operators ?
Complex
Number
complex c1 , c2 , x ;
x = cadd ( c1 , c2 ) ;
x = cadd ( cadd ( a , b ) , c ) ;

Operators
The complete list of C++ operators that are
overloaded is as follows
+ - * / % ^ &
| ~ ! = < > +=
-= *= /= %= ^= &= |=
<< >> >>= <<= = = != <=
>= && | | ++ - - -> * ,
-> [ ] ( ) new new[ ] delete delete
[]
a+b
Date.day
Example
Return_type operator + (Argument_List)
{
// Body of function
}
a*b+c;
x=y+z;
Example
class Complex
{
private :
double real ;
double imag ;
public :
// member function
}
Example
Complex c1 , c2 ;
c1 = c2 ;

Is equivalent to

c1.real = c2.real ;
c1.imag = c2.imag ;
Complex operator + ( Argument_ list ) ;
Example
Complex Complex :: operator + ( Complex c )
{
Complex temp ;
temp.real = real + c.real ;
temp.imag = imag + c.imag ;
return temp ;
}
Complex x , y , z ;
z=x+y;
z=x+d;
Complex Complex Double
Number Number Precision
Number
Complex operator + ( double d ) ;
z=x+y;
z=x+d;
Example
Complex Complex :: operator + ( Complex c )
{
Complex temp ;
temp.real = real + d ;
temp.imag = imag ;
return temp ;
}
z=d+x;
Complex Complex
Number Double
Precision Number
Number
Friend
Function
User Defined
Data types
<<
Example
main ( )
{
Complex c1 ( 1 , 2 ) , c2 ( 3 , 4 ) , c3 ;
c3 = c1 + c2 ;
c1.display ( ) ;
c2.display ( ) ;
c3.display ( ) ;
}
Complex operator + ( Complex & c ) ;

C is a reference to a
complex number
i += 2 ;
i=i+2;
c1 += c2 ;
Example

Complex operator += ( Complex & c )


Example
Complex Complex :: operator += ( Complex & c )
{
real += c.real ;
imag += c.imag ;
}
Example
Complex operator + ( Complex & c1 , Complex & c2 )
{
Complex temp ;
temp.real = c1.getreal ( ) + c2.getreal ( ) ;
temp.imag = c1.getimag ( ) + c2.getimag ( ) ;
return temp ;
}
class String
Example
{
private :

char s [ 30 ] ;

public :

String ( )
{
strcpy ( s , "" ) ;
}

// Declaration (prototype) of overloaded sum operator

String operator + ( String c ) ;


};
Example
String String :: operator + ( String c )
{
String temp ;
strcpy ( temp.s , "" ) ;
strcat ( temp.s , s ) ;
strcat ( temp.s , c.s ) ;
return temp ;
}

You might also like