1 / 12

Operator Overloading

Operator Overloading. Outline. Revision What is Operator Overloading Why Operator Overloading Overloading Unary operators Overloading Binary operators Overloading Assignment operator. Revision. Operator Overloading is a mechanism to redefine built-in operations for user defined types

scottpwebb
Download Presentation

Operator Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operator Overloading

  2. Outline • Revision • What is Operator Overloading • Why Operator Overloading • Overloading Unary operators • Overloading Binary operators • Overloading Assignment operator

  3. Revision • Operator Overloading is a mechanism to redefine built-in operations for user defined types • It allows to give normal C++ operators such as +,-,==,< additional meanings • Operator Overloading should be used to perform the same function or similar function on class objects as the built-in behavior

  4. Revision • Operator overloading is a very neat feature of object oriented programming in C++ • Each individual operator must be overloaded for use with user defined types • Overloading the assignment operator and the subtraction operator does not overload the -= operator.

  5. Things to keep in mind • At least one of the operands in any overloaded operator must be a user-defined type • operator + for one integer and one double, NOT possible • Can only overload the operators that exist. • All operators keep their current precedence and associativity, regardless of what they're used for

  6. Operator Functions • Operator functions may be defined as either member functions or as non-member functions. • Non-member functions are usually made friends for performance reasons. • Member functions usually use the this pointer implicitly.

  7. Commonly Overloaded Operators • Unary • ++, --, (increment, decrement) • Binary • +, -, *, /, % (arithmetic) • =, +=, -=, *=, /=, %= (assignment) • ==, !=, >, <, >=, <= (relational) • <<, >> (I/O) • ||, && (logical) • &, |, ^, ^=, &=, |= (bitwise)

  8. Non-overloadable Operators • . (dot operator / member selector) • :: (scope resolution operator) • ?: (conditional operator / arithematic if) • .* (member pointer selector) • sizeof

  9. Overloading Unary Operator • Increment Operators ( ++) • Unary Operators ( --)

  10. Example • #include <iostream> • using namespace std; • //////////////////////////////////////////////////////////////// • class Counter • { • private: • unsigned int count; //count • public: • Counter() : count(0) //constructor • { } • unsigned int get_count() //return count • { return count; } • void operator ++ () //increment (prefix) • { • ++count; • } • };

  11. int main() • { • Counter c1, c2; //define and initialize • cout << “\nc1=” << c1.get_count(); //display • cout << “\nc2=” << c2.get_count(); • ++c1; //increment c1 • ++c2; //increment c2 • ++c2; //increment c2 • cout << “\nc1=” << c1.get_count(); //display again • cout << “\nc2=” << c2.get_count() << endl; • return 0; • }

  12. Overloading Binary Operators • Arithmetic Operators • Assignment Operators • Comparison Operators • Arithmetic Assignment Operators

More Related