Opertaor Overloading
Opertaor Overloading
Opertaor Overloading
Introduction
Operator overloading
Enabling C++s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is misused, program difficult to understand Examples of already overloaded operators
Operator << is both the stream-insertion operator and the bitwise left-shift operator + and -, perform arithmetic on multiple types
Compiler generates the appropriate code based on the manner in which the operator is used
Operator Overloading
What?
an operator that has multiple meanings varies depending on use Why? Ease of use is a principle of OO How? by defining as an operator function
functions that can extend meaning of built-in operators (cannot define your own new operators) keyword operator is in definition, followed by the operator to be overloaded
Used
Introduction
Overloading an operator
Write function definition as normal Function name is keyword operator followed by the symbol for the operator being overloaded operator+ used to overload the addition operator (+)
Special cases
To use an operator on a class object it must be overloaded unless the assignment operator(=)or the address operator(&)
The assignment operator (=) may be used with every class without explicit overloading. The default behavior of the assignment operator is a memberwise assignment of the data members of the class. The address operator (&) may also be used with objects of any class without explicit overloading. It returns the address of the object in memory.
1 2 3 4 5 5
// Fig. 8.3: fig08_03.cpp // Overloading the stream-insertion and // stream-extraction operators. #include <iostream> #include <iomanip> 6 class PhoneNumber { 7 8 friend ostream &operator<<( ostream&, const PhoneNumber & ); friend istream &operator>>( istream&, PhoneNumber & );
9 10 11 12
private: char areaCode[ 4 ]; char exchange[ 4 ]; char line[ 5 ]; // 3-digit area code and null // 3-digit exchange and null // 4-digit line and null
13 }; // Overloaded stream-insertion operator (cannot be // a member function if we would like to invoke it with // cout << somePhoneNumber;). 14 ostream &operator<<( ostream &output, const PhoneNumber &num ) {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
output << "(" << num.areaCode << ") " << num.exchange << "-" << num.line; return output; // enables cout << a << b << c; } istream &operator>>( istream &input, PhoneNumber &num ) { input.ignore(); // skip ( input >> setw( 4 ) >> num.areaCode; // input area code input.ignore( 2 ); // skip ) and space input >> setw( 4 ) >> num.exchange; // input exchange input.ignore(); // skip dash (-) input >> setw( 5 ) >> num.line; // input line return input; // enables cin >> a >> b >> c; } int main() { PhoneNumber phone; // create object phone cout << "Enter phone number in the form (123) 456-7890:\n"; // cin >> phone invokes operator>> function by // issuing the call operator>>( cin, phone ). cin >> phone; // cout << phone invokes operator<< function by // issuing the call operator<<( cout, phone ). cout << "The phone number entered was: " << phone << endl; return 0; }
Enter phone number in the form (123) 456-7890: (800) 555-1212 The phone number entered was: (800) 555-1212
Program Output
Overloading Assignment
Overloading assignment operator solves problems with object assignment when object contains pointer to dynamic memory. Assignment operator is most naturally overloaded as an instance member function Needs to return a value of the assigned object to allow cascaded assignments such as
a = b = c;
Overloading Assignment
Assignment overloaded as a member function:
class CpClass { int *p; public: CpClass(int v=0) { p = new int; *p = v; ~CpClass(){delete p;} CpClass operator=(CpClass); };
Overloading Assignment
Implementation returns a value:
CpClass CpClass::operator=(CpClass r) { *p = *r.p; return *this; };
Overloaded [] Operator
Can be used to create classes that behave like arrays, providing bounds-checking on subscripts Overloaded [] returns a reference to object, not an object itself
y += z is equivalent to y.operator+=( z )
y += z is equivalent to operator+=( y, z )
Declares an overloaded cast operator function for creating a char * out of an A object
Declares an overloaded cast operator function for converting an object of A into an integer
A::operator otherClass() const;
Declares an overloaded cast operator function for converting an object of A into an object of otherClass
The compiler calls the overloaded cast operator function operator char * to convert the object into a char * and uses the resulting char * in the expression
convention adopted that when compiler sees postincrementing expression, it will generate the member-function call
d1.operator++( 0 ); // for d1++
0 is a dummy value to make the argument list of operator++ distinguishable from the argument list for ++operator