I'm working on a program that is basically suppose to list IceCreams with their Ingredients, name, price and discount, to the point in the code given I'm trying to take 2 different Ice Cream names and if say the user entered Caramel Apple Delight and chocolate I need it to give me "Caramel Apple Delight + chocolate", and since I'm new to how operators works and this needs to be done with an operator I've been stuck for hours on this, I've run into a similar problem and I don't know how I can do this, I've considered to concat the strings but it doesn't make any sense when it comes to the printing part.
IceCream &operator +(const IceCream &i){
IceCream temp;
temp.name = strncpy(this->name) + strncat(i.name);
if(*this>temp){
delete [] temp.name;
temp.name=new char [strlen(this->name)+1];
strncpy(temp.name,this->name,strlen(this->name)+1);
}
return temp;
}
This is the rest of the code and I am aware I have some issues with it but this is what I need help to solve since I'm having trouble understanding how all the operators work.
class IceCream{
private:
char *name;
char ingr[100];
float price;
int discount;
public:
IceCream(){name=new char [0];}
IceCream(char *name=" ", char* ingr=" ", float price=0.0, int discount=0){
this->name=new char[strlen(name)+1];
strcpy(this->name, name);
strcpy(this->ingr,ingr);
this->price=price;
this->discount=discount;
}
IceCream(const IceCream &i){
this->name=new char[strlen(i.name)+1];
strcpy(this->name,i.name);
strcpy(this->ingr,i.ingr);
this->price=i.price;
this->discount=i.discount;
}
~IceCream(){delete [] this->name;}
friend ostream& operator<<(ostream& out, IceCream &i){
if(i.discount>0){
out<<i.name<<": "<<i.ingr<<" "<<i.ingr" "<<"("i.discount")"<<endl;
}
else{
out<<i.name<<": "<<i.ingr<<" "<<i.price" "<<endl;
}
return out;
}
IceCream &operator ++(int){
discount+=5;
return *this;
}
IceCream &operator +(const IceCream &i){
IceCream temp;
temp.name = strncpy(this->name) + strncat(i.name);
if(*this>temp){
delete [] temp.name;
temp.ime=new char [strlen(this->name)+1];
strncpy(temp.name,this->name,strlen(this->name)+1);
}
return temp;
}