Skip to main content

Swaping value of variable A and B Through Friend Function in Classes








#include <iostream>
using namespace std;
class B;
class A
{int a;
public:
void set_val (int x ){
a=x;}
friend void swap1(A,B);};
class B{
int b;
public:
friend void swap1 (A,B);
void set_val (int y ){
b=y;}};
void  swap1 (A obj1, B obj2){
int temp;
temp =obj1.a  ;
obj1.a=obj2.b;
obj2.b = temp;
cout << "The value of a :"<<  obj1.a  << endl;
cout <<  "The value of b :"<< obj2.b  << endl;}
int main(){
A obj;
B o;
obj.set_val (5);
o.set_val(2);
swap1 (obj,o);
return 0;} 
Output:


Comments