Operator Overloading & Friends

Operator Overloading & Friends - Lecture 6:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 6: Operator Overloading PIC 10B Todd Wittman Sec 14.1 : Operator Overloading s Note that unless we tell the compiler how to do arithmetic on classes we create, it won't know how to do it. MyClass A; MyClass B; cout << A+B; ???????? s Sometimes it's more convenient to redefine a symbol for a class rather than create a function that does the same thing. C = A +B; vs. C = add(A,B); s C++ allows us to overload many different symbols, including: + - * / % ^ >> << = == < > <= >= += ++ ( ) [ ] s (The full list of overloadable symbols is on p. 605.)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Operator Overloading Guidelines s Suppose we are overloading the symbol # for some class MyClass: left # right s We have 2 choices: we could make the operator# function a member or non-member function. s If left is of type MyClass, we can make it a member function: returnType MyClass::operator# (rightType right) { ... s Note left is the implicit object in this case. s If left is not of type MyClass, make it a non-member function: returnType operator# (leftType left, rightType right) { ... s If the return value will overwrite the value of left , then make it send back the reference: ... The Product Class class Product { public: Product(); void read(); bool is_better_than(Product b) const; void print() const; private: string name; double price; int score; }; s For starters, let's define addition (+) for the Product class to join two products together, like we're selling them as a package. s Define new score to be the max of the two scores. ("Card",0.99,3) + ("Gum",2.00,7) = ("Card and Gum", 2.99, 7)
Background image of page 2
3 Product Addition: Member + s To write the operator+ as a member function, add this declaration to the Product public section: Product operator+ (const Product& right); s Then write the definition of the function in appropriate file: Product sum; sum.name = name + " and " + right.name; //Add strings. sum.price = price + right.price; //Add doubles. if (score > right.score) sum.score = score; //Find max score else sum.score = right.score; return sum; } Note the left Product is implicit. We access its values directly with name, price, score. Product Addition: Member += s Now let's define +=. s Note that A+=B will change the value of A. Product Product::operator += Product sum; sum.name = name+" and "+right.name; //Add strings. sum.price = price + right.price;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Operator Overloading &amp;amp; Friends - Lecture 6:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online