To swap two numbers using a friend class in C++, you can define a class that has access to the private member variables of the two numbers, and then define a friend function inside the class that performs the swap.
Code:
#include <iostream>using namespace std;
class Number2;class Number1 { private: int a; public: void get(){ cin>>a; }
friend void swapNums(Number1& a, Number2& b); void display() { cout << a << endl; }};
class Number2 { private: int b; public: void get(){ cin>>b; }
friend void swapNums(Number1& a, Number2& b); void display() { cout << b << endl; }};
void swapNums(Number1& a, Number2& b) { int temp = a.a; a.a = b.b; b.b = temp;}
int main() { Number1 num1; Number2 num2; num1.get(); num2.get();
cout << "Before swapping:" << endl; num1.display(); num2.display();
swapNums(num1, num2);
cout << "After swapping:" << endl; num1.display(); num2.display();
return 0;}
#include <iostream>
using namespace std;
class Number2;
class Number1 {
private:
int a;
public:
void get(){
cin>>a;
}
friend void swapNums(Number1& a, Number2& b);
void display() {
cout << a << endl;
}
};
class Number2 {
private:
int b;
public:
void get(){
cin>>b;
}
friend void swapNums(Number1& a, Number2& b);
void display() {
cout << b << endl;
}
};
void swapNums(Number1& a, Number2& b) {
int temp = a.a;
a.a = b.b;
b.b = temp;
}
int main() {
Number1 num1;
Number2 num2;
num1.get();
num2.get();
cout << "Before swapping:" << endl;
num1.display();
num2.display();
swapNums(num1, num2);
cout << "After swapping:" << endl;
num1.display();
num2.display();
return 0;
}
Output:
10 20
Before swapping:
10
20
After swapping:
20
10
Explanation:
This is a C++ program that demonstrates swapping of two numbers using friend functions and classes.
#include <iostream> is a preprocessor directive that includes the iostream header file which contains the declarations for the standard input/output library functions in C++. The directive provides access to the input/output stream objects such as cin and cout.
using namespace std; is a using directive which specifies that the namespace std is to be used in this program. The namespace std contains various standard C++ libraries such as input/output operations (cout, cin), string handling, etc.
The program defines two classes Number1 and Number2. Each class has a private data member and public member functions to get input and display the value.
The swapNums function is a friend function of both Number1 and Number2 classes. It takes two arguments by reference of Number1 and Number2 and swaps the values of their private data members.
In the main function, two objects of Number1 and Number2 are created. The user is prompted to enter the values of the objects using the get function. Then, the initial values of the objects are displayed using the display function.
The swapNums function is called to swap the values of the objects. The final values of the objects are displayed using the display function. Finally, the program returns 0 to indicate successful execution.
Overall, this program demonstrates the use of friend functions and classes to swap the values of two objects.
Related Links:
sign in process initialization failure fix
define a class to declare a character array of size 10 accept the character into the array
design a java program to create to display your name, branch with your college name?
write a java program to display your name and roll number on the terminal
define a class to declare a character array of size 10
0 Comments
Ask Your Queries in the comments