Friend Class

A class can also be declared to be the friend of some other class. When we create a friend class then all the member functions of the friend class also become the friend of the other class. This requires the condition that the friend becoming class must be first declared or defined.

Code in c++

#include<iostream>
using namespace std;
class shape
{
int a,b,c;
public:
void in()
{
cin>>a;
cin>>b;
cin>>c;
}
friend class circle;
};
class circle
{
public:
void multiply(shape obj)
{
cout<<obj.a*obj.b*obj.c;
}
void sum(shape obj)
{
cout<<obj.a+obj.b+obj.c;
}
void average(shape obj)
{
cout<<(obj.a+obj.b+obj.c)/3;
}

};
void main()
{
shape obj;
circle obj2;
obj2.multiply(obj);
obj2.sum(obj);
obj2.average(obj);
getchar();
getchar();
}

Print Friendly

Share this with your friends

One Comment to “Friend Class”

  1. What an awesome way to explain this-now I know evnytrhieg!

Leave a Reply

Your email address will not be published. Required fields are marked *