Q 1 write pograme in c++ for passbyvalue/passbyrefrence/passbyaddress .
#include<iostream>
using namespace std;
class passvalue
{
public:
void passbyvalue(int x, int y); //pass by value
void passbyrefrence(int &x, int &y);//pass by refrence.
void passbyaddress(int *x, int *y); //pass by address.
};
void passvalue::passbyvalue(int x, int y)
{
x=10;
y=20;
}
//actual value will be changed .
void passvalue::passbyrefrence(int &x, int &y)
{
//passbyrefence :
cout<<"passbyrefrence called"<<endl;
x=60;
y=50;
}
//actual value will be changed .
void passvalue::passbyaddress(int *x, int *y)
{
cout<<"passbyaddress is called"<<endl;
*x=70;
*y=80;
}
int main()
{
int i=40,j=30;
passvalue B;
cout<<"actual value"<<endl;
cout<<i<<endl<<j<<endl;
B.passbyvalue(i,j);
cout<<"passbyvalue called"<<endl;
cout<<i<<endl<<j<<endl;
cout<<"passbyrefrence called"<<endl;
B.passbyrefrence(i,j);
cout<<i<<endl<<j<<endl;
cout<<"after pasaddress called"<<endl;
B.passbyaddress(&i,&j);
cout<<i<<endl<<j<<endl;
return 0;
}
using namespace std;
class passvalue
{
public:
void passbyvalue(int x, int y); //pass by value
void passbyrefrence(int &x, int &y);//pass by refrence.
void passbyaddress(int *x, int *y); //pass by address.
};
void passvalue::passbyvalue(int x, int y)
{
x=10;
y=20;
}
//actual value will be changed .
void passvalue::passbyrefrence(int &x, int &y)
{
//passbyrefence :
cout<<"passbyrefrence called"<<endl;
x=60;
y=50;
}
//actual value will be changed .
void passvalue::passbyaddress(int *x, int *y)
{
cout<<"passbyaddress is called"<<endl;
*x=70;
*y=80;
}
int main()
{
int i=40,j=30;
passvalue B;
cout<<"actual value"<<endl;
cout<<i<<endl<<j<<endl;
B.passbyvalue(i,j);
cout<<"passbyvalue called"<<endl;
cout<<i<<endl<<j<<endl;
cout<<"passbyrefrence called"<<endl;
B.passbyrefrence(i,j);
cout<<i<<endl<<j<<endl;
cout<<"after pasaddress called"<<endl;
B.passbyaddress(&i,&j);
cout<<i<<endl<<j<<endl;
return 0;
}
Comments
Post a Comment