Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

Operator Overloading (Part I)

 
Category: C++
Comments (2)

Operator Overloading

In this C++ tutorial you will learn about Operator Overloading in two Parts, In Part I of Operator Overloading you will learn about Unary Operators, Binary Operators and Operator Overloading – Unary operators.



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


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


We have seen in previous sections the different types of operators. Broadly classifying operators are of two types namely:


  • Unary Operators
  • Binary Operators

Unary Operators:

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


Binary Operators:

The arithmetic operators, comparison operators, and arithmetic assignment operators all this which we have seen in previous section of operators come under this category.


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


Operator Overloading – Unary operators

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


The general syntax for defining an operator overloading is as follows:


return_type classname :: operator operator symbol(argument)
{
…………..
statements;
}


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


In the above:


  • return_type – is the data type returned by the function
  • class name - is the name of the class
  • operator – is the keyword
  • operator symbol – is the symbol of the operator which is being overloaded or
  • defined for new functionality
  • :: - is the scope resolution operator which is used to use the function definition outside the class. The usage of this is clearly defined in our earlier section of How to define class members.

For example


Suppose we have a class say Exforsys and if the programmer wants to define a operator overloading for unary operator say ++, the function is defined as


  


Inside the class Exforsys the data type that is returned by the overloaded operator is defined as


class Exforsys
{
    private:
    ……….
    public:
    void operator ++( );
    …………
};


So the important steps involved in defining an operator overloading in case of unary operators are namely:


Inside the class the operator overloaded member function is defined with the return data type as member function or a friend function. The concept of friend function we will define in later sections. If in this case of unary operator overloading if the function is a member function then the number of arguments taken by the operator member function is none as seen in the below example. In case if the function defined for the operator overloading is a friend function which we will discuss in later section then it takes one argument.


The operator overloading is defined as member function outside the class using the scope resolution operator with the keyword operator as explained above


Now let us see how to use this overloaded operator member function in the program


#include <iostream.h>
class Exforsys
{
    private:
    int x;
    public:
    Exforsys( ) { x=0; }      
//Constructor
    void display();
    void Exforsys ++( );
};


void Exforsys :: display()
{
    cout<<”\nValue of x is: “ << x;
}


void Exforsys :: operator ++( )  //Operator Overloading for operator ++ defined
{
    ++x;
}


void main( )
{
    Exforsys e1,e2;        
//Object e1 and e2 created
    cout<<”Before Increment”
    cout <<”\nObject e1: ”<<e1.display();
    cout <<”\nObject e2: ”<<e2.display();
    ++e1; 
//Operator overloading applied
    ++e2; 
    cout<<”\n After Increment”
    cout <<”\nObject e1: ”<<e1.display();
    cout <<”\nObject e2: ”<<e2.display();
}


The output of the above program is:


Before Increment
Object e1:
Value of x is: 0
Object e1:
Value of x is: 0
Before Increment
Object e1:
Value of x is: 1
Object e1:
Value of x is: 1


In the above example we have created 2 objects e1 and e2 f class Exforsys. The operator ++ is overloaded and the function is defined outside the class Exforsys.



When the program starts the constructor Exforsys of the class Exforsys initialize the values as zero and so when the values are displayed for the objects e1 and e2 it is displayed as zero. When the object ++e1 and ++e2 is called the operator overloading function gets applied and thus value of x gets incremented for each object separately. So now when the values are displayed for objects e1 and e2 it is incremented once each and gets printed as one for each object e1 and e2.


This is how unary operators get overloaded. We will see in detail how to overload binary operators in next section.


-R.Sripriya



Read Next: C++ Operator Overloading Part II



 

 

Comments


er.gourav said:

  good job ,excellent explanation.......
February 7, 2008, 6:43 am

sutha said:

  In function overloading we have same function name with different number of arguments. Here we use same function for different tasks. Is it applicable to operators(operator overloading)?
April 22, 2009, 1:04 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape