Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
c++ notes by vikas sharma

# c++ notes by vikas sharma

Ratings: (0)|Views: 132|Likes:

Categories:Types, School Work

### Availability:

See more
See less

06/10/2013

pdf

text

original

1
Dale Roberts
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
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
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;