From 759b2520436c7f33c3ea9fdf55eaffe37c3dabc9 Mon Sep 17 00:00:00 2001 From: nikkhilbisht24 <97297220+nikkhilbisht24@users.noreply.github.com> Date: Sat, 15 Oct 2022 21:34:20 +0530 Subject: [PATCH] Create To swap two objects of a same class.cpp This is a code for swapping objects of a class. --- Cpp/To swap two objects of a same class.cpp | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Cpp/To swap two objects of a same class.cpp 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 "<