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;
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;
}
}
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.
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.