Operator Overloading concept using example on complex number

Operator Overloading concept

In OOP concept Method overloading is and important aspect. 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).
Operator overloading can be done by implementing a function which can be :

Member Function
Non-Member Function
Friend Function

Complex a(1.2,1.3); //this class is used to represent complex numbers
Complex b(2.1,3); //notice the construction taking 2 parameters for the real and imaginary part
Complex c = a+b; //for this to work the addition operator must be overloaded

The addition without having overloaded operator + could look like this:
Complex c = a.Add(b);

Operator Overloading concept using example on complex number

Here we will do an excercise based on method overloading.
If we have 2 complex number we can use operation add subtract multiply and divide on them by name
so we have created some function. let’s see how it is implemented.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class complex // this class give the a + b i value a real and b imaginary
{
	float real,img;
	public:
	complex()
	{
		real=0;
		img=0;
	}
	complex(float x,float y)
	{
		real=x;
		img=y;
	}
	complex(complex &c)
	{
		real=c.real;
		img=c.img;
	}
	complex operator+(complex c); // method overloading function prototype
	complex operator-(complex c);
	complex operator*(complex c);
	complex operator/(complex c);
	void print()
	{
		cout<<"Result= "<<real<<"+"<<img<<".i"<<endl;
	}
	
};
// method overloading function declaration
complex complex::operator-(complex c)
{
	complex tmp;
	tmp.real=real-c.real;
	tmp.img=img-c.img;
return tmp;
}
complex complex::operator+(complex c)
{
	complex tmp;
	tmp.real=real+c.real;
	tmp.img=img+c.img;
return tmp;
}
complex complex::operator*(complex c)
{
	complex tmp;
	tmp.real=((real*c.real)-(img*c.img));
	tmp.img=((real*c.img)+(img*c.real));
return tmp;
}
complex complex::operator/(complex c)
{
	complex tmp;
	tmp.real=((real*c.real)-(img*c.img))/((real*c.real)+(img*c.img));
	tmp.img=((real*c.img)+(img*c.real))/((real*c.real)+(img*c.img));
return tmp;
}
int main()
{
	complex c1(4,5),c2(2,3),c3,c4,c5,c6;
// call the function
	c3=c1.operator+(c2);
	c3.print();
	c4=c1.operator-(c2);
	c4.print();
	c5=c1.operator*(c2);
	c5.print();
	c6=c1.operator/(c2);
	c6.print();
	//getch();
}
	

Operator Overloading concept

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.