Friend Functions Cpp
Friend Functions Cpp
Friend functions are actually not class member function. Friend functions are made to
give private access to non-class functions. You can declare a global function as friend, or a
member function of other class as friend.
Hence, friend functions can access private data members by creating object of the class.
Similarly we can also make function of some other class as friend, or we can also make an
entire class as friend class.
When we make a class as friend, all its member functions automatically become friend
functions.
In this example we have two classes XYZ and ABC. The XYZ class has two private data
members ch and num, this class declares ABC as friend class. This means that ABC can access
the private members of XYZ, the same has been demonstrated in the example where the
function disp() of ABC class accesses the private members num and ch. In this example we
are passing object as an argument to the function.
#include <iostream.h>
class XYZ
private:
char ch;
int num;
public:
void read()
cin>>ch>>num;
};
class ABC
public:
cout<<obj.ch<<endl;
cout<<obj.num<<endl;
};
int main() {
ABC obj;
XYZ obj2;
Obj2.read();
obj.disp(obj2);
return 0;
Friend Function:
Similar to friend class, this function can access the private and protected members of
another class. A global function can also be declared as friend as shown in the example below:
#include <iostream.h>
#include<conio.h>
Alphabet Computer Education, Narayangaon, 9960552002
class XYZ
private:
int num;
char ch;
public:
void read()
cin>>ch>>num;
};
cout<<obj.num<<endl;
cout<<obj.ch<<endl;
int main()
XYZ obj;
Obj.read();
disp(obj);
getch();
Alphabet Computer Education, Narayangaon, 9960552002
return 0;
#include <iostream.h>
#include<conio.h>
class B;
class A
private:
int numA;
public:
read(int n)
numA=n;
};
class B {
private:
int numB;
public:
put(int m)
{
Alphabet Computer Education, Narayangaon, 9960552002
numB=m;
};
int main()
A objectA;
B objectB;
A.read(6);
A.read(43);
getch();
return 0;
#include <iostream.h>
#include<conio.h>
class Box
double width;
public:
Alphabet Computer Education, Narayangaon, 9960552002
};
width = wid;
int main()
Box box;
box.setWidth(10.0);
printWidth( box );
getch();
return 0;
#include<iostream.h>
#include<conio.h>
const m=50;
class ITEMS
int itemCode[m];
Alphabet Computer Education, Narayangaon, 9960552002
float itemPrice[m];
int count;
public:
count=0;
void getitem(void);
void displaysum(void);
void remove(void);
void displayitems(void);
};
//===================================
cin>>itemCode[count];
cin>>itemPrice[count];
count++;
float sum=0;
for(int i=0;i<count;i++)
{
Alphabet Computer Education, Narayangaon, 9960552002
sum=sum+itemPrice[i];
int a;
cout<<”Enter itemcode : “;
cin>>a;
for(int i=0;i<count;i++)
if(itemCode[i] == a)
itemPrice[i] = 0;
for(int i=0;i<count;i++)
cout<<”\n”<<itemCode[i];
cout<<”\n”<<itemPrice[i];
cout<<”\n”;
Alphabet Computer Education, Narayangaon, 9960552002
int main()
ITEMS order;
order.CNT();
int x;
do
cin>>x;
switch(x)
case 1 : order.getitem();break;
case 2 : order.displaysum();break;
case 3 : order.remove();break;
case 4 : order.displayitems();break;
case 5 : break;
}
Alphabet Computer Education, Narayangaon, 9960552002
while(x != 5);
getch();
return 0;