In Class Operator Overloading

13
Operator overloading Operator overloading Operator overloading is a very important Operator overloading is a very important feature of Object Oriented Programming. feature of Object Oriented Programming. Curious to know why!!? It is because by Curious to know why!!? It is because by using this facility programmer would be using this facility programmer would be able to create new definitions to existing able to create new definitions to existing operators. In fact in other words a single operators. In fact in other words a single operator can take up several functions as operator can take up several functions as desired by programmers depending on the desired by programmers depending on the argument taken by the operator by using argument taken by the operator by using the operator overloading facility. the operator overloading facility.

Transcript of In Class Operator Overloading

Page 1: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 1/13

Page 2: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 2/13

Operator overloadingOperator overloading

Operator overloading provides a flexibility toOperator overloading provides a flexibility tocreate new definitions for most of the C++create new definitions for most of the C++

operators.operators.

By using classes to create new variables andBy using classes to create new variables andoperator overloading to create definitions for operator overloading to create definitions for operators, one can create a new language of operators, one can create a new language of one¶s own design.one¶s own design.

Page 3: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 3/13

Operator overloadingOperator overloading

After knowing about the feature of operator  After knowing about the feature of operator overloading now let us see how to define andoverloading now let us see how to define anduse this concept of operator overloading in C++use this concept of operator overloading in C++programming language.programming language.

We have seen in previous sections the differentWe have seen in previous sections the differenttypes of operators. Broadly classifying operatorstypes of operators. Broadly classifying operatorsare of two types namely:are of two types namely:

Unary OperatorsUnary Operators

Binary OperatorsBinary Operators

Page 4: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 4/13

Types of operatorsTypes of operators

Unary Operators:Unary Operators:

As the name implies takes operate on only one operand. As the name implies takes operate on only one operand.Some unary operators are namely ++ called asSome unary operators are namely ++ called asIncrement operator,Increment operator, ---- called as Decrement Operator, !called as Decrement Operator, !

,, ~, unary minus.~, unary minus.

Binary Operators:Binary Operators:

The arithmetic operators, comparison operators, andThe arithmetic operators, comparison operators, andarithmetic assignment operators all this which we havearithmetic assignment operators all this which we have

seen in previous section of operators come under thisseen in previous section of operators come under thiscategory.category.

Both the above classification of operators can beBoth the above classification of operators can beoverloaded. So let us see in detail each of this.overloaded. So let us see in detail each of this.

Page 5: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 5/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators

As said before operator overloading helps the As said before operator overloading helps theprogrammer to define a new functionality for the existingprogrammer to define a new functionality for the existingoperator. This is done by using the keyword operator.operator. This is done by using the keyword operator.

The general syntax for defining an operator overloadingThe general syntax for defining an operator overloadingis as follows:is as follows:

return type class name :: operator operator return type class name :: operator operator symbol(argument)symbol(argument){{««««..««««..statements;statements;}}

Page 6: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 6/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators Thus the above clearly specifies that operator overloading is definedThus the above clearly specifies that operator overloading is definedas a member function by making use of the keyword operator.as a member function by making use of the keyword operator.

In the above:In the above:

return_typereturn_type ± ± is the data type returned by the functionis the data type returned by the function class nameclass name -- is the name of the classis the name of the class

operator operator ± ± is the keywordis the keyword

operator symboloperator symbol ± ± is the symbol of the operator which is beingis the symbol of the operator which is beingoverloaded or overloaded or 

defined for new functionalitydefined for new functionality :::: -- is the scope resolution operator which is used to use the functionis the scope resolution operator which is used to use the function

definition outside the class. The usage of this is clearly defined indefinition outside the class. The usage of this is clearly defined inour earlier section of How to define class members.our earlier section of How to define class members.

Page 7: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 7/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators For exampleFor example

Suppose we have a class say ExforsysSuppose we have a class say Exforsysand if the programmer wants to define aand if the programmer wants to define a

operator overloading for unary operator operator overloading for unary operator 

say ++, the function is defined assay ++, the function is defined as

Page 8: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 8/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators

Page 9: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 9/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators Inside the class Exforsys the data type that isInside the class Exforsys the data type that is

returned by the overloaded operator is definedreturned by the overloaded operator is definedasas

class Exforsysclass Exforsys{{

private:private:«««.«««.

public:public:void operator ++( );void operator ++( );««««««««

};};

Page 10: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 10/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators The operator overloading is defined asThe operator overloading is defined as

member function outside the class usingmember function outside the class using

the scope resolution operator with thethe scope resolution operator with the

keyword operator as explained abovekeyword operator as explained above

Now let us see how to use this overloadedNow let us see how to use this overloaded

operator member function in the programoperator member function in the program

Page 11: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 11/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators #include <iostream.h>#include <iostream.h>class Exforsysclass Exforsys{{

private:private:int x;int x;public:public:

Exforsys( ) { x=0; }Exforsys( ) { x=0; } //Constructor  //Constructor void display();void display();void Exforsys ++( );void Exforsys ++( );

};};

void Exforsys :: display()void Exforsys :: display(){{

cout<<´cout<<´\ \nValue of x is: ³ << x;nValue of x is: ³ << x;}}

void Exforsys :: operator ++( )void Exforsys :: operator ++( ) //Operator Overloading for operator ++ //Operator Overloading for operator ++defineddefined{{

++x;++x;} }} }

Page 12: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 12/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators void main( )void main( )

{{Exforsys e1,e2;Exforsys e1,e2;  //Object e1 and e2 //Object e1 and e2

createdcreated

cout<<´Before Increment´cout<<´Before Increment´cout <<´cout <<´\\nObject e1: ´<<e1.display();nObject e1: ´<<e1.display();cout <<´cout <<´\\nObject e2: ´<<e2.display();nObject e2: ´<<e2.display();++e1;++e1;  //Operator overloading applied //Operator overloading applied++e2;++e2;cout<<´cout<<´\\n  After Increment´n  After Increment´cout <<´cout <<´\\nObject e1: ´<<e1.display();nObject e1: ´<<e1.display();cout <<´cout <<´\\nObject e2: ´<<e2.display();nObject e2: ´<<e2.display();

}}

Page 13: In Class Operator Overloading

8/8/2019 In Class Operator Overloading

http://slidepdf.com/reader/full/in-class-operator-overloading 13/13

Operator OverloadingOperator Overloading ± ± UnaryUnary

operatorsoperators The output of the above program is:The output of the above program is:

Before IncrementBefore IncrementObject e1:Object e1:Value of x is: 0Value of x is: 0Object e1:Object e1:Value of x is: 0Value of x is: 0Before IncrementBefore Increment

Object e1:Object e1:Value of x is: 1Value of x is: 1Object e1:Object e1:Value of x is: 1Value of x is: 1