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
Respected sir/madam, My self R SANKAR. I am final year BE civil engineering student (2014-2018). i have shortlisted for isro exam 2018.last university exam (7th sem) result was declared i have scored 6.98CGPA. but they not provide last result mark sheet still now 1. What are the documents bring to isro exam venue? 2. either bring bonafide certificate from college instead of mark sheets?
how to works epbx? how to configuration the epbx?
in vfd why we did not give directac ac supply?
why the uncontrolled rectifiers are converting fixed ac to fixed dc only why not it is converting variable dc
how to prepare for bcil bitp
types of highways
difference between pvc and gi pipes?
Equivalent resistance of 100 homs and 200 homs resistors connected in parallel is
I have cleared sbi clerk and have interview in 26/04/2010.But I am M.Ed & B.Ed,i want to u know my answed about not chose the teaching post
Design the test cases for Boundary Value analysis of the following. Consider a program that prompts the user to input three numbers (say x, y, z) and the data type for input parameters ensures that these will be integers greater than 0 and less than or equal to 100. The program should then output the numbers in ascending order.
Sir Pl Provide the diploma nautical science sample paper
used of viscocity in petrolium
what will the output of this program. sint a=10,b; b=a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a)
c program to compare the initial portions of the two strings and return the matched portion if matches, otherwise return the empty string.
can u explain me the way to know the relation between the active as well as the reactive power acting on an electical machine under the under excitation as well as over excitation mode