Answer to Question #180718 in C++ for Venkat

Question #180718

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.


1
Expert's answer
2021-04-12T14:28:28-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS