Sunday, October 30, 2011

C++ Program to Add ,Substract,Multiply and Divide Complex Numbers Using Operator Overloading

The following menu driven C++ program uses operator overloading to to perform Addition,Subtraction,Multiplication and Division of two complex numbers.Here Operators +,-,* and / are overloaded to perform the required operations with the desired objects.
eg:a*b in the terms of overloading means is to perform the '*' Operation associated with object a on object b.
The Complete Source Code is given below.If you have any Queries Just Comment on the Post.
 #include<iostream.h>  
 #include<conio.h>  
 #include<string.h>  
 #include<stdio.h>  
 class complex  
 {  
  int i,r;  
  public:  
  void read()  
  {  
  cout<<"\nEnter Real Part:";  
  cin>>r;  
  cout<<"Enter Imaginary Part:";  
  cin>>i;  
  }  
  void display()  
  {  
  cout<<"\n= "<<r<<"+"<<i<<"i";  
  }  
  complex operator+(complex a2)  
  {  
  complex a;  
  a.r=r+a2.r;  
  a.i=i+a2.i;  
  return a;  
  }  
  complex operator-(complex a2)  
  {  
  complex a;  
  a.r=r-a2.r;  
  a.i=i-a2.i;  
  return a;  
  }  
  complex operator*(complex a2)  
  {  
  complex a;  
  a.r=(r*a2.r)-(i*a2.i);  
  a.i=(r*a2.i)+(i*a2.r);  
  return a;  
  }  
  complex operator/(complex a2)  
  {  
  complex a;  
  a.r=((r*a2.r)+(i*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));  
  a.i=((i*a2.r)-(r*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));  
  return a;  
  }  
 };  
 void main()  
 {  
 int ch;  
 clrscr();  
 complex a,b,c;  
 do  
 {  
  cout<<"\n1.Addition 2.Substraction";  
  cout<<" 3.Mulitplication 4.Division 5.Exit\n";  
  cout<<"\nEnter the choice :";  
  cin>>ch;  
  switch(ch)  
  {  
  case 1:  
       cout<<"\nEnter The First Complex Number:";  
       a.read();  
       a.display();  
       cout<<"\nEnter The Second Complex Number:";  
       b.read();  
       b.display();  
       c=a+b;  
       c.display();  
       break;  
  case 2:  
       cout<<"\nEnter The First Complex Number:";  
       a.read();  
       a.display();  
       cout<<"\nEnter The Second Complex Number:";  
       b.read();  
       b.display();  
       c=b-a;  
       c.display();  
       break;  
  case 3:  
       cout<<"\nEnter The First Complex Number:";  
       a.read();  
       a.display();  
       cout<<"\nEnter The Second Complex Number:";  
       b.read();  
       b.display();  
       c=a*b;  
       c.display();  
       break;  
  case 4:  
       cout<<"\nEnter The First Complex Number:";  
       a.read();  
       a.display();  
       cout<<"\nEnter The Second Complex Number:";  
       b.read();  
       b.display();  
       c=a/b;  
       c.display();  
       break;  
  }  
  }while(ch!=5);  
 getch();  
 }  

9 comments:

  1. what is it's expected output?

    ReplyDelete
  2. This is also a very good post which I really enjoy reading. It is not everyday that I have the possibility to see something like this. 数学家教

    ReplyDelete
  3. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Visit this url

    ReplyDelete
  4. Technology oriented careers have been making a comeback. Accordingly, talented technology managers are necessary in every area of the field - from Web design and development, to database-driven e-commerce, to software engineering, to technical service and support. programming assignments

    ReplyDelete
  5. But wanna admit that this is very helpful , Thanks for taking your time to write this. ترفندهای بازی انفجار

    ReplyDelete
  6. We’re a group of volunteers and starting a new scheme in our community. Your web site offered us with valuable info to work on. You’ve done an impressive job and our whole community will be thankful to you. devops course

    ReplyDelete
  7. I will be very happy to discover this kind of post very useful personally, since it consists of great deal of info. I usually prefer to read the top quality content material and also this factor I discovered in your soul submit. Thanks for sharing buy shrooms

    ReplyDelete
  8. I just want to let you know that I just check out your site and I find it very interesting and informative.. best chair for programmers

    ReplyDelete

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...