diff --git a/Cpp/To swap two objects of a same class.cpp b/Cpp/To swap two objects of a same class.cpp new file mode 100644 index 0000000..fc799aa --- /dev/null +++ b/Cpp/To swap two objects of a same class.cpp @@ -0,0 +1,34 @@ +#include +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 "<