Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-24-overloading

 ics212-24-overloading

Avatar for William Albritton

William Albritton

November 12, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Fundamentals of Operator Overloading  Assignment Operator

     Binary Operators  Stream Operators  Unary Operators  Pre & Post Increment
  2. What is it?  Operator overloading is a way to

    use operators for different data types  For example, “+” is used to add char, int, double, etc.  Function definition is written the same as any other function, except function name becomes “operator” followed by the symbol that you are overloading  For example, function operator+()
  3. Automatically Overloaded Operators  Three operators are overloaded automatically by

    the compiler, and explicitly overloaded by the programmer 1. Assignment operator (=), which is a memberwise assignment of the class data members 2. Address operator (&), which returns the address of the object in memory 3. The comma (,) operator, which evaluates the left expression, then evaluates & returns the right expression
  4. Restrictions  Only these existing 42 operators (and no more)

    can be overloaded  +- * / % ^ & | << >> < > = !  += -= *= /= %= ^= &= |= <<= >>= <= >= == !=  && || ~ ++ -- ->* -> , () []  new delete new[] delete[]
  5. Restrictions  These four existing operators cannot be overloaded 

    . .* :: ?:  These properties cannot be changed  Precedence: * always has higher precedence than +  Associativity: operators are applied either left-to-right, or right-to-left  Arity: number of operands that the operator takes (unary operators and binary operators)
  6. Restrictions  Overloading an assignment operator and an addition operator

     fraction1 = fraction2 + fraction3;  Will not automatically overload the += operator  fraction4 += fraction5;  You have to write separate function definitions for operator=(), operator+(), and operator+=()
  7. Restrictions  Do not misuse the overloaded operators  Such

    as overloading the “+” operator to subtract
  8. Assignment Operator (=)  Assignment operator (=) assigns one object

    to another  By default, the compiler creates an operator=() using memberwise copy, where the value in each data member of one object is copied to another object’s data members  Can cause problems with dynamically allocated data members (such as linked list, binary tree, etc.), as the data members will point to the same data structure
  9. Assignment Operator in main() Fraction fraction1; Fraction fraction2(3,4); fraction1 =

    fraction2; fraction1.print(); // 3/4 fraction2.print(); // 3/4
  10. Assignment Operator Function Definition const Fraction &operator=(const Fraction &fraction){ set(fraction.numerator,

    fraction.denominator); return *this; /* “*this” is the current object */ }
  11. Use of “const DataType &”  In the assignment operator

    function definition, this code means that you have a reference variable that cannot be changed  Used for the parameter and the return value, so that neither the parameter nor return value can be changed  See example code: assign.cpp const Fraction &
  12. Two Ways to Overload  C++ has two ways to

    overload an operator: 1. Class Member Function: overloading an operator by adding another function to the class 2. Global Friend Function: overloading an operator as a global function and making it a friend function, so it can still access the private data members of the class
  13. Class Member Function  The function must be a class

    member function when overloading (), [], -> or any assignment operator  Also, the left operand must be a class object
  14. Class Member Function Definition Fraction operator+(const Fraction &fraction){ int numerator3

    = numerator * fraction.denominator + denominator * fraction.numerator; int denominator3 = denominator * fraction.denominator; Fraction fraction3(numerator3, denominator3); fraction3.reduce(); return fraction3;}
  15. Class Member Function Call Fraction fraction1; Fraction fraction2(2,7); Fraction fraction3(3,7);

    /* converted to: fraction1.operator=(fraction2.operator+ (fraction3)) */ fraction1 = fraction2 + fraction3; fraction1.print(); // 5/7
  16. Class Member Function Call  Converts "5" to a fraction

    (using the conversion constructor) and adds it to fraction2  Next line will not compile, because class member functions can only work when left operand is an object of that class fraction1 = fraction2 + 5; fraction1 = 5 + fraction2;
  17. Conversion Constructor  Conversion constructors convert data from one data

    type into another data type  For example, we have a constructor that takes an integer as an argument  This can be used by the compiler to convert integers into Fractions Fraction three(3); //converts 3 to 3/1
  18. Conversion Constructor Definition  This conversion constructor takes one integer

    as a parameter, and uses the set() function to do error checking, and set the numerator and denominator Fraction(int num){ set(num, ONE); }
  19. Class Member Function operator+()  For an example program, see:

    member.cpp  Implements operator+() as a class member function  Next, we will see how to implement operator+() as a global friend function
  20. Global Friend Function  Overloading an operator as a global

    friend function is useful when the left or right operand is a class object  Must use this method when the left operand is NOT a class object  For example, the << operator for output cout<<fraction2;
  21. Global Friend Function Definition friend Fraction operator+(const Fraction &fraction1, const

    Fraction &fraction2){ int numerator3 = fraction1.numerator * fraction2.denominator+fraction1.denominator * fraction2.numerator; int denominator3 = fraction1.denominator * fraction2.denominator; Fraction fraction3(numerator3, denominator3); fraction3.reduce(); return fraction3;}
  22. Global Friend Function Calls Fraction fraction1; Fraction fraction2(2,7); Fraction fraction3(3,7);

    /* converted to: fraction1.operator=(operator+ (fraction2, fraction3)) */ fraction1 = fraction2 + fraction3; fraction1.print(); // 5/7
  23. Global Friend Function Calls  Converts "5" to a fraction

    (using the conversion constructor) and adds it to fraction2  Next line also compiles, because friend global functions work when either left operand or right operand (or both) is an object of that class fraction1 = 5 + fraction2; fraction1 = fraction2 + 5;
  24. Overloading Stream Operators  To overload the stream operators for

    input and output, we have to use global friend functions, because the left operand is not a Fraction
  25. Overloading Stream Operators  cin is an object, which is

    an instance (variable) of the istream class  The function used with cin is operator>>() cin>>fraction1>>fraction2; //is equilavent to: operator>>(cin,fraction1); operator>>(cin,fraction2);
  26. Function Definition for operator>>  Object “input” is equivalent to

    “cin” friend istream &operator>>(istream &input, Fraction &fraction){ char slash = ‘s’; //stores division sign input>>fraction.numerator>>slash >>fraction.denominator; return input; }
  27. Overloading Stream Operators  cout is an object, which is

    an instance (variable) of the ostream class  The function is operator<<() cout<<fraction1<<fraction2; becomes //is equivalent to: operator<<(cout,fraction1); operator<<(cout,fraction2);
  28. Function Definition for operator<<  Object “output” is equivalent to

    “cout” friend ostream &operator<<(ostream &output, const Fraction & fraction){ output<<fraction.numerator<<‘/’ <<fraction.denominator; return output; }
  29. Overloading Unary Operators  Unary operators (operators with one operand)

    can be overloaded as a class member function or as a global friend function  For example, the unary minus operator (-)  Will return the same fraction with a negative numerator
  30. Global Friend Function Definition friend Fraction operator-(const Fraction &fraction1){ Fraction

    temp(-fraction1.numerator, fraction1.denominator); return temp; }
  31. Overloading Preincrement  Function call for preincrement, which adds 1

    to the current value and returns the new value ++fraction1; //is equivalent to: fraction1.operator++();
  32. Overloading Preincrement  Preincrement returns a reference to itself, so

    this is the function prototype (1st line of a function) Fraction &operator++();
  33. Overloading Postincrement  Function call for postincrement, which adds 1

    to the current value and returns the previous value  To distinguish postincremnt from preincrement, the compiler has to add a “dummy value” of zero fraction1++; //is equivalent to: fraction1.operator++(0);
  34. Overloading Postincrement  Postincrement returns a value , not a

    reference, so this is the function prototype (1st line of a function) Fraction operator++(int);
  35. Memory Management  Fundamentals of Operator Overloading  Assignment Operator

     Binary Operators  Stream Operators  Unary Operators  Pre & Post Increment