Deep and Shallow Copy Code
Deep and Shallow Copy Code
//class
class calculator
{
private:
int a, b, c;
int *p;
public:
int sum(int x, int y, int *z){ //Changing z to *z
a = x;
b = y;
*p = *z; //changing p =z to *p = *z
c = a+b;
}
int show()
{
cout<<"Sum is: "<<c;
}
calculator(){
p = new int;
}
calculator(const calculator &c){
a = c.a;
b = c.b;
//p = c.p;//shallow copy
p = new int;
*p = *(c.p);//Deep Copy
}
};
int main() {
calculator c1;
int ptr = 4;
c1.sum(2,3,&ptr); //passing 4's address to a pointer variable not 4 itself
//c1.show();
//Copy constructor
calculator c2 = c1;
//c2 = c1; //implicit operator overloading
c2.show();
return 0;
}