Operator Overloading in C++ | Non Overloaded Operator | Example | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Sunday, July 26, 2020

Operator Overloading in C++ | Non Overloaded Operator | Example | My CS Tutorial

Operator Overloading in C++ | Non Overloaded Operator in c++ | Example | Simple C++ program of operator overloading | C++ Operator Overloading | My CS Tutorial

Operator overloading in c++ | My CS Tutorial

Operators Overloading in C++


Operator overloading is an important concept in C++. You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well.

 It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) etc.

Syntax of operator overloading


To overload an operator, a special operator function is defined inside the class as:-

class class_name
{
_ _ _ _ _
public:
return_type operator symbol
{
_ _ _ _ _
}
_ _ _ _ _ _ _
};

operator overloading example


Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

Box operator+(const Box&);

declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded operators may be defined as ordinary non-member functions or as class member functions. In case we define above function as non-member function of a class then we would have to pass two arguments for each operand as follows −

Box operator+(const Box&, const Box&);


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 - ?:

Program of operator overloading


#include<iostream.h>
#include<conio.h>
class complex
{
int a,b,c;
public:
complex()
{
}
void getvalue()
{
cout<<"Enter two numbers=";
cin>>a>>b;
}
void operator ++()
{
a=++a;
b=++b;
}
void operator --()
{
a=--a;
b=--b;
}
void display()
{
cout<<a<<"+"<<b<<"i"<<endl;
}
};
void main()
{
complex obj;
obj.getvalue();
obj++;
cout<<"Increment complex number=\n";
obj.display();
obj--;
cout<<"Decrement complex number=\n";
obj.display();
getch();
}

Output:-

Enter two numbers= 3   6
Increment complex number
4 + 7i

Decrement complex number
3 + 6i

Implementing Operator Overloading in C++


Operator overloading can be done by implementing a function which can be :

Member Function
Non-Member Function
Friend Function

Operator overloading function can be a member function if the Left operand is an Object of that class, but if the Left operand is different, then Operator overloading function must be a non-member function.

Operator overloading function can be made friend function if it needs access to the private and protected members of class.

Restrictions on Operator Overloading in C++


Following are some restrictions to be kept in mind while implementing operator overloading.

Precedence and Associativity of an operator cannot be changed.
Arity (numbers of Operands) cannot be changed. Unary operator remains unary, binary remains binary etc.
No new operators can be created, only existing operators can be overloaded.
Cannot redefine the meaning of a procedure. You cannot change how integers are added.

Operator Overloading Examples


Here are various operator overloading examples to help you in understanding the concept.

Sr.No Operators & Example

1 Unary Operators Overloading

2 Binary Operators Overloading

3 Relational Operators Overloading

4 Input/Output Operator
        Overloading

5 ++ and -- Operators Overloading

6 Assignment Operators Overloading

7 Function call () Operator          
        Overloading

8 Subscripting [] Operator
        Overloading

9 Class Member Access Operator ->
        Overloading



Operator Overloading in C++ | Non Overloaded Operator in c++ | Example | Simple C++ program of operator overloading | C++ Operator Overloading | My CS Tutorial
_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN


No comments:

Post a Comment