c++ program to add 2 complex number using operator
overloading technique
Answer Posted / aalok
// Program for complex operation by using operator overloading.
#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
complex()
{}
void accept();
void display();
friend complex operator +(complex ,complex );
friend complex operator -(complex ,complex );
friend complex operator *(complex ,complex );
//friend complex operator /(complex ,complex );
}; // end of class complex.
void complex::accept()
{
cout<<"\n\n Enter Real & Imaginary part of complex";
cin>>r>>i;
} //end of accept().
void complex::display()
{
cout<<r<<"+"<<i<<"i";
} //end of display().
complex operator +(complex c1,complex c2)
{
complex ans;
ans.r=c1.r+c2.r;
ans.i=c1.i+c2.i;
return(ans);
} //end of complex +()
complex operator -(complex c1,complex c2)
{
complex ans;
ans.r=c1.r-c2.r;
ans.i=c1.i-c2.i;
return(ans);
} //end of complex -()
complex operator *(complex c1,complex c2)
{
complex ans;
ans.r=(c1.r*c2.r)-(c1.i*c2.i);
ans.i=(c1.r*c2.i)+(c1.i*c2.r);
return(ans);
} //end of complex *()
/*complex operator /(complex c1,complex c2)
{
complex ans,t1,t2;
t1.r=(c1.r*c2.r)+(c1.i*c2.i);
t1.i=(c1.i*c2.r)-(c1.r*c2.i);
t2.r=(c2.r*c2.r)+(c2.i*c2.i);
t2.i=0;
ans.r=t1.r/t2.r;
ans.i=t1.i/t2.i;
return(ans);
} //end of complex /() */
void main()
{
int ch;
complex c1,c2,c3;
clrscr();
cout<<"\n Enter first complex no";
c1.accept();
c1.display();
cout<<"\n Enter second complsx no";
c2.accept();
c2.display();
cout<<"\n\n~~~~~MENU~~~~~";
cout<<"\n 1. ADDATION ";
cout<<"\n 2. SUBTRACTION ";
cout<<"\n 3. MULTIPLACTION ";
cout<<"\n 4. DIVISION ";
cout<<"\n 5. EXIT ";
cout<<"\n\n Enter your choice = \t";
cin>>ch;
switch(ch)
{
case 1:
{
c3=c1+c2;
cout<<"\n ADDATION=";
c3.display();
} // end case 1.
case 2:
{
c3=c1-c2;
cout<<"\n SUBTRACTION=";
c3.display();
} // end case 2.
case 3:
{
c3=c1*c2;
cout<<"\n MULTIPLACTION=";
c3.display();
} // end case 3.
/* case 4:
{
c3=c1/c2;
cout<<"\n DIVISION=";
c3.display();
} // end case 4. */
} //end of switch case.
getch();
} //End of main ().
| Is This Answer Correct ? | 8 Yes | 2 No |
Post New Answer View All Answers
Can some1 please mail me the question papers of iiit hyderabad...my mail id is cenablogspot@gmail.com
is vertical heat exchanger has better heat transfer coefficient than horizontal heat exchanger?
difference between pvc and gi pipes?
What is the differents between the Physics & technical Physics
Sir Pl Provide the diploma nautical science sample paper
Can some1 please mail me the question papers of iiit hyderabad...my mail id is cenablogspot@gmail.com
REPO Structure and terminology
why is it important to take m.e mechanical?is our l;ife make happier than other b.e courses only answer no comments
richman keeps in pocket poor man throws and kids eat me am an english word P-U--E--L
please send me NTPC previous papers on my email ID 18amarjeet.civil@gmail.com or mr.yadav143@gmail.com
in vfd why we did not give directac ac supply?
c program to compare the initial portions of the two strings and return the matched portion if matches, otherwise return the empty string.
WHO IS INVITED WIRELESS FIDELITY TECHNOLOGY
how to resize the components?
what is monetary policy