Вы находитесь на странице: 1из 3

Shallow copy

In shallow copy, if a data field in a class is a pointer to some object, the address of the pointer is copied rather than its contents.

using namespace std; class deep { private: int a; int *b; //pointer public: deep(int a,int b) //constructor { this->a=a; this->b=new int(b); } deep(deep &x) { a=x.a; b=x.b; //shallow copy

} void display() { cout<<"1st value ="<<a<<endl; cout<<"2nd value ="<<*b<<endl; } }; void main() { int o,p; cout<<"Enter the first value"<<endl; cin>>o; cout<<"Enter the second value"<<endl; cin>>p;

deep s(o,p); deep g(s); s.display(); g.display(); }

DEEP COPY
In deep copy, if a data field in a class is a pointer to some object, the contents of that objects are copied rather than its reference.

#include<iostream>

using namespace std; class deep { private: int a; int *b; //pointer public: deep(int a,int b) //constructor { this->a=a; this->b=new int(b); } deep(deep &x) { a=x.a; b=new int(*x.b); //deep copy

} void display() { cout<<"1st value ="<<a<<endl;

cout<<"2nd value ="<<*b<<endl; } }; void main() { int o,p; cout<<"Enter the first value"<<endl; cin>>o; cout<<"Enter the second value"<<endl; cin>>p; deep s(o,p); deep g(s); s.display(); g.display(); }

Вам также может понравиться