Create a class Number with a pointer as a data data member of integer type. Write user defined copy constructor to
the class and implement the deep copy for the Number class objects. So when a new object is created from an
old object of the Number class, pointer data member can point different memory block.
#include <iostream>
using namespace std;
class Number{
const int* i;
public:
Number(const int *x){
i = new int[1];
i = x;
}
Number(const Number &N){
i = new int[1];
i = N.i;
}
void getData(){
cout<<&i<<endl;
}
};
int main(){
int x = 15;
Number N1(&x), N2 = N1;
N1.getData();
N2.getData();
return 0;
}
Comments
Leave a comment