Copy Constructor and overloading operations

If anyone can help that would be incredible. I've been lost with this for quite some time now and I just dont get it. I don't understand how to write a copy constructor or what its purpose is. My professor gave me a code and I have to complete it but since I have no idea what he is talking about I don't even know where to start with this thing. I completed the functions...I think, but if someone could help me write this copy constructor or even clarify what my professor wants me to do with this code that would be great. I need to learn how to write this thing but all the examples seem so confusing and I don't understand its purpose. He asks "overload the assignment operator with deep copy for the class SalesRecord." I don't know how to create a deep copy or what to do. So if anyone can help and at least point me in the right direction that would be appreciated. Thanks for any help anyone can give me.

SalesRecord.h (I know I need an overload operator function for the + I think but I'm not sure what he's wanting me to with this.)

class SalesRecord
{
private:
float *sales_amount;
int number_of_sales;

public:
SalesRecord(int howmanysales);
SalesRecord(SalesRecord& othersales);
~SalesRecord();
void StoreSale(float newsales, int index);
float FindSale(int index);

};

Main.cpp

#include<iostream>
#include "salesrecord.h"
using namespace std;
int main( )
{
SalesRecord s1(4), s2(3);
s1.StoreSale(2.4, 0); s1.StoreSale(8.7, 1);
s1.StoreSale(6.3, 2); s1.StoreSale(3.5, 3);
s2=s1;
s1.StoreSale(3.5, 0);
cout << s2.FindSale(0)<<endl;
cout << s2.FindSale(1)<<endl;
cout << s2.FindSale(2)<<endl;
cout << s2.FindSale(3)<<endl;
}

SalesRecord.cpp (I wrote this all but I have no idea how to make a deep copy. I know he wants me to overload the + sign but I don't get what he's asking me to do. Hus instructions are always so vague.)

#include<iostream>
#include "salesrecord.h"
using namespace std;

SalesRecord::SalesRecord(int howmanysales)
{
int i;
if (howmanysales<1)
{
cerr << "invalid size: ";
exit(1);
}
sales_amount=new float[howmanysales];
int number_of_sales=howmanysales;

for (i=0; i<number_of_sales; i++)
{
sales_amount[i]=0;
}
}


SalesRecord::~SalesRecord()
{
delete[] sales_amount;
}


void SalesRecord::StoreSale(float newsales, int index)
{
sales_amount[index]=newsales;
}

float SalesRecord::FindSale(int index)
{
return sales_amount[index];
}

I wrote all this out but I've now hit a wall. I just need to complete this thing and finally understand what the hell a deep copy is.
A deep copy initializes a new object with the data of another. So, if you have already been working on something you previously declared like this:

SalesRecord record1;

Then you could use your copy constructor to create another object based on the data members of record1:

SalesRecord record2 = record1;

Coding a basic example of a copy constructor for your case:

1
2
3
4
SalesRecord::SalesRecord(const SalesRecord &source) {
     sales_amount = source.sales_amount;
     number_of_sales = source.number_of_sales;
}


And unless I am missing something, I don't see where you would need to overload +. I do however, see where you would need to do so with the = operator. My above examples invoke a copy constructor because of new object creation. However,

s2 = s1

in your code would invoke an overloaded =, which is not defined. Maybe you can enlighten me.
Last edited on
> and finally understand what the hell a deep copy is.

See: http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

With deep-copy and deep-assignment, your class would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class SalesRecord
{
    private:
        float *sales_amount;
        int number_of_sales;

    public:

        // ...

        // copy constructor - makes a deep copy
        // SalesRecord(SalesRecord& othersales); // make const-correct
        SalesRecord( const SalesRecord& othersales ) ;

        // copy assignment - makes a deep assignment
        SalesRecord& operator= ( const SalesRecord& othersales ) ;

        // ...
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// ...

SalesRecord::SalesRecord( const SalesRecord& othersales )
{
    number_of_sales = othersales.number_of_sales ; // copy number

    // deep copy array
    sales_amount = new float[number_of_sales] ;
    for( int i=0 ; i<number_of_sales ; ++i )
         sales_amount[i] = othersales.sales_amount[i] ;
}

SalesRecord& SalesRecord::operator= ( const SalesRecord& othersales )
{
    // if not trivial self-assignment
    // See: http://www.parashift.com/c++-faq-lite/assignment-operators.html
    if( sales_amount != othersales.sales_amount )
    {
        // do what the destructor does
        delete [] sales_amount ;

        // and then do what the copy constructor does
        // deep copy array
        sales_amount = new float[number_of_sales] ;
        for( int i=0 ; i<number_of_sales ; ++i )
             sales_amount[i] = othersales.sales_amount[i] ;
    }
    return *this ;
}

// ... 
Thanks for the help and clearing up some of the cobwebs. And i did mean an = not a + operator. That's what happens when I try to do two things at once. It makes a lot more sense when I can see it happening in front of me so it makes a bit more sense to me.
Topic archived. No new replies allowed.