Sorry, that isn't what I was looking for. Please post CellularPhone.h and CellularPhoneStock.h
Looking at the code, it seems that CellularPhoneStock is something like:
1 2 3 4 5
class CellularPhoneStock {
public:
CellularPhone **phonez;
unsigned nrOfPhones;
};
It looks like phonez points to an array of CellularPhone*. But I don't see you allocate that array anywhere. Also I don't see you deleting the memory that gets allocated in addPhone.
If the assignment allows it, I suggest that you use a vector of CellularPhones instead. That way all the memory management will be handled for you:
1 2 3 4
class CellularPhoneStock {
public:
std::vector<CellularPhone> phonez;
};
Okay, so you have to do the memory management yourself. To do this you need to clearly define who "owns" the memory that you allocate. This usually boils down to who will delete it. In your case it's pretty clear that phonez owns the CellularPhones that it points to. That means the destructor has to delete them. I think you have that, but it looks like you deleted the post that had the code.
Now what about the copy constructor? Since phonez owns the items it points to, you have to make new copies of the source's CellularPhones. In other words, the copy constructor should go through origObj.phonez, make a copy of each CellularPhone in it, and add the copy to the this->phonez. Pay attention to exactly how many items in origObj.phonez you copy, and set this->nrOfPhones accordingly.
Another question, since you make it seem so easy lol, the operator <, I have to overload it.
This one:
booloperator<(CellularPhone &otherObj);
Now, in the header above, its placed in the wrong class, but ive fixed it.
So what I have to overload the operator for is because I have to check wich phone has the most nrOfType (numbers of x phone), and to do so, my teacher said I have to add up the amount of nrOfPhones before returning true (I dont understand what he meant, but this is what I wrote)
:
1 2 3 4 5 6 7 8 9
for (int i = 0; i < this->nrOfPhones; i++){
if (this->nrOfType()>sec.this->nrOfType()){
returntrue;
}
else{
returnfalse;
}
}
}
But in the overloaded == operator, I told it to grab the content and compare it, I thought sending the adress was the correct way of doing so, but then, now when I think about it, the adress of the pointer will be sent ?
Anyways, I did as u said, and, it adds the phones, BUT, it still add duplicates :(
Your operator==() takes a reference to a CellularPhone as the parameter, so coder77 is right, you need to compare the values as he's shown.
But your loop to find a duplicate isn't right. Why are you changing nrOfPhones in there? You should do this intstead:
- Create the new CellularPhone and assign it to a temp pointer
- Go through the existing phones to see if this is a duplicate. If so then
delete the newly created CellularPhone and return.
- If you don't find a duplicate then add the new CellularPhone to the list and ONLY THEN increment nrOfPhones.
This way nrOfPhones will actually reflect the number of valid items all the time.
Now let's talk about operator==(). You've coded it to compare the color and model, but operator<() ignores the color and model and compares the nrOfType right? So consider two phones:
CellularPhone a: color=black, model=Razor, nrOfPhone = 12
CellularPhone b: color=black, model=Razor, nrOfPhone = 13
CellularPhone c: color=blue, model=iPhone, mrOfPhone = 12;
With your < and == operators:
a == b, and a < b
a != c but a<c is false, and so is c<a, which implies that they are equal
You need to ask yourself how the phones are ordered and what makes two phones equal. Then code operator< and operator== to be consistent.