C++ Program to swap two members using Friend Function
Pre-requisite: Friend Function
CASE 1:
Given two numbers a & b, swap these two numbers using friend function of C++.
Examples:
Input : a = 5, b = 9 Output : a = 9, b = 5 Input : a = 4, b = 6 Output : a= 6, b = 4
Approach:
Create a class Swap, declare three variable in it, i.e., a, b, and temp and create a constructor for inputs. Declare a friend function in it. Define the friend function outside the class scope by taking arguments as call by reference to pass the copy of Swap Object. Perform the swap operation with Swap variables.
// C++ Program to swap two numbers using friend function #include <iostream> using namespace std; class Swap { // Declare the variables of Swap Class int temp, a, b; public: // Define the parametrized constructor, for inputs Swap(int a, int b) { this->a = a; this->b = b; } // Declare the friend function to swap, take arguments // as call by reference friend void swap(Swap&); }; // Define the swap function outside class scope void swap(Swap& s1) { // Call by reference is used to passed object copy to // the function cout << "\nBefore Swapping: " << s1.a << " " << s1.b; // Swap operations with Swap Class variables s1.temp = s1.a; s1.a = s1.b; s1.b = s1.temp; cout << "\nAfter Swapping: " << s1.a << " " << s1.b; } // Driver Code int main() { // Declare and Initialize the Swap object Swap s(4, 6); swap(s); return 0; } |
Before Swapping: 4 6 After Swapping: 6 4
CASE 2:
Given two objects s1 & s2 of a class, swap the data members of these two objects using friend function of C++.
Examples:
Input : a = 6, b = 10 Output : a = 10, b = 6 Input : a = 4, b = 6 Output : a= 6, b = 4
Approach:
Create a class Swap, declare one variable in it, i.e., num and create a constructor for inputs. Declare a friend function in it. Define the friend function outside the class scope by taking arguments as call by reference to pass the copy of Swap Object. Perform the swap operation.
//C++ Program to swap data members of two objects of a class using friend function. #include <iostream> using namespace std; class Swap { // Declare the variable of Swap Class int num; public: // Define the parametrized constructor, for input. Swap(int num) { this->num = num; } // Declare the friend function to swap, take arguments // as call by reference friend void swap(Swap&, Swap&); }; // Define the swap function outside class scope void swap(Swap& s1, Swap& s2) { // declare a temporary variable. int temp; // Call by reference is used to passed object copy to // the function cout << "\nBefore Swapping: " << s1.num << " " << s2.num; // Swap operations with objects of class Swap temp = s1.num; s1.num = s2.num; s2.num = temp; cout << "\nAfter Swapping: " << s1.num << " " << s2.num; } // Driver Code int main() { // Declare and Initialize the objects of Swap class Swap s1(6), s2(10); swap(s1,s2); return 0; } |
Before Swapping: 6 10 After Swapping: 10 6
Recommended Posts:
- Can we access private data members of a class without using a member or a friend function?
- Friend class and function in C++
- C Program to Swap two Numbers
- unordered_set swap() function in C++ STL
- unordered_multiset swap() function in C++ STL
- valarray swap() function in c++
- multimap swap() function in C++ STL
- unordered_multimap swap() function in C++ STL
- Difference between std::swap and std::vector::swap
- Initialization of data members
- Static data members in C++
- Different ways to Initialize all members of an array to the same value in C
- set::swap() in C++ STL
- swap() in C++
- map::at() and map::swap() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : routpranab03



