c++ program to illustrate the friend function,here we are finding mean of two numbers.
//wap in c++ to illustrate the friend function,here we are finding mean of two numbers.
#include<iostream>
using namespace std;
class sample
{
int num1,num2;
public :
void setval();
/*here we are passing sample as argument as we are going to pass objects as argument
this is the syntax of Passing Objects as Function Arguments*/
friend float mean(sample);
};
void sample::setval()
{
cout<<"Enter two numbers";
cin>>num1>>num2;
}
//here we are passing x with sample as we are Passing Object(t) as Function Arguments.
float mean(sample x)
{
return ((x.num1+x.num2)/2);
}
int main(){
sample s;
s.setval();
//we can not write mean(s) as s.mean as this is the friend function.
cout<<mean(s);
}
#include<iostream>
using namespace std;
class sample
{
int num1,num2;
public :
void setval();
/*here we are passing sample as argument as we are going to pass objects as argument
this is the syntax of Passing Objects as Function Arguments*/
friend float mean(sample);
};
void sample::setval()
{
cout<<"Enter two numbers";
cin>>num1>>num2;
}
//here we are passing x with sample as we are Passing Object(t) as Function Arguments.
float mean(sample x)
{
return ((x.num1+x.num2)/2);
}
int main(){
sample s;
s.setval();
//we can not write mean(s) as s.mean as this is the friend function.
cout<<mean(s);
}
Comments
Post a Comment