Thursday 5 April 2018

Overloading Binary Operator

Binary operations are performed which require two operands to perform operations.

To add two numbers generally we use statement like
   C = sum(A,B);      //Functional Notation

This functional Notation can be replaced by natural looking expression
   C = A + B;
             By overloading '+' operator using an operator +( ) function.

The Overloaded function can be invoked by expression such as for binary operators-
   Syntax :
   X op Y;



#include<conio.h>

#include<iostream.h>
class complex 
{
float x;
float y;
public:
complex(double real, double imag)
{
x = real;
y = imag;
}
complex( )
{


}
complex operator+(complex c);
void display( );
};
complex complex ::operator+(complex c)
{
complex temp;
temp.x = x+c.x;
temp.y = y+c.y;
return (temp);
}
void complex::display( )
{
cout<<x<<" +j"<<y;
}
void main( )
{
complex c1(2.5,3.5);
complex c2(1.6,2.7);
complex c3;
clrscr( );
c3 = c1 + c2;
cout<<"\n\n\tc1 is--->"<<"\t";
c1.display( );
cout<<"\n\n\tc2 is--->"<<"\t";
c2.display( );
cout<<"\n\n\tc3 is--->"<<"\t";
c3.display( );
getch( );
}

Output:-
c1 is--->               2.5    +j3.5
c2 is--->               1.6    +j2.7
c3 is--->               4.1    +j6.2

Binary Operator Overloading using friend function

In many cases whether you overload an operator by using a friend or member function makes no functional difference. In those cases it is usually best to overload by using member function. However there is one situation in which overloading by using a friend increase flexibility of an overloaded operator.

We have seen how unary operator is overloaded, similar way we can overload binay operator. Binary operator requires two operands.

The syntax for overloading binary operator using friend function as follows:

friend Return_type operator op(Arguments)
{                      //Body of the operator function

}

WAP to overload binary '+' operator to add two complex numbers using friend function

#include<conio.h>
#include<iostream.h>
class complex
{
double real, ima;
public:
void getdata( )
{
cout<<"\n\tEnter real part of complex no.";
cin>>real;
cout<<"\n\tEnter imaginary part of complex no";
cin>>img;
}
void display( )
{
cout<<"\t"<<real<<"+i"<<img;
}
friend complex operator +(complex,complex);
};

complex operator +(complex c1, complex c2)
{
complex temp;
temp.real = c1.real + c2.real;
temp.img = c1.img + c2.img;
return temp;
}
void main( )
{
complex A,B,C;
clrscr( );
cout<<"\n\tEnter data for first object";
A.getdata( );
cout<<"\n\tEnter Data for second object";
B.getdata( );
C = A + B;
cout<<"\n\tData for first object--->";
A.display( );
cout<<"\n\tData for second object--->";
B.display( );
cout<<"\n\tAddition of two complex no is--->";
C.display( );
getch( );
}

Output:-
Enter data for first object
Enter real part of complex no12

Enter imaginary part of complex no5.6

Enter Data for second object
 Enter real part of complex no5

Enter imaginary part of complex no3.5

Data for first object--->    12+i5.6 
Data for second object--->   5+i3.5
Addition of two complex no is--->      17+i9.1

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (725) Python Coding Challenge (169) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses