Arithmetic Operations using Operator Overloading

Write a program for all the four arithmetic operations in float data type using operator overloading concept.

Questions by shakthisangi

Showing Answers 1 - 2 of 2 Answers

avinashk60

  • Jan 19th, 2010
 

const ComplexNo& ComplexNo::operator+(const ComplexNo &b)
{
ComplexNo &result = *this;
i = i + b.i;
j = j + b.j;
return result;
};

  Was this answer useful?  Yes


Addition Operator:
-----------------------------------------------------
Complex operator +( const Complex &temp)

{
    Complex ctemp;

 ctemp.m_real = m_real+ temp.m_real;
 ctemp.m_imag = m_imag+temp.m_imag;

  cout<<" operator is overloaded"<<endl;

  return ctemp;
//here c1+c2 by taking other temporary object "ctemp" ,c1 will not get modified.
}

Subtraction Operator:
-----------------------------------------------------
Complex operator -( const Complex &temp)

{
    Complex ctemp;

 ctemp.m_real = m_real- temp.m_real;
 ctemp.m_imag = m_imag-temp.m_imag;

  cout<<" operator is overloaded"<<endl;

  return ctemp;
//here c1+c2 by taking other temporary object "ctemp" ,c1 will not get modified.
}

Multiplication Operator
-------------------------------------------------
Complex Complex::operator *(const Complex &temp)
{

 Complex ctemp=*this;

 ctemp.m_real=m_real*temp.m_real;
 ctemp.m_imag=m_imag*temp.m_imag;

 return ctemp;
}
 and  now You can go on with other operator......okkk

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions