You are on page 1of 2

Program No.

Aim:- Write a Program in C++ to demonstrate the use of friend


function.
Program:-
#include<iostream.h>
class average
{
private:
int a,b;
public:
void setdata(int,int);
void display(void);
friend int mean(average);
};
void average::setdata(int m,int n)
{
a=m;
b=n;
}
void average::display(void)
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
SHRI VASANTRAO NAIK MAHAVIDHYALAYA, DHARNI Page 1
Program No. 4

int mean(average y)
{
return(y.a+y.b)/2.0;
}
void main()
{
average x;
x.setdata(10,15);
x.display();
cout<<"Average="<<mean(x);
}
---------------------------------------Output-----------------------------------
a=10
b=15
Average=12

SHRI VASANTRAO NAIK MAHAVIDHYALAYA, DHARNI Page 2

You might also like