operator overloading and type convesions bcas,bapatla b.mohini devi

12
operator operator overloading overloading and and type convesions type convesions BCAS,Bapatla BCAS,Bapatla B.mohini devi B.mohini devi

Upload: esther-preston

Post on 04-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

operator overloading operator overloading and and

type convesions type convesions

BCAS,Bapatla BCAS,Bapatla B.mohini devi B.mohini devi

Page 2: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

pointpointer,vitual er,vitual functions&functions&PolyPolymorpmorphismhism

Page 3: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

IntnroductionIntnroduction C++ has ability to provide the operators with a special C++ has ability to provide the operators with a special

meaning for a data types.meaning for a data types.

We can overload (give additional meaning to) all the C++ We can overload (give additional meaning to) all the C++ operators except:operators except:

It is one of the many exciting features of C++.It is one of the many exciting features of C++.– Class member access operators ( . & .*)Class member access operators ( . & .*)– Scope resolution operators ( : : )Scope resolution operators ( : : )– Size operator (sizeof)Size operator (sizeof)– Conditional operators (? : )Conditional operators (? : )

When an operator is overloaded, its original meaning is not When an operator is overloaded, its original meaning is not lost.lost.

Page 4: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

Defining Operator OverloadingDefining Operator Overloading

To define an additional task to an operator, we must To define an additional task to an operator, we must

specify what it means in relation to the class to which specify what it means in relation to the class to which

the operator is applied.the operator is applied.

This is done with the help of a special function called This is done with the help of a special function called

operator functionoperator function..return type class-name : :operator op (arg-list)return type class-name : :operator op (arg-list){{ Function body Function body // task defined// task defined}}

Page 5: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

return type class-name : :operator return type class-name : :operator op (arg-list)op (arg-list)

{{ Function body Function body // task defined// task defined}} return typereturn type is the type of value returned by is the type of value returned by

the specified operation.the specified operation. opop is the operator being overloaded.is the operator being overloaded.

opop is preceded by the keyword is preceded by the keyword operatoroperator..

operator opoperator op is the function name.is the function name.

Page 6: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

Operator Function must be eitherOperator Function must be either– member function (non-static)member function (non-static)OrOr– friend function.friend function.The basic difference :The basic difference :

A friend function will have only one A friend function will have only one argument for unary operators and two argument for unary operators and two for binary operators.for binary operators.

A member function has no arguments A member function has no arguments for unary operators and one argument for unary operators and one argument for binary operators.for binary operators.

This is because the object used to This is because the object used to invoke the member function is passed invoke the member function is passed implicitly and therefore is available for implicitly and therefore is available for the member function.the member function.

Arguments may be passed either by Arguments may be passed either by value or by reference.value or by reference.

Page 7: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

The process of overloading involves the The process of overloading involves the following steps:following steps:– Create a class that defines the data type that is to Create a class that defines the data type that is to

be used in the overloading operation.be used in the overloading operation.

– Declare the operator function Declare the operator function operator op( )operator op( ) in in the public part of the class. the public part of the class. It may be either a It may be either a member function or a friend function.member function or a friend function.

– Define the operator function to implement the Define the operator function to implement the required operations.required operations.

Page 8: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

Overloading Unary OperatorsOverloading Unary Operators

Consider a unary minus operator:Consider a unary minus operator:– It takes just one operand.It takes just one operand.

– It changes the sign of an operand when applied It changes the sign of an operand when applied to a basic data item.to a basic data item.

– The unary minus when applied to an object The unary minus when applied to an object should change the sign of each of its data should change the sign of each of its data items.items.

Page 9: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

Overloading Binary OperatorsOverloading Binary Operators

As a rule, in overloading binary operators,As a rule, in overloading binary operators,

– the the left-handleft-hand operand is used to invoke the operand is used to invoke the operator function and operator function and

– the right-hand operand is passed as an the right-hand operand is passed as an argumentargument

Page 10: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

return complex((x+c.x), (y+c.y));return complex((x+c.x), (y+c.y));

The compiler invokes an appropriate The compiler invokes an appropriate constructor, initializes an object constructor, initializes an object with no name and returns the with no name and returns the contents for copying into an contents for copying into an object.object.

Such an object is called a temporary Such an object is called a temporary object and goes out of space as object and goes out of space as soon as the contents are assigned soon as the contents are assigned to another object.to another object.

Page 11: Operator overloading and type convesions BCAS,Bapatla B.mohini devi

Overloading Binary Operators Overloading Binary Operators Using FriendsUsing Friends

– Friend function requires two arguments to be explicitly Friend function requires two arguments to be explicitly passes to it.passes to it.

– Member function requires only one.Member function requires only one.

friend complex operator+(complex, complex);friend complex operator+(complex, complex);

complex operator+(complex a, complex b)complex operator+(complex a, complex b){{ return complex((a.x + b.x),(a.y + b.y));return complex((a.x + b.x),(a.y + b.y));}}

– We can use a friend function with built-in type data as We can use a friend function with built-in type data as

the left-hand operand and an object as the right-hand the left-hand operand and an object as the right-hand

operand.operand.

Page 12: Operator overloading and type convesions BCAS,Bapatla B.mohini devi