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


Please Help Members By Posting Answers For Below Questions

Equivalent resistance of 100 homs and 200 homs resistors connected in parallel is

1427


why the uncontrolled rectifiers are converting fixed ac to fixed dc only why not it is converting variable dc

1511


how to resize the components?

1915


REPO Structure and terminology

1985


in vfd why we did not give directac ac supply?

2190






difference between pvc and gi pipes?

1504


What are the most asked questions for learner operators in the interview for eskom?

1605


could u send me the model papers for drug inspector exam at imrupinder@gmail.com

1896


Discussion Topic: “Statutory Defintions” Discussion Questions: Q1 Is it correct that a loan or an advance by a private limited company to a shareholder of the company is considered as payment of dividend? Explain your views. Q2 Will a loan or advance by a private limited company to its holding company be considered as payment of dividend if the subsidiary possesses sufficient amount of accumulated profits?

2469


What is the number which when divided by 12,15 and 24 gives remainder 5?

1145


how to prepare for bcil bitp

1818


Book for preparation of HPCL (CS/IT) .

2312


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.

1652


Can some1 please mail me the question papers of iiit hyderabad...my mail id is cenablogspot@gmail.com

2180


.students decide to gift principal 4200 rupees. if teachers give 50% more than the students and an external person gives 3 times to that of teachers.how much do teachers give.

1953