-1

I have two pointer variables as data members. How do I make copies of these two data members? How do I make a deep copy constructor for this? I tried many times, but it's not working.

class abc {
    float *number1;
    float *number2;

public:
    abc(float l,float w)
    {
       number1 = new float;
       number2 = new float;
       number1 = &l;
       number2 = &w;
    }
    
    float get_1()
    {
        return *number1;
    }
    
    float get_2()
    {
        return *number2;    
    }
    
    abc(const abc& ob)
    {
        number1 = ob.number1;
        number2 = ob.number2;
    }
    
    void display()
    {
        cout << "Number 1 : " << *number1 << endl;
        cout << "Number 2 : " << *number2 << endl;
    }
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
john
  • 79
  • 7

3 Answers3

2

Also your constructor is wrong:

abc(float l,float w)
{
   number1= new float;
   number2=new float;
   number1 =&l; //this will overwrite the pointer returned by new with the address of a local variable
   number2=&w; //the same
}

you should do this:

abc(float l,float w)
{
   number1= new float(l); // allocate a float and set the value to l
   number2=new float(w); // allocate a float and set the value to w
}

And the copy constructor is quite similar:

abc(const abc& ob)
{
    number1 = new float(*ob.number1); // allocate a float and set the value to number1 from the other object
    number2 = new float(*ob.number2); // allocate a float and set the value to number2 from the other object
}

This example can be improved further by using the initializer list of the constructors:

abc(float l,float w) :
        number1 { new float(l)},
        number2 { new float(w)}
{
}

abc(const abc& ob) :
        number1 { new float(*ob.number1)},
        number2 { new float(*ob.number2)}
{
}
mch
  • 9,424
  • 2
  • 28
  • 42
  • 2
    You mean `new float(*ob.number1)` instead of `new float(ob.number1)`? (also for `number2`) – MikeCAT Apr 19 '21 at 14:41
  • @john If you are going to give feedback on answers, you'd better be more precise. "not working" doesn't provide any insight into what it is that's not working. – Ted Lyngmo Apr 19 '21 at 14:45
2

For starters this constructor

abc(float l,float w)
{
   number1 = new float;
   number2 = new float;
   number1 = &l;
   number2 = &w;
}

produces memory leaks. At first a memory was allocated in these statements

   number1 = new float;
   number2 = new float;

and its addresses were assigned to pointers. But then at once the pointers were reassigned

   number1 = &l;
   number2 = &w;

So the addresses to the allocated memory were lost.

The constructor can look at least the following way

abc( float l, float w ) : number1( new float( l ) ), number2( new float( w ) )
{
}

These functions

float get_1()
{
    return *number1;
}

float get_2()
{
    return *number2;    
}

should be declared like

float get_1() const
{
    return *number1;
}

float get_2() const
{
    return *number2;    
}

In general it is better to define these functions like

const float & get_1() const
{
    return *number1;
}

const float & get_2() const
{
    return *number2;    
}

float & get_1()
{
    return *number1;
}

float & get_2()
{
    return *number2;    
}

The copy constructor can look like

abc( const abc &ob) 
    : number1( new float( *ob.number1 ) ), number2( new float( *ob.number2 ) )
{
}

The function display should look at least like

void display() const 
{
    cout << "Number 1 : " << *number1 << endl;
    cout << "Number 2 : " << *number2 << endl;
}

Instead of this function you could define a friend operator <<. For example

class abc
{
    //...
    friend std::ostream & operator <<( std::ostream &os, const abc &ob )
    {
        os << "Number 1 : " << *ob.number1 << std::endl;
        os << "Number 2 : " << *ob.number2 << std::endl;

        return os;
    }
}

Also you should define explicitly the copy assignment operator and the destructor.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
void display()
{
    cout<<"Number 1 : "<<number1<<endl;
    cout<<"Number 2 :"<<number2<endl;
}

This will print the location in memory of the numbers. Is that what you want? To display the value of the numbers

void display()
{
    cout<<"Number 1 : "<< *number1 << endl;
    cout<<"Number 2 : "<< *number2 << endl;
}
ravenspoint
  • 19,093
  • 6
  • 57
  • 103