Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Cpp/To swap two objects of a same class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;
class y;
class x{
int num;
public:
x(){
cout<<"Enter a number: ";
cin>>num;
}
friend void swap(x &,y &);
};
class y{
int data;
public:
y(){
cout<<"Enter a number: ";
cin>>data;
}
friend void swap(x &,y &);
};
void swap(x &o1,y &o2){
int temp;
temp=o1.num;
o1.num=o2.data;
o2.data=temp;
cout<<"Objects after swapping are "<<o1.num<<" and "<<o2.data;
}

int main(){
x x1;
y y1;
swap(x1,y1);
}