Swap two variable using call by reference in C++ by R4R Team

Call by reference:
-Here call by reference means we call a function and pass variable reference instead of variable value.
-Pointer are used in such kind of calling.

program:

#include < iostream>
using namespace std;
class Myclass
{
public:
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
};
int main()
{
Myclass obj;
int i=10,j=20;
cout<<"Before function calling"<< endl;
cout<<"i and j is: "<< i<<" "<< j<< endl;
obj.swap(&i,&j);
cout<<"After function calling"<< endl;
cout<<"i and j is: "<< i<<" "<< j;
return 0;
}


output-

Before function calling
i and j is 10 20
After function calling
i and j is 20 10

-When we pass any variable through call by reference then it also effect the actual data.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!