Friday, 22 June 2018

Operator Overloading in C++

Same function name and different parameters is called overloading.
Operator overloading is an important concept in C++. It is used to perform operation on user define data type.

It is the example of compile time polymorphism in which an operator is overloaded to give user defined meaning to it.
Example:
'+' operator can be overloaded to perform addition on various data types, like for Integer, String (concatenation) etc.
Advantages:
It is used to perform different operations on the same operand.
Note:
Almost any operator can be overloaded in C++. However there are few operator which can not be overloaded. Operator that are not overloaded are follows
·         scope operator - ::
·         sizeof
·         member selector - .
·         member pointer selector - *
·         ternary operator - ?:
Syntax



Restrictions on Operator Overloading
1.    Operator overloading allows you to redefine the way operator works for user-defined types only (objects, structures). It cannot be used for built-in types (int, float, char etc.).

2.    No new operators can be created, only existing operators can be overloaded.

3.    Operator overloading cannot change the precedence and associatively of operators. However, if you want to change the order of evaluation, parenthesis should be used.

       4.    Two operators = and & are already overloaded by default in C++. For     example: To copy objects of same class, you can directly use = operator.  You do not need to create an operator function.

 Example 1:Addition of  Two Numbers using Operator Overloading
#include<iostream.h>
#include<conio.h>
class Add {
public:
int x, y, z;
void sum(int a, int b)
{
x=a;
y=b;
}

void display()
{
cout<<"\nSum of X is:"<<x;
cout<<"\nSum of Y is:"<<y;
}

void operator+(Add &);
};

void Add::operator+(Add &ob) {
x=x+ob.x;
y=y+ob.y;
display();
}

void main()
{
Add ob1, ob2;
clrscr();
ob1.sum(10,10);
ob2.sum(20,10);
ob1+ob2;
getch();
}

Output:
Sum of X is: 20
Sum of Y is: 30

Example 2: C++ program for unary minus (-) operator overloading.

#include<iostream.h>
using namespace std;
 class Demo
{
    private:
        int n;
        
    public:
        //function to get number
        void getNum(int x)
        {
            n=x;
        }
        //function to display number
        void dispNum(void)
        {
            cout << "value of n is: " << n;
        }
        //unary - operator overloading
        void operator - (void)
        {
            n=-n;
        }
};

int main()
{
    Demo d1;
    d1.getNum(20);
    -d1;
    d1.dispNum();
    cout << endl;
    return 0;
    
}
Output:
value of n is: -20

1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...