In this article operator overloading we give the information bout operator overloading is a compile-time polymorphism in which operators are overloaded to give a special meaning to a user-defined data type.

Operator Overloading in C++:-

In C++, we can specify more than one definition for the operator in the same scope. This is called operator overloading.

In other words, “Operator overloading is a compile-time polymorphism in which operators are overloaded to give a special meaning to a user-defined data type.”

In C++, operator overloading is used to perform operations in user-defined data types.

Its main advantage is that through this we can perform different operations in the same operand.

For example, we use the ‘+’ operator to add integers. And ‘+’ is also used to concatenate strings.

Operator Overloading

In C++, most of the operator’s can be overloaded, but there are some operator’s in it which cannot be overloaded. Operator’s which cannot be overloaded are the following:-

scope operator – ::

sizeof

member selector -.

member pointer selector – *

ternary operator -?:

Syntax of operator overloading:-

We use a special operator function to overload the operator:-

class className

{

… .. …

public

returnType operator symbol (arguments)

{

… .. …

}

… .. …

};

Here,

  • Return Type is the return type of the function.
  • Operator is a keyword.
  • Symbol is an operator that we want to overload.
  • Arguments are the arguments passed to the function.

Rules of Operator Overloading:–

There are some important rules for this which we have to keep in mind.

  • Only built-in operators can be overloaded. If some operator is not present in C++ then we cannot overload them.
  • The precedence and associativity of operators cannot be changed.
  • We cannot use the friend function to overload any particular operators. But, we can use the member function to overload these operators.
  • It is necessary to define assignment “=”, subscript “[]”, function call “()” and arrow operator “->” as member function.
  • The overloaded operator must include at least one operand of the user-defined data type.
  • An overloaded operator cannot hold default parameters.
  • Some operators such as – assignment “=”, address “&”, and comma “,” are already overloaded.

Example of Operator Overloading:

#include<iostream.h>

#include<conio.h>

class demo

{

private:

int y;

public:

demo()

{

y=5;

}

void operator ++()

{

y=y+2;

cout<<“\n The Count is: “<<y;

}

void operator –()

{

y=y-2;

cout<<“\n The Count is: “<<y;

}

};

void main()

{

demo p;

clrscr();

++p;

–p;

getch();

}

OUTPUT:-

The Count is: 7

The Count is: 5

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *