Friend Function and Operator Overloading
Friend Function and Operator Overloading
Operator Overloading
Friendly function
If a function is defined as a friend function then,
the private and protected data of class can be
accessed from that function.
The complier knows a given function is a friend
function by its keywordfriend.
The declaration of friend function should be made
inside the body of class starting with keyword
friend.
Operator Overloading
Redefine the meaning of operator when they
operate on class objects is known as operator
overloading.
This is done by operator function.
Example
int nX = 2, nY = 3;
cout << nX + nY << endl;
C++ already knows how the plus operator
(+) should be applied to integer operands
the compiler adds nX and nY together and
returns the result.
&
<
>
+=
-=
*=
/=
%=
^=
&=
|=
<<
>>
>>=
<<=
==
!=
<=
>=
&&
||
++
--
->*
->
[]
()
new
delete
new[]
delete[]
Operators
C++that
Operators
that cannot be
cannot be overloaded
. overloaded
.*
::
?:
sizeof
Restrictions on Operator
Overloading
Overloading restrictions
1. Member function
OR
symbol operands
+,_,*,/, other than
etc
one being
Function body.
used to
}
invoke this
op
2.Defined inside the class
OVEROADING ARITHMETIC
OPERATORS
void show()
{
cout<<"x= "<<x<<" y= "<<y<<" z= "<<z;
}
friend void operator-(space &s);
};
void operator-(space &s)
{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
void main()
{
space s(3,-4,5);
s.show();
-s;
// equivalent to operator-(s);
// s2=-s will not work bcoz no val returned
s.show();
}
complex complex::operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
void main()
{
complex c1(3,4);
complex c2(5,6);
complex c3;
c1.show();
c2.show();
c3=c1+c2;
// equivalent to
c3=c1.operator+(c2)
c3.show();
}
C3=C1+C2;
As we know that a member function can be
invoked only by an object of the same class.
Here, the object C1 takes the responsibility of
invoking the function and C2 plays the role of
an argument that is passed to the function.
Its equivalent to C3=C1.operator+(C2);
The data members of C1 are accessed
directly and data members of C2 are
accessed using the dot operator.
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
obj1=++obj;
obj.Display();
obj1.Display();
return 0;
}
int main()
{ Check obj, obj1;
obj.Display();
obj1.Display();
obj1=++obj;
obj.Display();
obj1.Display();
obj1=obj++;
obj.Display();
obj1.Display();
return 0; }
Output:i=0
i=0
i=1
i=1
i=2
i=1
Class exercise
Assignment Operator
overloading
Class sample
{
private:
int x, y;
public:
sample(int a,int b)
{
x=a;
y=b;
}
Main(){
Sample obj1(10,20), obj2(3,4);
obj2=obj1;
obj2.display();
}
Example:class PhoneNumber
{
private:
string areaCode; // 3-digit area code
string exchange; // 3-digit exchange
string line; // 4-digit line
public:
friend ostream &operator<<( ostream &, const
PhoneNumber & );
friend istream &operator>>( istream &, PhoneNumber &
);
}; // end class PhoneNumber
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form :" << endl;
cin >> phone // invokes operator>> by implicitly issuing the global function call operator>>( cin,
phone )
cin >> phone;
cout << "The phone number entered was: ; // cout << phone invokes operator<< by implicitly
issuing
// the global function call operator<<( cout, phone )
cout << phone << endl;
return 0;
}
Output
Enter phone number in the form :
800 555 1212
The phone number entered was: (800) 555-1212
Class exercise
Overload the Input
Output operator for
the Complex Class:
Subscript operator[]
const int SIZE = 10; overloading
class ArrayList
{ private:
int arr[SIZE];
public:
ArrayList() ;
int &operator[](int i)
{ if( i > SIZE )
{ cout << "Index out of bounds"
<<endl;
// return first element. return
arr[0];
}
return arr[i];
}
};
ArrayList::ArrayList()
{ register int i;
for(i = 0; i < SIZE; i++)
{ arr[i] = i; }
}
int main()
{ ArrayList A;
cout << "Value of A[2] : " << A[2]
<<endl;
cout << "Value of A[5] : " <<
A[5]<<endl;
cout << "Value of A[12] : " <<
A[12]<<endl; return 0;
}
Output:
Value of A[2] : 2
Value of A[5] : 5
Index out of bounds
Value of A[12] : 0
class IntArray
{ private:
int *ptr;
public:
IntArray(int size) { ptr = new int[size]; }
~IntArray() { delete [] ptr; }
int& operator[](int index);
int& operator()(int index); };
//Function call operator
int& IntArray::operator()(int index){ return
ptr[index]; }
int main(void)
{ IntArray iArray(10);
iArray(0) = 5;
iArray(1) = 3;
cout << iArray(0)
<< endl;
cout << iArray(1)
<< endl; return
0; }
Output
5
3
==Operator
int operator==(MyString obj1,MyString
obj2)
{
int rel=0;
if(strcmp(obj1.value,obj2.value)==0){
rel=1;
}
return rel;
}
!=Operator
int operator!=(MyString obj1,MyString
obj2)
{
int rel=0;
if(strcmp(obj1.value,obj2.value)!=0)
{
rel=1;
}
return rel;
}
>= Operator
int operator>=(MyString obj1,MyString obj2)
{
int rel=0;
int result=0;
rel=strcmp(obj1.value,obj2.value);
if(rel>0 || rel==0) { result=1; }
return result;
}