#OOP_D_ITS - 5th - C++ Oop Operator Overloading


Published on

Published in: Health & Medicine, Technology

#OOP_D_ITS - 5th - C++ Oop Operator Overloading

  1. 1. C++ OOP :: Operator Overloading<br />06/10/2009<br />1<br />Hadziq Fabroyir - Informatics ITS<br />
  2. 2. Function Signatures<br />A function signature is what the compiler and linker use to identify a function.<br />In C , functions are identified only by their name<br />In C++ , a function’s signature includes its name, parameters, and (for member functions) const. It does NOT include the return type.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />2<br />
  3. 3. Ex: C++ swap( ) Function<br />We still need separate functions, but they can all have the same name.<br />For Examples:<br />void swap (int& a, int& b);<br />void swap (double& a, double& b);<br />void swap (struct bob& a, struct bob& b);<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />3<br />
  4. 4. Operator Overloading Overview<br />Many C++ operator are already overloaded for primitive types. Examples:<br />+ - * / &lt;&lt; &gt;&gt;<br />It is often convenient for our classes to imitate the operations available on primitive types (e.g., + or - ).<br />Then we can use the same concise notation for manipulating our objects.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />4<br />
  5. 5. Ex: Complex Number Class<br />class Complex {public:<br /> Complex (int real = 0, int imagine = 0);<br /> int getReal ( ) const;<br /> int getImagine ( ) const;<br /> void setReal (int n);<br /> void setImagine (int d);<br />private:<br /> int real;<br /> int imagine;<br />};<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />5<br />
  6. 6. Using Complex Class<br />It makes sense to want to perform mathematical operations with Complex objects.<br /> Complex C1 (3, 5), C2 (5, 9), C3;<br /> C3 = C1 + C2; // addition<br /> C2 = C3 * C1; // subtraction<br /> C1 = -C2; // negation<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />6<br />
  7. 7. Operators Are Really Functions<br />For user-defined types, when you use an operator, you are making a function call.<br />Consider the expression: C2 + C1<br />This is translated into a function call.<br />The name of the function is “operator+”<br />The call is:<br />C2.operator+(C1);<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />7<br />
  8. 8. Declaring operator+As a Member Function<br />class Complex {<br /> public:<br /> const Complex<br /> operator+ (const Complex &operand) const;<br /> …<br />};<br />Note all of the const’s!<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />8<br />
  9. 9. operator+ Implementation<br />const Complex <br />Complex :: operator+ (const Complex &operand) const <br />{<br /> Complex sum;<br />// accessor and mutators not required<br /> sum.imagine = imagine + operand.imagine;<br />// but preferred<br /> sum.setReal( getReal( ) + operand.getReal ( ) ); <br /> return sum;<br />}<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />9<br />
  10. 10. Using operator+<br />We can now write<br />C3 = C2 + C1;<br />We can also use cascading operators.<br />C4 = C3 + C2 + C1;<br />And we can write<br />C3 = C2 + 7;<br />But C3 = 7 + C2 is a compiler error. (Why?)<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />10<br />
  11. 11. operator+ As aNon-member, Non-friend<br />const Complex<br /> operator+ (const Complex &lhs, // extra parameter<br /> const Complex &rhs) // not const<br />{ // must use accessors and mutators<br /> Complex sum;<br /> sum.setImagine (lhs.getImagine( )<br /> + rhs.getImagine( ) );<br /> sum.setReal (lhs.getReal ( ) + rhs.getReal( ) );<br /> return sum;<br />} // is now commutative<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />11<br />
  12. 12. Printing Objects<br />Each object should be responsible for printing itself.<br />This guarantees objects are always printed the same way.<br />It allows us to write intuitive output code:<br /> Complex C5 (5, 3);cout &lt;&lt; C5 &lt;&lt; endl;<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />12<br />
  13. 13. Operator&lt;&lt;<br />The insertion operator &lt;&lt; is a function and can (and should) be overloaded. We can do operator&gt;&gt;, too.<br />&lt;&lt; is a binary operator.<br />The left-hand operand is of type ostream&<br />Therefore, operator&lt;&lt; cannot be a member function. It must be a non-member.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />13<br />
  14. 14. operator&lt;&lt;<br />ostream& <br />operator&lt;&lt; (ostream& out, const Complex& c) {<br /> out &lt;&lt; c.getReal( );<br /> int imagine = c.getImagine( );<br /> out &lt;&lt; (imagine &lt; 0 ? “ - ” : “ + ” ) <br /> out &lt;&lt; imagine &lt;&lt; “i”;<br /> return out;<br />}<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />14<br />
  15. 15. Operator&lt;&lt;Returns Type ‘ostream &’<br />Why? So we can write statements such as<br />cout &lt;&lt; C5 &lt;&lt; “is a complex number”<br />OR<br />cout &lt;&lt; C3 &lt;&lt; endl &lt;&lt; C2 &lt;&lt; endl;<br />&lt;&lt; associates from left to right.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />15<br />
  16. 16. Overloading Unary Operators<br /> Complex C1(4, 5), C2;<br /> C2 = -C1;<br />is an example of a unary operator (minus).<br />We can and should overload this operator as a member function.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />16<br />
  17. 17. Unary operator-<br />const Complex Complex :: operator- ( ) const<br />{<br /> Complex x;<br /> x.real = -real;<br /> x.imagine = imagine;<br /> return x;<br />}<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />17<br />
  18. 18. Overloading =<br />Remember that assignment performs a memberwise (shallow) copy by default.<br />This is not sufficient when a data member is dynamically allocated.<br />=must be overloaded to do a deep copy.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />18<br />
  19. 19. Restrictions<br />Most of operators can be overloaded.<br />You can’t make up your own operators.<br />You can’t overload operators for primitive types (like int).<br />You can’t change the precedence of an operator.<br />You can’t change the associativity of an operator.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />19<br />
  20. 20. Converting between Types<br />Cast operator<br />Convert objects into built-in types or other objects <br />Conversion operator must be a non-static member function.<br />Cannot be a friend function<br />Do not specify return type<br />For user-defined class A<br /> A::operator char *() const; // A to char<br /> A::operator int() const; //A to int<br /> A::operator otherClass() const; //A to otherClass<br /> When compiler sees (char *) s it calls <br /> s.operator char*()<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />20<br />
  21. 21. Good Programming Practices<br />Overload operators so that they mimic the behavior of primitive data types.<br />Overloaded binary arithmetic operators should<br />return const objects by value<br />be written as non-member functions when appropriate to allow commutativity<br />be written as non-friend functions (if data member accessors are available)<br />Overload unary operators as member functions.<br />Always overload &lt;&lt;<br />Always overload = for objects with dynamic data members.<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />21<br />
  22. 22. Another Example<br />Vectors in the Plane<br />Suppose we want to implement vectors in 2D and the operations involving them. <br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />22<br />
  23. 23. For your practice …<br />Exercise<br />Vector2D Class<br />Properties: double X, double Y<br />Method (Mutators): (try) all possible operators that can be applied on Vector 2D<br />(define methods of) the remains operation that can’t be overloaded<br />Lab Session for lower order (Senin, 15.00-17.00)<br />Lab Session for upper order (Senin, 19.00-21.00)<br />Please provide:<br /> the softcopy(send it by email – deadline Sunday 23:59)<br /> the hardcopy(bring it when attending the lab session)<br />06/10/2009<br />Hadziq Fabroyir - Informatics ITS<br />23<br />
  24. 24. ☺~ Next: C++ OOP :: Inheritance ~☺<br />[ 24 ]<br />Hadziq Fabroyir - Informatics ITS<br />06/10/2009<br />