sale_item sale_item::operator + (sale_item &s2){
}

sale item is a class how to overload this operator


sale_item::sale_item(const char N[],const char C[],double P){
}

this is the constructor

well it really depends on how you want 2 sale items to be added together. the function declaration you have looks good though. btw please use code tags.

well it really depends on how you want 2 sale items to be added together. the function declaration you have looks good though. btw please use code tags.

i meant can u solve the overloadin + constructor

give me 1 way

Similar to what Nathan said, it depends on your definition of the "Sales_Item" class(i.e. what are the private and public member variables)

In order to reference the calling object's member variables, use : this->var_name The "this" pointer points to the instance of the object that is on the left hand side of the '+' operator : instanceA + instanceB ..... and : instanceB is the argument to the '+' operator.

Also, you should make both the return type and function parameter constant by using the const modifier so the object returned can't be modified so easily, or the argument can't be changed inside the '+' function.

Just one more thing : pass the argument to '+' by reference. Its more efficient.

excelent post harry010. tarekwehbe another note for you is that constructor is taking in 2 char arrays and one int. i would the char arrays are the name of the item and something else and that the int is the price of the item so adding to sale_items objects really doesn't make sense unless you just want to add the prices of the objects. if that is the case then i would suggest having an array of sale_item objects of what items the user has selected and and int for the total price of all those objects.

This article has been dead for over six months. Start a new discussion instead.