CSIT Object Oriented Programming with C++(Operator Overloading)Part I

 In this post, I am going to teach you Basic Concept of OPERATOR OVERLOADING.

So, let's get started,

Firstly, What is overloading?

Here is the Answer : Overloading refers to the ability to use a single identifier to define multiple methods of a class that differ in their input and output parameters.

Now take a look at definition of Operator Overloading:

In C++ , Operator Overloading means giving additional meanings and schematics to Normal C++ operators so that we can use them with User Defined Data Types. 

Now look at the list of Operators that can't be Overloaded.

  • Class Member Access / Dot Operator (.)
  • Pointer to Member Operator (.*)
  • Scope Resolution Operator (::)
  • Size Of Operator ( sizeof() )
  • Conditional Operator (?:)
There are two ways to Overload Operators  in C++. They are;

  1. Using Member Function
  2. Using Friend Function
IMPORTANT : For Overloading Operators ,We have to use 'operator' function. 

Now lets have a look at syntax for overloading operators.

Syntax:

return_type operator op (arguments)
{
//code to be implemented
}

Here 'op' means operator sign that we are going to overload.
 
Note : If we are overloading Operator using Friend Function, we have to add 'friend' keyword in front.

The following operators can't be overloaded using friend function.
  • Assignment Operator (=)
  • Function Call Operator ( () )
  • Subscripting Operator ( [] )
  • Class member access / Arrow Operator( ->)

In next post, we will learn about Rules that should be followed while performing operator Overloading. So refer next  post for that topic.

If This post Helped you, Please leave a comment, and share with your Coder Friends.
Thank You.

Keep Learning, Keep Growing


T

Comments

Popular posts from this blog

CSIT Object Oriented Programming with C++(Insertion and Extraction Operator Overloading ) Part VI

CSIT Object Oriented Programming with C++(Unary Operator Overloading using Friend function) Part IV

CSIT Object Oriented Programming with C++(Unary Operator Overloading) Part III