Opertaor Overloading

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

Operator Overloading

Dr. Savita Kumari

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

method syntax or operator syntax

s1.operator>(s2) vs. s1 > s2

What is operator overloading?


Changing the definition of an operator so it can be applied on the objects of a class is called operator overloading. To overload an operator, we need to write a function for the operator we are overloading.

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.

Restrictions on Operator Overloading


C++ operators that can be overloaded
Operators that can be overloaded + ~ /= <<= -new[] ! %= == ->* delete[] * = ^= != , / < &= <= -> % > |= >= [] ^ += << && () & -= >> || new | *= >>= ++ delete

C++ Operators that cannot be overloaded


Operators that cannot be overloaded . .* :: ?: sizeof

Restrictions on Operator Overloading


Overloading restrictions
Precedence of an operator cannot be changed Associativity of an operator cannot be changed 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

No new operators can be created


Use only existing operators

No overloading operators for built-in types


Cannot change how two integers are added Produces a syntax error

Operator Functions as Class Members vs. as friend Functions


Member vs non-member
Operator functions can be member or non-member functions When overloading ( ), [ ], -> or any of the assignment operators, must use a member function

Operator functions as member functions


Leftmost operand must be an object (or reference to an object) of the class
If left operand of a different type, operator function must be a nonmember function

Operator functions as non-member functions


Must be friends if needs to access private or protected members Enable the operator to be commutative

Overloading Stream-Insertion and Stream-Extraction Operators


Overloaded << and >> operators
Overloaded to perform input/output for userdefined types Left operand of types ostream & and istream & Must be a non-member function because left operand is not an object of the class Must be a friend function to access private data members

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 Operators as Instance Members


A binary operator overloaded as an instance member needs only one parameter, which represents the operand on the right:
class OpClass { int x; public: OpClass operator+(OpClass right); };
Chapter 11 Starting Out with C++: Early
2006 Pearson Education. All Rights Reserved

Overloading Operators as Instance Members


The left operand of the overloaded binary operator is the calling object The implicit left parameter is accessed through the this pointer
OpClass OpClass::operator+(OpClass r) { OpClass sum; sum.x = this->x + r.x; return sum; }

Invoking an Overloaded Operator


Operator can be invoked as a member function: OpClass a, b, s; s = a.operator+(b); It can also be invoked in the more conventional manner: OpClass a, b, s; s = a + b;

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

Invoking the assignment operator:


CpClass a, x(45); a.operator=(x); a = x;
2006 Pearson Education. All Rights Reserved

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

2006 Pearson Education. All Rights Reserved

11.7 Type Conversion Operators


Conversion Operators are member functions that tell the compiler how to convert an object of the class type to a value of another type The conversion information provided by the conversion operators is automatically used by the compiler in assignments, initializations, and parameter passing
2006 Pearson Education. All Rights Reserved

Syntax of Conversion Operators


Conversion operator must be a member function of the class you are converting from The name of the operator is the name of the type you are converting to The operator does not specify a return type

2006 Pearson Education. All Rights Reserved

Conversion Operator Example


To convert from a class IntVal to an integer: class IntVal { int x; public: IntVal(int a = 0){x = a;} operator int(){return x;} }; Automatic conversion during assignment: IntVal obj(15); int i; i = obj; cout << i; // prints 15

Overloading Unary Operators


Overloading unary operators
Can be overloaded with no arguments or one argument Should usually be implemented as member functions
Avoid friend functions and classes because they violate the encapsulation of a class

Example declaration as a member function:


class String { public: bool operator!() const; ... };

Overloading Unary Operators


Example declaration as a non-member function
class String { friend bool operator!( const String & ) ... }

Overloading Binary Operators


Overloaded Binary operators
Non-static member function, one argument Example:
class String { public: const String &operator+=( const String & ); ... };

y += z is equivalent to y.operator+=( z )

Overloading Binary Operators


Non-member function, two arguments Example:
class String { friend const String &operator+=( String &, const String & ); ... };

y += z is equivalent to operator+=( y, z )

Converting between Types


Cast operator
Forces conversions among built-in types Specifies conversions between user defined and built-in types Conversion operator must be a non-static member function Cannot be a friend function Do not specify return type
Return type is the type to which the object is being converted

For user-defined class A


A::operator char *() const;

Declares an overloaded cast operator function for creating a char * out of an A object

Converting between Types


A::operator int() const;

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

Compiler and casting


Casting can prevent the need for overloading If an object s of user-defined class String appears in a program where an ordinary char * is expected, such as
cout << s;

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

Overloading ++ and - Pre/post incrementing/decrementing operators


Allowed to be overloaded Distinguishing between pre and post operators
prefix versions are overloaded the same as other prefix unary operators
d1.operator++(); // for ++d1

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

You might also like