Yahoo Interview Question
Software Engineer / Developersif the data member are static then the member wont be affected after passing by value .
but if data member is dynamic the it will be modified when pass by reference .
#include <iostream.h>
#include <conio.h>
class base
{
public :
int a[3];
int *ptr;
base()
{
a[0]=5;a[1]=10;a[2]=20;
ptr=new int[1];
*ptr=100;
}
void display()
{
cout<<a;
}
void vdisplay()
{
cout<< " this is the base virtual";
}
};
int main()
{
void modify(base obp);
base ob1;
cout<<"\n\n "<<" "<<ob1.a[0]<<" "<<ob1.a[1]<<" "<<ob1.a[1]<<" ptr= "<<*ob1.ptr;//5 10 10 ptr=100
modify(ob1);
cout<<"\n\n "<<" "<<ob1.a[0]<<" "<<ob1.a[1]<<" "<<ob1.a[1]<<" ptr="<<*ob1.ptr;//5 10 10 ptr=999
getch();
}
void modify(base obp)
{
obp.a[0]=999;obp.a[1]=999;obp.a[2]=999;*obp.ptr=999;
}
output :
5 10 10 ptr=100
5 10 10 ptr=999
if the data member are static then the member wont be affected after passing by value .
but if data member is dynamic the it will be modified when pass by reference .
#include <iostream.h>
#include <conio.h>
class base
{
public :
int a[3];
int *ptr;
base()
{
a[0]=5;a[1]=10;a[2]=20;
ptr=new int[1];
*ptr=100;
}
void display()
{
cout<<a;
}
void vdisplay()
{
cout<< " this is the base virtual";
}
};
int main()
{
void modify(base obp);
base ob1;
cout<<"\n\n "<<" "<<ob1.a[0]<<" "<<ob1.a[1]<<" "<<ob1.a[1]<<" ptr= "<<*ob1.ptr;//5 10 10 ptr=100
modify(ob1);
cout<<"\n\n "<<" "<<ob1.a[0]<<" "<<ob1.a[1]<<" "<<ob1.a[1]<<" ptr="<<*ob1.ptr;//5 10 10 ptr=999
getch();
}
void modify(base obp)
{
obp.a[0]=999;obp.a[1]=999;obp.a[2]=999;*obp.ptr=999;
}
output :
5 10 10 ptr=100
5 10 10 ptr=999
Deep Copy --> Make another copy of the variable or class.
- Anonymous September 19, 2008Shallow Copy --> Make the pointer point to what the other pointer points to.