Friend function example with surce code in c++,
Simple Friend function
#include<iostream>
using namespace std;
class myclass{
int a, b;
public:
friend int add(void);
friend int sub(void);
void setval(){
cout<<" value of a: "; cin>>a;
cout<<"value of b: ";
cin>>b;
}
};
int add(void){
myclass obj;
obj.setval();
int tenp;
//cout<<"A + B = "<<obj. a + obj.b;
tenp = obj.a + obj. b;
return tenp;
}
int sub(void){
myclass obj ;
obj. setval();
int temp;
temp = obj. a - obj.b;
return temp;
}
int main(){
int result,result2;
result = add();
cout<<result<<endl;
result2 = sub();
cout<<result2;
return 0;
}
Comments
Post a Comment