CSC 330 Object Oriented Programming Operator Overloading Friend Functions & Forms 1
Restrictions on Operator Overloading Most of C++ s operators can be overloaded. Operators that can be overloaded + - * / % ^ & ~! = < > += -= *= /= %= ^= &= = << >> >>= <<= ==!= <= >= && ++ -- ->*, -> [] () new delete new[] delete[] No new operators can be created Use only existing operators 2
Arithmetic Operators 3
Comparison Operators 4
Logical Operators 5
Bitwise Operator 6
Other Operators 7
Examples of Bitwise Operators 8
Restrictions on Operator Overloading cont d Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately 9
Overloading Operators The Rules Associativity refers to the order in which actions within an expression are carried out You cannot change associativity when you overload operators You also cannot change the normal precedence of any operator 10
Overloading Operators The Rules 11
A Complex Class #include <iostream> class Complex { double real; double image; public: Complex(double re=0, double im=0) { this->real = re; this->image = im; Complex add(const Complex& c) const { Complex result; result.real = this->real + c.real; result.image = this->image + c.image; return result;.... ; 12
Using the Complex Class int main() { Complex z(1,2), w(3,4); Complex a = z.add(w); a.display(); // want a = z+w (4,6) 13
Operator Functions operator keyword together with an operator operator+ operator- etc. Can overload all operators except: ::. *?: 14
Complex with operator+ class Complex { //... public: //... Complex operator+(const Complex& c) const { Complex result; result.real = this->real + c.real; result.image = this->image + c.image; return result; //... ; int main() { Complex z(1,2), w(3,4); Complex a = z + w; a.display(); // z.operator+(w) 15
Global Complex Operators class Complex { double real, image; public: ; Complex(double x = 0, double y = 0) { this->real = x; this->imag = y; double getreal() const {return real; double getimage() const {return image; //... Complex operator+(const Complex& c1, const Complex& c2) { double re = c1.getreal() + c2.getreal(); double im = c1.getimage() + c2.getimage(); Complex result(re, im); return result; 16
friend Functions class Complex { public: Complex( ); // default Complex( double ) ; // real given Complex( double, double ) ; // both given void write( ) const; // friend functions friend Complex operator+( const Complex&, const Complex& ) ; friend Complex operator-( ( const Complex&, const Complex& ) ; friend Complex operator*( const Complex&, const Complex& ) ; friend Complex operator/( const Complex&, const Complex& ) ; private: double real; double image; ; 17
Implementation File // Complex + as top-level friend Complex operator+( const Complex& t, const Complex& u ) { return Complex( t.real + u.real, t.image + u.image ) ; // Complex - as top-level friend Complex operator-( ( const Complex& t, const Complex& u ) { return Complex( t.real - u.real, t.image - u.image ) ; // Complex * as top-level friend Complex operator*( const Complex& t, const Complex& u ) { return Complex( t.real * u.real - t.imag * u.image, t.imag * u.real + t.real * u.image ) ; // Complex / as top-level friend Complex operator/( const Complex& t, const Complex& u ) { double abs_sq = u.real * u.real + u.image * u.image; return Complex( (t.real( * u.real + t.image * u.image)/abs_sq, (t.image * u.real - t-real * u.image)/abs_sq); ); 18
Discussions Friendship (i.e., using the friend keyword) is a complex and dangerous topic for various reasons: Friendship, when applied to program design, is an escape mechanism allowing us to circumvent the principles of encapsulation and data hiding. The use of friends should therefore be minimized to situations where they can be used naturally. If friends are used, realize that friend functions or classes become implementation dependent on the classes declaring them as friends. Once the internal organization of the data of a class declaring friends changes, all its friends s must be recompiled (and possibly modified) as well. Therefore, as a rule of thumb: don't use friend functions or classes. 19
Global Operator Overloading Similar to friend Function Overloading, Except the Keyword friend is Omitted and Global Functions CANNOT ACCESS private Members class Complex{ //All Public Members! public: int real, image; Complex(int ip1 = 0, int ip2 = 0) :real(ip1), image(ip2){; ; void operator!(complex a) { int temp = a.real; a.real = a.image; a.image = temp; cout << "Real: " << a.real << endl; cout << "Imaginary: " << a.image << endl; main() { Complex one(1,2);!one; 20
Overloading of Operators Having a Variable Arity Operators Such as - Can be Unary or Binary Overloading of Such Operators Involves Creating a Unary Function (One Operand) and a Binary Function (Two Operands) Only if Both Forms are Used, They Need to be Implemented class Number{ int n; public: Number(int x = 0):n(x){ Number operator-(){n = -n; return *this; Number operator-(number ip) {n = n ip.n; ; return *this; ; main(){ Number one(1), two(2), three; one = -one; //unary operator three = one - two; //three.n = -3 21
Operators with Prefix and Postfix Forms Separate Functions for Each -- Prefix and Postfix -- Forms are Needed Prefix Form is Treated as an Unary Operator Postfix Form is Treated as a Binary Operator 22
Prefix Overloaded Function -- Example class Number{ int n; public: Number(int x):n(x){; ){; //Constructor //prefix operator -- unary Number operator++(); ; Number Number::operator++(){ n++; return *this; main(){ Number one(10); //one.n = 10 ++one; //one.n = 11 23
Postfix Overloaded Function -- Example Postfix Operator is Implemented as a Binary Operator with an int Argument with a Default Value of 0. When specifying an overloaded operator for the postfix form of the increment or decrement operator, the additional argument must be of type int; specifying any other type generates an error. class Number{ int n; public: Number(int x):n(x){; ){; //Constructor //postfix operator -- binary -- int argument Number operator++(int); ; Number Number::operator++(int y) {if (y!= 0) n += y; else n++; return *this; 24
Postfix overloaded function Example (Cont) main() { Number one(10); // one.n = 10 one++; // one.n = 11 one.operator++(2); // one.n = 13 There is no syntax for using the increment or decrement operators to pass these values other than explicit invocation, as a shown in the preceding code. A more straightforward way to implement this functionality is to overload the addition/assignment operator (+=). 25
Special Overloading Forms A Few Operators Require Special Treatments During Overloading Conversion Operator const Array Operator Function Call -- Parenthesis Operator Stream Insertion -- << Operator Stream Extraction -- >> Operator Pointer to Member -- -> Operator Assignment Operator new Operator delete Operator 26
Overloading Stream-Insertion and Stream-Extraction Operators Overloaded << and >> operators Must have left operand of types ostream &, istream & respectively It must be a non-member function (left operand not an object of the class) It must be a friend function if it accesses private data members 27
1 // Fig. 18.3: fig18_03.cpp 2 // Overloading the stream-insertion and 3 // stream-extraction operators. 4 #include <iostream> 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 using std::ostream; 10 using std::istream; 11 12 #include <iomanip> 13 14 using std::setw; 15 16 class PhoneNumber { 17 friend ostream &operator<<( ostream&, const PhoneNumber & ); 18 friend istream &operator>>( istream&, PhoneNumber & ); 19 20 private: 21 char areacode[ 4 ]; // 3-digit area code and null 22 char exchange[ 4 ]; // 3-digit exchange and null 23 char line[ 5 ]; // 4-digit line and null 24 ; 25 26 // Overloaded stream-insertion operator (cannot be 27 // a member function if we would like to invoke it with 28 // cout << somephonenumber;). 29 ostream &operator<<( ostream &output, const PhoneNumber &num ) 30 { 28
31 output << "(" << num.areacode << ") " 32 << num.exchange << "-" << num.line; 33 return output; // enables cout << a << b << c; 34 35 36 istream &operator>>( istream &input, PhoneNumber &num ) 37 { 38 input.ignore(); // skip ( 39 input >> setw( 4 ) >> num.areacode; // input area code 40 input.ignore( 2 ); // skip ) and space 41 input >> setw( 4 ) >> num.exchange; // input exchange 42 input.ignore(); // skip dash (-) 43 input >> setw( 5 ) >> num.line; // input line 44 return input; // enables cin >> a >> b >> c; 45 46 47 int main() 48 { 49 PhoneNumber phone; // create object phone 50 51 cout << "Enter phone Number in the form (123) 456-7890:\n"; 52 53 // cin >> phone invokes operator>> function by 54 // issuing the call operator>>( cin, phone ). 55 cin >> phone; 56 57 // cout << phone invokes operator<< function by 58 // issuing the call operator<<( cout, phone ). 59 cout << "The phone Number entered was: " << phone << endl; 60 return 0; 61 29
Enter phone Number in the form (123) 456-7890: (800) 555-1212 The phone Number entered was: (800) 555-1212 30
Example: Student Class class Student { public: Student(); Student( const string & id, const string & lname ); bool operator < (const Student & S2 ); Student & operator =(const = Student & S2 ); bool operator ==( ( const Student & S2 ); friend ostream & operator <<(ostream & out, const Student & S); friend istream & operator >>(istream & inp,, Student & S); private: string m_id; string m_lastname; ; 31
Overloading < bool operator <( const Student & S2 ) { return m_id < S2.m_ID; 32
Overloading = Student & operator =( const Student & S2 ) { // if assigning object to itself... if( *this == S2 ) return *this; m_id = S2.m_ID; m_lastname = S2.m_LastName; return *this; 33