You are on page 1of 12

01

02

LECTURE 22
04
Today’s Agenda
01 Necessity of Friend Functions

02 A Function Being Friend of 2 Classes

03 A programming Challenge

05 Forward Declaration

06
05 Friend Classes

05
Solution of Assignment Given in last Class

class Student cin>>P.roll>>P.grade>>P.per;


{ }
private: void Student::show()
int roll; {
char grade; cout<<roll<<", "<<grade<<", "<<per<<endl;
float per; }
public: int main()
.
friend void get(Student &); {
void show(); Student S;
}; get(S);
void get(Student & P) S.show();
{ return 0;
cout<<"Enter roll, grade and per: "; }
Necessity of use of friend function

1. Programming Situation where friend function is {


private:
used
int b;
 Solve the programming challenge given below public:
class Alpha ....
{ .... Some
private: }; Member
int a; int main() functions
public: {
.... Some
Member
. Alpha obj1; Initialization of
.... Beta obj2; Object using
functions
}; .... There member
.... functions
Create object of both the //
}
classes and compare whether Compare
obj1 & obj2
the a of Alpha is greater or b
of Beta is greater
class Beta
Reason

1. The above-given problem can't be solved as the member function of Alpha can't
access the member function of Beta and the vice versa of the above scenario
will also encounter the same problem

2. Now the only solution is that we have to create a friend function which is the
friend of both the classes which will be parametrized and will take the object as
.
a parameter of both the classes so we can access the private data member
inside a single function
A Function Being Friend of 2 Classes

class Beta; }
class Alpha friend void compare(const Alpha &, const Beta &); obj1
{ };
private: void compare(const Alpha & P, const Beta & Q)
int a; This kind of {
a
public: if(P.a > Q.b) 10
void get() statement is cout<<"Greater is "<<P.a<<endl;
{ called Forward else if(Q.b > P.a)
cout<<"Enter a: ";
cin>>a; Declaration
cout<<"Greater is "<<Q.b<<endl;
else
1000
} cout<<"Both are equal"<<endl;
friend void compare(const Alpha &, const Beta &); .
}
}; int main()
class Beta
{
{
Alpha obj1;
obj2
private: Beta obj2;
int b; There declaration obj1.get();
public: obj2.set(); b 20
void set() is important compare(obj1, obj2);
{ return 0;
cout<<"Enter b: "; }
cin>>b; 2000
Forward Declaration
FORWARD DECLARATION
In this, the declaration of a class is given prior to the definition of the class
this is required when class is in use before the definition of class

1. Suppose we have one more class in the above-given example let say Gamma then
we have to declare the function inside the Gamma class, then our code will contain
total 3 classes, and friend function will be declared in 3 places i.e. inside all the
above mentioned 3 classes and the total forward declaration will be 2 i.e for class
Beta and class Gamma

2. A friend function is required where we have to perform a common operation


between two different classes. Here the operation will not be possible using a single
member function we have to create a common friend function for both the classes
Friend Classes
A friend class is a special class that is allowed to access all the private members of the class of
which it has been declared a friend. In other words, if class B has been declared a friend of
class A, then all the member functions of class B can access all the private members of class A
 Example:-
class A
{
private:
....
public:
....
friend class B;
Declare B as
}; a friend of A
class B
{
private: Every member functions of B can access
.... Private members of class A
public:
....
};
Example of Friend Classes
.class
{
Num void add(const Num &);
void display();
private: };
int a; void AddNum::add(const Num &P) obj1
int b; {
public: c = P.a + P.b; a 10
void get() }
{ void AddNum::display() b 20
cout<<"Enter a and b: "; {
cin>>a>>b; cout<<"Sum is: "<<c<<endl;
} } (1000)
void show() int main()
{ {
cout<<a<<", "<<b<<endl; Num obj1; obj2
} AddNum obj2;
friend class AddNum; obj1.get();
}; obj2.add(obj1); c 30
class AddNum obj1.show();
{ obj2.display();
private: return 0; (2000)
int c; }
public:
Example of Friend Classes
.class
{
Num
Another void AddNum::add(const Num &P)
private: { obj1
int a; Way c = P.a + P.b;
int b; } a 10
public:
void get() void AddNum::display(const Num &P) b 20
{ {
cout<<"Enter a and b: "; cout<<"Numbers are: "<<P.a<<", "<<P.b<<endl;
cin>>a>>b; cout<<"Sum is: "<<c<<endl; (1000)
} }
friend class AddNum;
}; int main()
obj2
{
class AddNum Num obj1;
{ AddNum obj2; c 30
private: obj1.get();
int c; obj2.add(obj1);
public: obj2.display(obj1); (2000)
void add(const Num &); return 0;
void display(const Num &); }
};
Output of The Previous Code

Output:-

Enter a and b: 10 20
Numbers are: 10, 20
The sum is: 30

Process returned 0 (0x0) execution time : 4.497 s


Press any key to continue.
End of Lecture 22
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like