c++ program to add 2 complex number using operator
overloading technique

Answer Posted / varsha

#include<iostream>
using namespace std;

class complex_number
{
int real_part;
int imaginary_part;
public:
void setData(int val,int flag)
{
real_part = 0;
imaginary_part = 0;
if(flag == 0)
{
real_part = val;
imaginary_part = 0;
}
else if(flag == 1)
{
real_part = 0;
imaginary_part = val;
}
}
void setData(int rp, int ip, int flag)
{ real_part = 0;
imaginary_part = 0;
if(flag == -1)
{
real_part = rp;
imaginary_part = ip;
}
}
void show()
{ if(imaginary_part>=0)
cout<<"Complex Number
is: "<<real_part<<"+"<<imaginary_part<<"i"<<endl;
else
cout<<"Complex Number
is: "<<real_part<<imaginary_part<<"i"<<endl;
}
complex_number operator+ (complex_number op2);
complex_number operator- (complex_number op2);
complex_number operator++();
complex_number operator++(int x);
};

complex_number complex_number:: operator+(complex_number
op2)
{
complex_number temp;
temp.real_part = op2.real_part + real_part;
temp.imaginary_part = op2.imaginary_part +
imaginary_part;
return temp;
}

complex_number complex_number:: operator-(complex_number
op2)
{
complex_number temp;
temp.real_part = real_part - op2.real_part;
temp.imaginary_part = imaginary_part -
op2.imaginary_part;
return temp;
}

complex_number complex_number:: operator++()
{
real_part++;
imaginary_part++;
return *this;
}
complex_number complex_number:: operator++(int x)
{
++real_part;
++imaginary_part;
return *this;
}
int main()
{
complex_number ob1;
ob1.setData(5,0);

complex_number ob2;
ob2.setData(10,1);
//cout<<"Before addition"<<ob2.show();
complex_number ob3;
ob3.setData(2,3,-1);
complex_number ob4;
ob4.setData(4,5,-1);
complex_number ob5;
cout<<"Before addition, complex numbers are :"<<endl;
ob4.show();
ob3.show();
ob5 = ob4+ob3;
cout<<"After Addition";
ob5.show();
cout<<"Before addition, complex numbers are :"<<endl;
ob1.show();
ob4.show();
ob5 = ob1 + ob4;
cout<<"After Addition";
ob5.show();
cout<<"Before addition, complex numbers are :"<<endl;
ob2.show();
ob4.show();
ob5 = ob2+ ob4;
cout<<"After Addition";
ob5.show();
cout<<"Before preincrement"<<endl;
ob5.show();
++ob5;
cout<<"After preincrement"<<endl;
ob5.show();
cout<<"Before postincrement"<<endl;
ob5.show();
ob5++;
cout<<"After postincrement"<<endl;
ob5.show();
cout<<"Before Subtraction, complex numbers are :"<<endl;
ob4.show();
ob3.show();
ob5 = ob4-ob3;
cout<<"After Subtraction";
ob5.show();
cout<<"Before Subtraction, complex numbers are :"<<endl;
ob1.show();
ob4.show();
ob5 = ob1 - ob4;
cout<<"After Subtraction";
ob5.show();
cout<<"Before Subtraction, complex numbers are :"<<endl;
ob2.show();
ob4.show();
ob5 = ob2- ob4;
cout<<"After subtraction";
ob5.show();


int i;
cin>>i;
return 0;
}

Is This Answer Correct ?    78 Yes 77 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

If Q is the point of ABCD rectangle where QA=3CM,QB=4CM,and QC=5cm then find the length of QD ?

2467


c program to compare the initial portions of the two strings and return the matched portion if matches, otherwise return the empty string.

1754


Book for preparation of HPCL (CS/IT) .

2194


sir , please send me previous gate ece questionpapers with explanations.i want 15 years of gate ece question papers

1773


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

1932






how to prepare for bcil bitp

1737


22. A solution of 1% (w/v) starch at pH 6.7 is digested by 15 μg of β‐amylase (mol wt 152,000). The rate of maltose (mol wt = 342) had a maximal initial velocity of 8.5 mg formed per min. The turnover number is

6004


Sir Pl Provide the diploma nautical science sample paper

2081


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

1071


what will the output of this program. sint a=10,b; b=a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a)

1107


can i have the model papers for gate along with answers on my email id krish231191@gmail.com please???

1838


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

1432


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

2115


The horse has played a little known but very important role in the field of medicine. Horses were injected with toxins of diseases until their blood built up immunities. Then a serum was made from their blood.Serums to fight with diphtheria and tetanus were developed this way. It can be inferred from the passage, that horses were (A) given immunity to diseases (B) generally quite immune to diseases (C) given medicines to fight toxins (D) given diphtheria and tetanus serums

2994


weather any body knows how th partiton of sections in rrb is aote 1,genera nowlge 2,intellgince 3,maths 4,english 5,science

1993