Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Operator Overloading Part 1

By Sripriya R | on September 7, 2006 |
C++ Tutorials

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 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, it operates on only one operand. Some unary operators are named ++ also called the Increment operator, — also called the Decrement Operator, ! , ~ are called 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:

  1. return_type classname :: operator operator_symbol(argument) 
  2. { 
  3. ... 
  4. statements; 
  5. }

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

  1. class Exforsys 
  2. { 
  3.     private: 
  4.     ... 
  5.     public: 
  6.     void operator ++( ); 
  7.     ... 
  8. };

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

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

  1.  #include <iostream>
  2. using namespace std;
  3. class Exforsys 
  4. { 
  5. private: 
  6. 	int x; 
  7. public: 
  8. 	Exforsys( ) { x=0; }       //Constructor
  9. 	void display(); 
  10. 	void operator ++( ); 
  11. };
  12.  
  13. void Exforsys :: display() 
  14. { 
  15. 	cout << "nValue of x is: " << x; 
  16. } 
  17.  
  18. void Exforsys :: operator ++( )  //Operator Overloading for operator ++ defined
  19. { 
  20. 	++x; 
  21. } 
  22.  
  23. void main( ) 
  24. { 
  25. 	Exforsys e1,e2;         //Object e1 and e2 created
  26. 	cout << "Before Increment"; 
  27. 	cout << "nObject e1: "; e1.display(); 
  28. 	cout << "nObject e2: "; e2.display(); 
  29. 	++e1;  //Operator overloading applied
  30. 	++e2;  
  31. 	cout << "n After Increment"; 
  32. 	cout << "nObject e1: "; e1.display(); 
  33. 	cout << "nObject e2: "; e2.display(); 
  34. }

The output of the above program is:

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.

« « Group Discussion Challenges
Sample Resume – Auditor Resume » »

Author Description

Avatar

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • C++ Functions with Arguments

    September 21, 2006 - 0 Comment
  • Object Oriented Programming Paradigm

    June 10, 2006 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Looping

    September 4, 2007 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment
  • Elements of Object Oriented Programming

    June 10, 2006 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Type Conversions

    August 13, 2006 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment
  • Basic concepts of OOPS and Structure of C++ program

    July 26, 2006 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT