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
what is the difference between interface and abstract class
how to works epbx? how to configuration the epbx?
sir , please send me previous gate ece questionpapers with explanations.i want 15 years of gate ece question papers
what will the output of this program. sint a=10,b; b=a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a)
why is it important to take m.e mechanical?is our l;ife make happier than other b.e courses only answer no comments
the servents of india society was founded by ?
i have M.TECH interview on RF AND MICROWAVE ENGINEERING IN IIT (KHARAGPUR/ROORKEE).KINDELY TELL WHAT SUBJECTS SHOULD I READ AND WHAT ARE THE INTERVIEW QUESTIONS.
difference between pvc and gi pipes?
could u send me the model papers for drug inspector exam at imrupinder@gmail.com
Can some1 please mail me the question papers of iiit hyderabad...my mail id is cenablogspot@gmail.com
If Q is the point of ABCD rectangle where QA=3CM,QB=4CM,and QC=5cm then find the length of QD ?
c program to compare the initial portions of the two strings and return the matched portion if matches, otherwise return the empty string.
Book for preparation of HPCL (CS/IT) .
how to resize the components?
plz send me bhel paper....