Академический Документы
Профессиональный Документы
Культура Документы
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 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
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(); }