CASE:1
Given two numbers a & b, swap these two numbers using the friend function of C++.
Examples:
Input : a = 15, b = 9
Output : a = 9, b = 15
Input : a = 4, b = 16
Output : a= 16, b = 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 = 10Output : a = 10, b = 6Input : a = 4, b = 6Output : 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.
Comments