Friend Function & Friend Class
Friend Function & Friend Class
&
Friend Class
Why we need Friend Function?
•The private data of a class can’t be accessed
from the outside of the class.
• But consider the following situation,There are two
classes manager and scientist. We would
like to use income_tax() function to operate on the
objects of these classes.
• In such situation C++ allows the common
function to be made friendly with both the classes.
Friend function declaration
class ABC{
………
………
public:
………
………
friend void xyz(); //declaration
};
The function declaration should be preceded by the
keyword friend.
The function is defined elsewhere in the program.
The function definition does not use either the keyword
friend or the scope resolution operator ::
Program to demonstrate friend function
class Alpha{
int a;
int b;
public:
void set();
friend void add(Alpha ob2); //add is declared as friend to class Alpha
};
void Alpha :: set(){ //member function set() is defined outside the class
a=10;
b=20;
}
void add(Alpha ob2){ //add() is a normal C++ function
int sum= ob2.a + ob2.b;
cout<<“Sum=“<<sum;
}
int main(){
Alpha ob1;
ob1.set();
add(ob1);
return 0;
}
Friend Function properties
• It is not in the scope of the class to which it has
been declared as friend.
class Y {
int b;
public:
Y(int b1) { b = b1; } Member function add() of Y is
void add (X p) { accessing the private data of X
cout<< ( b + p.a) So X must declare add() of Y as
} its friend
};
Friend Class
All the member functions of one class are friend functions of another class.
class Alpha{
…..
int fun1();
float fun2(); // member function of class Alpha
…..
};
class Beta{
…..
friend int Alpha :: fun1(); // fun1() of Alpha is friend of Beta
friend int Alpha :: fun2(); // fun2() of Alpha is friend of Beta
…..
};
class Beta{
…..
friend class Alpha; // All member functions of Alpha are friends to Beta
…..
};
Here the class Alpha is the friend class of class Beta i.e. all the member
functions of class Alpha will be friend of class Beta.
class X { int main() {
int a; X x1(5);
public: Y y1(10);
X(int a1) { a = a1; } y1.add(x1);
friend class Y; y1.sub(x1);
}; return 0;
}
class Y {
int b;
public:
Y(int b1) { b = b1; }
void add (X p) Member functions add() and
{ sub() of Y is accessing the
cout<< ( b + p.a) private data of X
} So X must declare add() and
void sub (X p) sub() of Y as its friends
{ otherwise it has to declare the
cout << ( b - p.a) whole Y class as its friend.
}
};
Operator Overloading
Introduction
• It is a mechanism of adding some extra features to the existing
operators so that they can act upon
• objects to treat the class as built-in data type.
• The mechanism of giving special meanings to an existing operator
is called operator overloading.
class Alpha{
The semantics of an operator can be extended but
we can’t change its syntax.
………
………
};
int main(){
Alpha ob1,ob2,ob3; // int a,b,c;
ob3 = ob1 + ob2 ; // c = a + b;
}
The semantics of an operator can be extended but we can’t change
its syntax.
Introduction…
Operator overloading is done by using a special function
called operator function.
The general form of operator function is:
Unary Binary
}
void operator-()
{a = -a;} The compiler interprets –x1 as
}; x1.operator-();
Using member function
class X {
int a; int main( )
public: {
X() { }
X(int b) { a=b; } X x1(5);
void disp() X x2;
{ x1.disp(); // 5
cout<<a;
} x2=-x1;
X operator-(); x2.disp(); // -5
};
return 0;
X X :: operator-() {
X temp; }
temp.a = -a;
return (temp);
} The compiler interprets x2=–x1 as
x2 = x1.operator-();
Using friend function
class X {
int a; int main( )
public: {
X() { }
X(int b) { a=b; } X x1(5);
void disp() {cout<<a; } X x2;
x1.disp(); // 5
friend X operator-(X obj);
}; x2=-x1;
x2.disp(); // -5
X operator-(X ob) {
X temp; return 0;
temp.a = -ob.a;
return (temp); }
}
The compiler interprets x2=–x1 as
x2 = operator-(x1);
Using friend function
class X { int main( )
int a; {
public: X x1(5);
X() { } x1.disp(); // 5
X(int b) { a=b; } -x1;
void disp() {cout<<a; } x1.disp(); // -5
friend void operator-(X obj); return 0;
};
}
void operator-(X ob) {
ob.a=-ob.a;
Wrong Output 5
}
– Assignment Operator ( = )
– Function Call Operator ( () )
– Subscription Operator ( [] )
– Class Member Access Operator ( -> )
Assignments
• WAP to create a Date class, increment the day, month, year of
the Date class using operator overloading mechanism .