Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
5Activity
0 of .
Results for:
No results containing your search query
P. 1
c++ notes by vikas sharma

c++ notes by vikas sharma

Ratings: (0)|Views: 132|Likes:
Published by vikas_sharma12
u can dowload this doc
u can dowload this doc

More info:

Categories:Types, School Work
Published by: vikas_sharma12 on Nov 08, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

06/10/2013

pdf

text

original

 
 1
Dale Roberts
Operator OverloadingOperator Overloading 
Friend Functions & Special FormsFriend Functions & Special Forms
 Dale Roberts, Lecturer 
Computer Science, IUPUIE-mail: droberts@cs.iupui.edu
Department of Computer and Information Science,School of Science, IUPUI
 
 
Dale Roberts
2
Operator Overloading Using a Friend FunctionOperator Overloading Using a Friend Function
Number of Parameters Accepted by an Overloaded Friend Operator Number of Parameters Accepted by an Overloaded Friend Operator Function Depend Upon the Operator Type -- One for Unary Operators andFunction Depend Upon the Operator Type -- One for Unary Operators andTwo for Binary OperatorsTwo for Binary Operators
class complex{class complex{int re, im;int re, im;public: public:complex(int ip1 = 0, int ip2 = 0)complex(int ip1 = 0, int ip2 = 0):re(ip1), im(ip2){}:re(ip1), im(ip2){}friend complex operator+(complex, complex);friend complex operator+(complex, complex);};};//Friend Operator + Function//Friend Operator + Functioncomplex operator+(complex a, complex b)complex operator+(complex a, complex b){return complex(a.re+b.re, a.im+b.im);}{return complex(a.re+b.re, a.im+b.im);}main(){ main(){complex one(1,1), two(2,2), three;complex one(1,1), two(2,2), three;three = one + two; //three = operator+(one, two)three = one + two; //three = operator+(one, two)}}
 Is a friend function necessary in this case? No because LH operand is an instance of the class.
 
 
Dale Roberts
3
Operator Functions as Class Members vs. asOperator Functions as Class Members vs. as
friendfriend
FunctionsFunctions
Non-member overloaded operator functionsNon-member overloaded operator functions
Enable the operator to be commutativeEnable the operator to be commutative
 
HugeInteger bigInteger;HugeInteger bigInteger;int integer;int integer;bigInteger = integer + bigInteger; bigInteger = integer + bigInteger;
 or or  
 bigInteger = bigInteger + integer; bigInteger = bigInteger + integer;
 

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->