View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12890 Members
Join this group to post and comment.
yamrajbhalla
yamrajbhalla • Mar 30, 2009

C++ Programs by Yamrajbhalla

This Will Do the +,-,*and / of the complex numbers



/*Operator overloading for complex Numbers*/
#include
#include

class complex
{
 float real;
 float img;

 public:
  complex()
  {
   real=img=0;
  }


 complex(float real,float img)
 {
  this->real=real;
  this->img=img;
 }

 void setcomplex(float r,float i)
 {
  real=r;
  img=i;
 }

 void print()
 {
  cout<
out put



(3+3i) + (5+5i) =(8+8i)
(3+3i) - (5+5i) =(-2+-2i)
(3+3i) * (5+5i) =(15+15i)
(3+3i) / (5+5i) =(0.6+0.6i)
The program looks good except the logic of for * (multiplication) and / (division). The code for that was mentioned as

  complex operator *(complex c)
 {complex t;
   t.real=real*c.real;
   t.img=img*c.img;
   return t;
   }
  complex operator /(complex c)
  {complex t;
   t.real=real/c.real;
   t.img=img/c.img;
   return t;
  }
with output
(3+3i) * (5+5i) =(15+15i)
(3+3i) / (5+5i) =(0.6+0.6i)

The output should be
(3+3i) * (5+5i) = 3*5 + 3i*5 + 3*5i + 3i*5i = 15 + 15i + 15i -15 = (0+30i)

(3+3i) / (5+5i) = [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)]
= (15 + 15i -15i + 15) / (25 + 25i -25i + 25) = 30/50 = 0.6 = (0.6+0i)

Coding for multiplication should be easy. Lets try coding for division.

-Pradeep
shalini_goel14
shalini_goel14 • Mar 31, 2009
pradeep_agrawal
(3+3i) / (5+5i) = [(3+3i)/(5+5i)] * [(5+5i)/(5+5i)]
= (15 + 15i -15i + 15) / (25 + 25i -25i + 25) = 30/50 = 0.6 = (0.6+0i)

Coding for multiplication should be easy. Lets try coding for division.

-Pradeep
Hey pradeep, Don't you think for division it should be like following :
(3+3i) / (5+5i) = [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)]

Please correct me if anything wrong. 😔

Thanks !
Yes that should be [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)].

I did all calculation using [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)] but mentioned [(3+3i)/(5+5i)] * [(5+5i)/(5+5i)] by mistake.

Thanks for correcting me. I am modifying the above statement to reflect this.

-Pradeep
yamrajbhalla
yamrajbhalla • Apr 2, 2009
bro after posting this i have chnged the code for multiplication and divison bt i stuck with fever and i am nt able to update it
by the way thanks for view my post

Share this content on your social channels -