#include<iostream.h>
#include<conio.h>
class cpp
{
public:
void call_by_value(int,int);
void call_by_reference(int*,int*);
int & return_by_reference(int &,int &);
void ref(int &,int &);
};
int & cpp::return_by_reference(int &x,int &y)
{
if(x>y)
return x;
else
return y;
}
void cpp::ref(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
cout<<"\na is:"<<x<<"\tb is:"<<y;
}
void cpp::call_by_value(int x,int y)
{
int t;
t=x;
x=y;
y=t;
cout<<"\nvalue of a="<<x<<"\nvalue of b="<<y;
}
void cpp::call_by_reference(int *x,int *y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
cout<<"\nvalue of a="<<*x<<"\nvalue of b="<<*y;
}
void main()
{
cpp r;
int k,c,a,b;
clrscr();
do
{
cout<<"\n*********************************************\n";
cout.width(30);
cout<<"PASSING ARGUMENTS\n";
cout<<"***********************************************\n";
cout<<"1.pass by value\n";
cout<<"2.pass by reference\n";
cout<<"3.return by reference\n";
cout<<"4.ref\n5.exit";
cout<<"***********************************************\n";
cout<<"Enter u r choice";
cin>>k;
switch(k)
{
case 1:
cout<<"\nEnter a and b";
cin>>a>>b;
r.call_by_value(a,b);
break;
case 2:
cout<<"\nEnter a and b";
cin>>a>>b;
r.call_by_reference(&a,&b);
break;
case 3:
cout<<"\nEnter a and b";
cin>>a>>b;
c=r.return_by_reference(a,b);
cout<<"\nMaximum value"<<c;
break;
case 4:
cout<<"\nenter a,b:";
cin>>a>>b;
r.ref(a,b);
}
}while(k!=5);
getch();
}
Sunday, 7 December 2014
C++ program to view the difference between pass by value, pass by reference, return by reference
Posted by Madan
Posted on 13:48





