9 opoverload

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 9 Meng Su 2/16/2011 1 Today’s Topics Overloading Operators 1. 1. 2. 2. 2. 3. External way (not as a class member) Internal way (as a class member) member) Friend functions List as ADT 2/16/2011 2 1. Overloading Operators Programmer Programmer can redefine the meaning of these operator such as: +, -, ... for using in our own classes. Two Two methods of overloading an operator: as as an external functions -- not a member of the class. as as an internal member function of the class. 2/16/2011 3 1 In In .h file: declare it, (outside of the class) ostream & operator<<(ostream & out, operator<<(ostream const Time & t); In .cpp In .cpp file: define it, ostream & operator<<(ostream & out, operator<<(ostream const Time & t) { t.display(out); return out; } t.display(out); In In main.cpp file: use it, Time bedTime; bedTime; …… cout << bedTime; bedTime; // if display is public, it is same as bedTime.display(cout); bedTime.display(cout); 2/16/2011 4 Syntax of Operator Overloading (external) Suppose Suppose the operator is ∆ ReturnType operator∆( type1 x, type2 y) { …… } After After the definition we can use a∆b is equivalent to call ope...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online