8 Overloading Operators Main Idea Use the same

  • Slides: 39
Download presentation
8. Overloading Operators Main Idea: Use the same function name (function overload) or operator

8. Overloading Operators Main Idea: Use the same function name (function overload) or operator symbol (operator overload) to perform different computations, initializations or other actions. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 1

Overload: Examples Example 2: Overloading the class constructor Assume a complex class type has

Overload: Examples Example 2: Overloading the class constructor Assume a complex class type has been defined class Complex {…}; We want to be able to initialize a class object in different ways, e. g. by giving values to its data members or by initializing it to some object of the same class Solution: overload the class constructor, so that argument values of the type of the corresponding data members Complex first(re, im); and Complex second(first); can be used. 11/17/2008 single argument of type Complex, the same type as the object being initialized MET CS 563 - Fall 2008 8. Overloading Functions and Operators 2

Overload: Examples (continued) Example 3: We want to manipulate user defined objects, such as

Overload: Examples (continued) Example 3: We want to manipulate user defined objects, such as Date class objects or aggregate types such as strings the same way as we manipulate integers or floats, e. g. a) Overloading the '<' operator for Date today < tomorrow should produce true b) the '+' and '==' operator for strings "Welcome " + " home !" should produce "my. Password" == "my. Passwd" should produce 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators "Welcome home !" false 3

Overloading Operators Goal: Manipulate aggregate data types, such as arrays, structures, classes in the

Overloading Operators Goal: Manipulate aggregate data types, such as arrays, structures, classes in the same manner as built-in types, i. e. with infix operators that perform user defined operations on the objects. Main Idea: A binary operator is a function with two arguments, e. g. the expression 3+5 that evaluates to 8 is the same as the function plus() plus(3, 5) returning the value 8. Thus operator overload is in essence function overload. In order to implement it we need to write the appropriate definitions and to agree on a way to unambiguously relate these definitions to the operator symbol. Example: operator + {…}, keyword operator - {…} function body overloaded operator Syntax: 11/17/2008 operator <Operator. Symbol> {…} MET CS 563 - Fall 2008 8. Overloading Functions and Operators 4

Implementing Operator Overload: Member Function or friend Function ? A fundamental question of implementing

Implementing Operator Overload: Member Function or friend Function ? A fundamental question of implementing operator overload is: Can we always implement it as a member function of the class or do we need another mechanism as well? We will explore this question by overloading the '+' operator for a user defined class type Rational for rational numbers. The class Rational represents the rational numbers, i. e. all numbers that can be represented as p/ q where p and q are integers, e. g 2/3, 1/4, etc. The class includes the arithmetic and comparison operators and prints the rational number in quotient form by keeping the nominator and denominator as small as possible (as shown in the above examples). 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 5

Class Rational Interface / Behavior Initialize itself to 0 Perform addition '+' with integers

Class Rational Interface / Behavior Initialize itself to 0 Perform addition '+' with integers and rationals //other arithmetic operations… Perform comparison '<' with integers and rationals //other comparison operations… Tell its value in reduced quotient form Attributes / Data and Internals nominator denominator reducing the nominator and denominator to smallest value 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 6

(i) First Try: Overloaded '+' as a Member Function of class Rational{ public: Rational(int

(i) First Try: Overloaded '+' as a Member Function of class Rational{ public: Rational(int n=0, int d=1); //constructor initializing to 0 (0/1) //overloaded '+' prototype //input: single argument of type Rational, //returns value of (input + object) Rational operator+(const Rational& ); … private: int nom; //nominator int denom; //denominator … //functions for reducing nom and denom to smallest values }; 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 7

Class Rational - Constructors and Use of Overloaded '+' Constructor Definition: Rational : :

Class Rational - Constructors and Use of Overloaded '+' Constructor Definition: Rational : : Rational (int n, int d): nom(n), denom(d) {} Object Definitions: Rational t(5, 4), s(3), u, v; Use of Overloaded Operator: function name according to syntax Function Style (acc. Signature): t. operator+(s); Operator Style (Shorthand): 11/17/2008 t + s; MET CS 563 - Fall 2008 8. Overloading Functions and 8

Class Rational - Definition of Overloaded '+' as Member Function Rational : : operator+(const

Class Rational - Definition of Overloaded '+' as Member Function Rational : : operator+(const Rational &b ) { Rational sum; sum. nom = b. denom * nom + b. nom * denom; sum. denom = denom * b. denom; sum. reduce(); return sum; } Similar definitions can be written for the remaining binary arithmetic operators such as -, *, /, etc. Use: t. operator+(s); t + s; t + 5; //works as 5 becomes the initial value for argument b 5 + t; // illegal!!! 5 is a literal and 5. operator is not defined!! Problem: t+5 5+t !! when using our new + operator The addition is not commutative contrary to everything we have been taught since elementary school. Not a good idea. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 9

Class Rational - Declaring Overloaded '+' as friend Function within the class Rational {

Class Rational - Declaring Overloaded '+' as friend Function within the class Rational { public: Rational(int n=0, int d=1); //constructor argument 1 argument 2 keyword friend Rational operator+( const Rational&, const Rational&); //overloaded '+' prototype as friend function //input two arguments of type Rational, //returns sum of two arguments … //Similar declarations can be written for the remaining binary //arithmetic operators such as -, *, /, etc. … private: //same as in previous definition int nom; //nominator int denom; //denominator //functions for reducing nom and denom to smallest values }; 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 10

Defining the Overloaded '+' friend Function Outside the Class Rational operator+( const Rational &a,

Defining the Overloaded '+' friend Function Outside the Class Rational operator+( const Rational &a, const Rational &b ) Note: The definition has no { indication that Rational sum; there is any relation to some sum. nom = b. denom * a. nom + b. nom * a. denom; class(es). It is a sum. denom = a. denom * b. denom; regular function sum. reduce(); definition and only return sum; the class can give } it special meaning by declaring it a Similar definitions can be written for the remaining binary friend and giving arithmetic operators such as -, *, /, etc. access to its private data members. Use: operator+(t, s); t + s; t + 5; //works as 5 becomes the initial value for argument b 5 + t; // works as well!!! 5 is a literal for argument a!! 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 11

(iii) Overloaded Computer Specific Operators Return Objects Not Just Values Important Shared Characteristics: I/O

(iii) Overloaded Computer Specific Operators Return Objects Not Just Values Important Shared Characteristics: I/O as well as assignment operators should return objects, not just values to allow compound statements, such as Object of output stream class ostream I/O cout << "The sum is " << a+b; Assignment t += t +s; returns object t modified cout of type ostream resulting from insertion of a+b in previously modified cout 11/17/2008 += a; s = s +a; returns object s modified cout of type ostream resulting from insertion of "The sum is " in cout left-to-right associativity s right -to- left associativity MET CS 563 - Fall 2008 8. Overloading Functions and 12

Overloaded Computer Specific Operators: I/O Friends and Assignment Members Important Difference: I/O cout <<

Overloaded Computer Specific Operators: I/O Friends and Assignment Members Important Difference: I/O cout << a+b; Assignment t += s; lhs and rhs are of different types lhs and rhs are of the same type Input / Output Operators are implemented as friend functions: Assignment Operators are implemented as member functions: 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 13

Another look at class Rational { public: Rational (int = 0, int = 1

Another look at class Rational { public: Rational (int = 0, int = 1 ); // default constructor //Arithmetic operators as friend functions friend Rational operator+( const Rational&, const Rational& ); //Similarly the remaining arithmetic operators //Comparison operators as friend functions friend bool operator>( const Rational&, const Rational& ); //Similarly the remaining comparison operators //Output operator as friend function friend ostream& operator<<(ostream& , const Rational& ); //Similarly the input operator //Assignment operators as member functions Rational& operator+=(const Rational& ); //Similarly the remaining assignment operators private: int nom; int denom; int gcd(int , int ); //input: two integers, returns greatest common devisor void reduce( void ); //reduces nom and denom to smallest value}; 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 14

Class Rational - Definition and Use of Overloaded Output Reference as it will change

Class Rational - Definition and Use of Overloaded Output Reference as it will change after Prototype in class definition: insertion of expressions to be output friend ostream& operator<<(ostream& , const Rational& ); Definition: Reference: returns class object, not just object value; thus allowing compound operations ostream& operator<<(ostream& out, const Rational& s){ out << s. nom << '/' << s. denom ; return out; } overloaded stream insertion Use: operator Rational d( 3, 9 ); cout << "d = " << argument 1: out 11/17/2008 d << endl; argument 2: s d = 1/3 MET CS 563 - Fall 2008 8. Overloading Functions and 15

Class Rational - Definition and Use of Overloaded Assignment Prototype in class definition: Rational&

Class Rational - Definition and Use of Overloaded Assignment Prototype in class definition: Rational& operator+=(const Rational& ); Reference: returns class object, not just object value; thus Definition: allowing compound operations Rational& Rational : : operator+=(const Rational& r){ nom = nom * r. denom + denom * r. nom; denom *= r. denom; reduce(); Refers to object itself, i. e. object that received message and was modified. return *this; } Use: Rational r(3, 2); cout<< "r = " << r << endl; r = 3/2 cout<< r << "+ 2 ="; 3/2+ 2 =7/2 r += 2; cout<< r << endl; 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 16

The this Pointer Gives Access to Object's Own Address Goal: Provide object with the

The this Pointer Gives Access to Object's Own Address Goal: Provide object with the ability to refer to itself As shown in the overloaded addition assignment this can be quite useful. Solution: Each object is provided with a pointer this that contains the object's address. Note: • The type of the this pointer is given by the type of the corresponding object. • The this pointer is not part of the object itself, i. e. it is not reflected in the result of the sizeof operation that returns the size of an object. It is passed into object as an implicit first argument with every non-static message (call) to the object. • The this pointer is used implicitly or explicitly to reference data members and methods of the class 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and 17

Operators that Cannot Be Overloaded Most operators in C++ can be overloaded. However, the

Operators that Cannot Be Overloaded Most operators in C++ can be overloaded. However, the following operators cannot be overloaded . . * 11/17/2008 : : ? : sizeof MET CS 563 - Fall 2008 8. Overloading Functions and 18

7. 3. Class String - (i) C - strings - String literals (constants): sequence

7. 3. Class String - (i) C - strings - String literals (constants): sequence of characters enclosed in double quotes, e. g. "I am a string" The internal representation is an array of characters terminated by the NULL character ''. Thus the length of the array is always the string length + 1 I a m a s t r i n g - Character arrays - can have variable contents char s[] = "error"; e r r o r or char* ps = "error"; ps e 11/17/2008 r r o r MET CS 563 - Fall 2008 8. Overloading Functions and Operators 19

Why bother with operator overload for strings? - C++ has an extensive library of

Why bother with operator overload for strings? - C++ has an extensive library of functions that becomes accessible by including the header <string. h> , e. g. char *strcpy(char *s 1, const char *s 2) copies string s 2 into s 1. Returns value of s 1 char *strcat(char *s 1, const char *s 2) appends string s 2 to string s 1. Returns value of s 1. char *strcmp(const char *s 1, const char *s 2) compares string s 1 to string s 2. Returns 0 for s 1=s 2, <0 for s 1<s 2, and >0 for s 1>s 2. return type of sizeof operator for arrays, typically unsigned int; sizeof returns the total number of bytes in array size_t strlen(const char *s) returns length of s = number of characters in s not including ''. But C++ does not provide any operators that process strings as units!! Thus we need to define our own overloaded operators if we want more comfort in handling strings! 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 20

(ii) new and delete Allocate and Deallocate Memory at Run Time Goal: Use and

(ii) new and delete Allocate and Deallocate Memory at Run Time Goal: Use and free memory as needed during program execution instead of blocking it in a static size array throughout the run of the program Solution: Operator a) new allocates memory of the required type, provides pointer to it, and calls constructor: rational. Ptr * rational. Ptr Rational * rational. Ptr; nom rational. Ptr = new Rational; 0 In general: denom 1 <type. Name. Ptr> = new <Type. Name> ; b) delete frees the memory allocated by new : delete rational. Ptr; //memory allocated to *rational. Ptr is freed In general: delete <type. Name. Ptr> ; Important Note: In order to deallocate an entire array, not just the first element, one needs to use delete [ ] <array. Ptr> ; Otherwise access to memory is lost, but memory is not freed and cannot be used anymore --> memory leak. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 21

(iii) A User Defined class String Interface / Behavior Construct String object from C-string

(iii) A User Defined class String Interface / Behavior Construct String object from C-string Construct String object from another String object copy constructor Destruct String object freeing the memory Assign String (=) object to another String object Append Assign (+=) String object to another String object Append (+) String object to another String object Tell its length Tell its C-string value Tell the character at a given location overloaded subscripts [] Perform comparisons with other String instances Perform I/O of String instances Attributes / Data & Internals string value, i. e. the sequence of characters composing the string 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 22

class String - Definition Constructors and class String { Destructors public: String(const char s[]="");

class String - Definition Constructors and class String { Destructors public: String(const char s[]=""); //constructs deep copy of C-string s String(const String& s); //constructs deep copy of String type s ~String(); //deallocates string memory String& operator=(const String& rhs); //Assigns deep copy rhs String& operator+=(const String& rhs); //Appends (catenates) deep copy of rhs friend String operator+(const String& s, const String& t); //Returns deep copy of s appended with deep copy of t Access Member Functions Append and Assign int length() const; //Returns number of string characters const char* char. String() const; //Returns C-string value //Subscripts char& operator[](unsigned i); //Element of variable string at position i char operator[](unsigned i) const; //Element of constant string at position i 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 23

class String - Definition (continued) Comparison friend bool operator==(const String& s, const String& t);

class String - Definition (continued) Comparison friend bool operator==(const String& s, const String& t); friend bool operator!=(const String& s, const String& t); friend bool operator<=(const String& s, const String& t); friend bool operator>=(const String& s, const String& t); I/O Operators friend istream& operator>>(istream& in, String& s); //Reads <= 999 char to newline; Newline is extracted, but not assigned. friend ostream& operator<<(ostream& out, const String& s); //Ouputs string s private: char* info_; //pointer to char, NO dimension defined }; 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 24

deep copy means that the C-string is copied to the private array of the

deep copy means that the C-string is copied to the private array of the object, as opposed to a Prototype in class definition: shallow copy that simply copies the array name i. e. the address of the first element. class String - Constructors String(const char s[]=""); //constructs deep copy of C-string s String(const String& s); //constructs deep copy of String type s //copy constructor In general signature of copy constructor of some class A A(const A&) Definition: String: : String(const char *s) { info_= new char[strlen(s)+1]; //allocates char numbers +1 for '' strcpy(info_, s); } Use: String a("Jane"), b("George"); Definition: String: : String(const String& s) { info_= new char[strlen(s. info_)+1]; //allocates char numbers +1 for '' strcpy(info_, s. info_); } Use: 11/17/2008 String c(a); MET CS 563 - Fall 2008 8. Overloading Functions and Operators 25

class String - Destructor Prototype in class definition: ~String(); //deallocates string memory Definition: String:

class String - Destructor Prototype in class definition: ~String(); //deallocates string memory Definition: String: : ~String() { delete [] info_; //Deallocates array (of any length!) pointed to by info_ } We absolutely need the [] in order to free all memory allocated to the array info_ points to. Omitting it will lead to memory leak Use: Called automatically at end of object life time. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 26

class String - Assign and Assign Append Operators Are Members Prototype in class definition:

class String - Assign and Assign Append Operators Are Members Prototype in class definition: String& operator=(const String& rhs); //Assigns deep copy rhs String& operator+=(const String& rhs); //Appends(catenates) deep copy of rhs Definitions: String& String: : operator=(const String& rhs) { if(this != &rhs) { delete [] info_; info_ = new char[strlen(rhs. info_)+1]; strcpy(info_, rhs. info_); } return *this; } String& String: : operator+=(const String& rhs){ char *temp= new char[strlen(info_)+strlen(rhs. info_)+1]; strcpy(temp, info_); strcat(temp, rhs. info_); //catenates s. info_ to temp delete [] info_; //deallocate memory pointed to by info_ = temp; //assign info_ to point to catenation of string return *this; } Use: a=b; a+=b; //exactly as for any built-in type 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 27

(ii) Second Try: Overloaded '+' as a friend Function of class Rational Solution: Introduce

(ii) Second Try: Overloaded '+' as a friend Function of class Rational Solution: Introduce a new type of function that is • not a member functions but still • has access to the private data members of the class Such functions are referred as friend functions and are • declared within the class as are the member functions, but are differentiated from them through the keyword friend preceding the prototype. • defined outside the class with no mention to the class of which they are friends, i. e. the class knows who its friends are, but not the outside world. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 28

class String - Append Operator (+) Is friend Prototype in class definition: friend String

class String - Append Operator (+) Is friend Prototype in class definition: friend String operator+(const String& s, const String& t); //Returns deep copy of s appended with deep copy of t Definition: String operator+(const String& s, const String& t) //Returns deep copy of s appended with deep copy of t { String temp(s); //Copy constructor invoked for temp, temp initialized to s temp += t; //t appended to temp with overloaded += return temp; } Use: c = a + b; //exactly as for any built-in type 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 29

class String - Access Member Functions The methods accessing length and the C-string value

class String - Access Member Functions The methods accessing length and the C-string value of the String class are similar to any other access member functions and pretty unremarkable: int String: : length() const //Returns number of string characters { return strlen(info_); } and const char* String: : char. String() const//Returns C-string value { return info_; } The overload of the subscript, however, is more interesting. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 30

class String - Subscripts Member Functions returns object char& String: : operator[](unsigned i) //Element

class String - Subscripts Member Functions returns object char& String: : operator[](unsigned i) //Element of variable string at position i { if(i<0 || i>strlen(info_)){ cerr<<"Subscript range error"<<endl; exit(1) terminates program as if it } executed normally; used typically for return info_[i]; input errors } returns value char String: : operator[](unsigned i) const //Element of constant string at position i { cout<<"(returned from constant []) "; if(i<0 || i>strlen(info_)){ cerr<<"Subscript range error"<<endl; exit(1); } return info_[i]; } 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 31

class String - Comparison Operators Prototype in class definition: friend bool operator==(const String& s,

class String - Comparison Operators Prototype in class definition: friend bool operator==(const String& s, const String& t); Definition: bool operator==(const String& s, const String& t){ int l=s. length(); if( l < t. length()) l=s. length(); for (int i = 0; i<l ; i++) if(s. info_[i]!=t. info_[i]) return false; return true; } Use: bool equal = a == b; //exactly as for any built-in type 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 32

class String - I/O Operators Prototype in class definition: friend istream& operator>>(istream& in, String&

class String - I/O Operators Prototype in class definition: friend istream& operator>>(istream& in, String& s); //Reads <= 999 char to newline; Newline is extracted, but not assigned. friend ostream& operator<<(ostream& out, const String& s); //Ouputs string s Definition: istream& operator>>(istream& in, String& s) //Reads <= 999 char to newline; Newline is extracted, but not assigned. { char buffer[1000]; in. getline(buffer, 1000, 'n'); //Remove <=999 char from in //up to and including 'n', store all but 'n' in buffer, append '' s=String(buffer); return in; } ostream& operator<<(ostream& out, const String& s) //Ouputs string s { out << s. char. String(); return out; } Use: cout << a; cin >> b; //exactly as 563 for- Fall any 2008 built-in type 11/17/2008 MET CS 33 8. Overloading Functions and Operators

class String Driver – Constructors and Equality void main() { String a("Jane"), b("George"); cout<<"String

class String Driver – Constructors and Equality void main() { String a("Jane"), b("George"); cout<<"String a is "<<a<<" of length "<< a. length()<<endl; cout<<"String b is "<<b<<" of length "<< b. length()<<endl; cout << "String c is constructed with copy constructor with argument string a" << endl; String c(a); cout<<"String c is "<<c<<" of length "<< c. length()<<endl; String a is Jane of length 4 String b is George of length 6 String c is constructed with copy constructor with argument string a String c is Jane of length 4 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 34

class String Driver (continued) - Subscripts cout<< "n. The positions of the letters in

class String Driver (continued) - Subscripts cout<< "n. The positions of the letters in string a are: "<<endl; for(int i=0; i< a. length(); i++) cout << "Position "<< i << " holds " << a[i] <<endl; cout<< "n. The positions of the letters in string b are: "<<endl; for(i=0; i< b. length(); i++) cout << "Position "<< i << " holds " << b[i] <<endl; if(a==b) cout<<"n. Strings a and b are equal"<<endl; else cout<<"n. Strings a and b are not equal"<<endl; The positions of the letters in string a are: Position 0 holds J Position 1 holds a Position 2 holds n Position 3 holds e The positions of the letters in string b are: Position 0 holds G Position 1 holds e Position 2 holds o Position 3 holds r Position 4 holds g Position 5 holds e 11/17/2008 Strings a and b -are MET CS 563 Fall not 2008 equal 8. Overloading Functions and Operators 35

class String Driver (continued)- Subscripts for const Objects const String message("Careful!!"); cout<<"The message is

class String Driver (continued)- Subscripts for const Objects const String message("Careful!!"); cout<<"The message is "<<message<<" of length "<< message. length()<<endl; cout<< "n. The positions of the letters in the message are: "<<endl; for(i=0; i< message. length(); i++) cout << "Position "<< i << " holds " << message[i] <<endl; The message is Careful!! of length 9 The positions of the letters in the message are: (returned from constant []) Position 0 holds C (returned from constant []) Position 1 holds a (returned from constant []) Position 2 holds r (returned from constant []) Position 3 holds e (returned from constant []) Position 4 holds f (returned from constant []) Position 5 holds u (returned from constant []) Position 6 holds l (returned from constant []) Position 7 holds ! (returned from constant []) Position 8 holds ! 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 36

class String Driver (continued) – I/O String friend. List[5]; cout<<"n. Begin input for list

class String Driver (continued) – I/O String friend. List[5]; cout<<"n. Begin input for list of five friends: "<<endl; for(i=0; i<5 ; i++){ cout<<"Enter name "<< i+1 <<": "; cin>>friend. List[i]; } cout<<"n Your list of five friends contains: "<<endl; for(i=0; i<5 ; i++){ cout<<" >"<<friend. List[i]<<" and the length is " << friend. List[i]. length() <<endl; Begin input for list of five friends: Enter name 1: Jane Brown } Enter name 2: David Monroe }//end main() Enter name 3: Mary Simmons Enter name 4: Sam Voigt Enter name 5: Steve Cushing 11/17/2008 Your list of five friends contains: >Jane Brown and the length is 10 >David Monroe and the length is 12 >Mary Simmons and the length is 12 >Sam Voigt and the length is 9 >Steve Cushing and the length is 13 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 37

Overload - Summary • An overloaded function is a set of functions with the

Overload - Summary • An overloaded function is a set of functions with the same name and different argument lists • An overloaded operator is a C++ operator that has been extended through an additional definition to manipulate user defined and aggregate types in the same way as built-in types. • To overload an operator a function with the name operator<Operator. Symbol> , where operator is a keyword, must be defined. • Friend functions of a class are functions that are not member functions but have access to the private data members of the class. • Friend functions are declared within the class with the keyword friend preceding the function prototype, and are defined outside the class as a regular function, with no indication of their friendship relation to any class. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 38

Overload – Summary (continued) • Operator overload can be implemented as a member function

Overload – Summary (continued) • Operator overload can be implemented as a member function or a friend function: - overloaded binary operators (arithmetic and comparison) are implemented as friends in order to treat both their arguments in the same way; - overloaded assignment operators are implemented as member functions returning objects instead of values; - overloaded I/O operators are implemented as friend functions returning objects instead of values. • Strings are implemented as character arrays terminating with the NULL character ''. • User-defined classes with overloaded functions and operators make working with the user-defined and aggregate type objects as easy as working with simple built-in type such as integers. • The operators new and delete allocate and deallocate memory at run time. A final note: From this point on read as much code as possible and focus on design rather than on syntax. 11/17/2008 MET CS 563 - Fall 2008 8. Overloading Functions and Operators 39