/* c++ program to swap no between two objects of diff class */
#include<iostream>
using namespace std;
class A
{
public:
int num1;
void input()
{
cout<<"\nenter the number:";
cin>>num1;
}
void display()
{
cout<<num1<<endl;
}
friend class B;
};
class B
{
public:
int num2;
void input()
{
cout<<"\nenter the second number:";
cin>>num2;
}
void display()
{
cout<<num2<<endl;
}
friend class A;
};
void swapping(A &A1,B &B1)
{
int temp;
temp=A1.num1;
A1.num1=B1.num2;
B1.num2=temp;
}
int main()
{
A A11;
B B11;
A11.input();
B11.input();
cout<<"number before swapping:";
A11.display();
B11.display();
swapping(A11,B11);
cout<<"number after swapping:";
A11.display();
B11.display();
return 0;
}
#include<iostream>
using namespace std;
class A
{
public:
int num1;
void input()
{
cout<<"\nenter the number:";
cin>>num1;
}
void display()
{
cout<<num1<<endl;
}
friend class B;
};
class B
{
public:
int num2;
void input()
{
cout<<"\nenter the second number:";
cin>>num2;
}
void display()
{
cout<<num2<<endl;
}
friend class A;
};
void swapping(A &A1,B &B1)
{
int temp;
temp=A1.num1;
A1.num1=B1.num2;
B1.num2=temp;
}
int main()
{
A A11;
B B11;
A11.input();
B11.input();
cout<<"number before swapping:";
A11.display();
B11.display();
swapping(A11,B11);
cout<<"number after swapping:";
A11.display();
B11.display();
return 0;
}
No comments:
Post a Comment