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+()
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
. .* :: ?: 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)
fraction1 = fraction2 + fraction3; Will not automatically overload the += operator fraction4 += fraction5; You have to write separate function definitions for operator=(), operator+(), and operator+=()
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
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 &
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
(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;
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
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;
(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;
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);
an instance (variable) of the ostream class The function is operator<<() cout<<fraction1<<fraction2; becomes //is equivalent to: operator<<(cout,fraction1); operator<<(cout,fraction2);
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
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);