#include <iostream>
using namespace std;
int main()
{
int x;
x=10;
cout<<x<<endl;
int &r=x; //r is a reference to x (pointer ==> int *p; p = &x;)
r=20; //same as x=20;
cout<<x<<endl;
x++; //same as r++;
cout<<r<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x,y;
x=10;
int & iRef=x;
y=iRef; //same as y=x;
cout<<y<<endl;
y++; //x and iRef unchanged
cout<<x<<endl<<iRef<<endl<<y<<endl;
return 0;
}
//call by value
#include <iostream>
using namespace std;
void swap(int x, int y);
int main()
{
int a,b;
a=10;
b=20;
cout<<"before swap: a="<<a<<" b="<<b<<endl;
swap(a,b);
cout<<"after swap: a="<<a<<" b="<<b<<endl;
return 0;
}
void swap(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}
//call by reference - using pointer
#include <iostream>
using namespace std;
void swap(int *x, int *y);
int main()
{
int a,b;
a=10;
b=20;
cout<<"before swap: a="<<a<<" b="<<b<<endl;
swap(&a,&b);
cout<<"after swap: a="<<a<<" b="<<b<<endl;
return 0;
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
//call by reference - using Reference variables
#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main()
{
int a,b;
a=10;
b=20;
cout<<"before swap: a="<<a<<" b="<<b<<endl;
swap(a,b);
cout<<"after swap: a="<<a<<" b="<<b<<endl;
return 0;
}
void swap(int &x, int &y)
{
int t;
t=x;
x=y;
y=t;
}