C++ Notes By Vikas Sharma

  • Uploaded by: vikas_sharma12
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C++ Notes By Vikas Sharma as PDF for free.

More details

  • Words: 1,178
  • Pages: 18
Department of Computer and Information Science, School of Science, IUPUI

Operator Overloading Friend Functions & Special Forms

Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected]

Dale Roberts

1

Operator Overloading Using a Friend Function Number of Parameters Accepted by an Overloaded Friend Operator Function Depend Upon the Operator Type -- One for Unary Operators and Two for Binary Operators class complex{ int re, im; public: complex(int ip1 = 0, int ip2 = 0) :re(ip1), im(ip2){} friend complex operator+(complex, complex); }; //Friend Operator + Function complex operator+(complex a, complex b) {return complex(a.re+b.re, a.im+b.im);} main(){ complex one(1,1), two(2,2), three; three = one + two; //three = operator+(one, two) } Is a friend function necessary in this

case? No because LH operand is an instance of the class. Dale Roberts

2

Operator Functions as Class Members vs. as friend Functions

Non-member overloaded operator functions Enable the operator to be commutative

HugeInteger bigInteger; int integer; bigInteger = integer + bigInteger; or bigInteger = bigInteger + integer; Dale Roberts

3

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 re, im; complex(int ip1 = 0, int ip2 = 0) :re(ip1), im(ip2){} }; void operator!(complex a) { int temp = a.re; a.re = a.im; a.im = temp; cout << "Real: " << a.re << endl; cout << "Imaginary: " << a.im << endl; } main() { complex one(1,2); !one; }

Dale Roberts

4

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 the 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 }

Dale Roberts

5

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

Dale Roberts

6

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 } Dale Roberts

7

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;}

Dale Roberts

8

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 shown in the preceding code. A more straightforward way to implement this functionality is to overload the addition/assignment operator (+=).

Dale Roberts

9

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 Dale Roberts

10

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

Dale Roberts

11

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

// Fig. 18.3: fig18_03.cpp // Overloading the stream-insertion and // stream-extraction operators. #include using using using using using

std::cout; std::cin; std::endl; std::ostream; std::istream;

#include using std::setw; class PhoneNumber { friend ostream &operator<<( ostream&, const PhoneNumber & ); friend istream &operator>>( istream&, PhoneNumber & ); 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

// Overloaded stream-insertion operator (cannot be // a member function if we would like to invoke it with // cout << somePhoneNumber;). ostream &operator<<( ostream &output, const PhoneNumber &num ) {

Dale Roberts

12

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; }

Dale Roberts

13

Enter phone number in the form (123) 456-7890: (800) 555-1212 The phone number entered was: (800) 555-1212

Dale Roberts

14

Converting between Types Cast operator Convert objects into built-in types or other objects Conversion operator must be a non-static member function. Cannot be a friend function Do not specify return type For user-defined class A A::operator char *() const; A::operator int() const; A::operator otherClass() const;

// A to char //A to int //A to otherClass

When compiler sees (char *) s it calls s.operator char*()

Dale Roberts

15

Converting between Types (cont) The compiler can call these functions to create temporary objects. If s is not of type char * Calls A::operator char *() const; for cout << s;

Dale Roberts

16

Special overloading forms - Example Special Forms Example

Dale Roberts

17

Acknowledgements These slides were originally development by Dr. Uday Murthy and Dr. Rajeev Raje. Some contents comes from the Deitel slides that accompany your text. Some information regarding the postfix form the increment and decrement operators comes from MSDN.

Dale Roberts

18

Related Documents