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
the servents of india society was founded by ?
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 resize the components?
.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.
is vertical heat exchanger has better heat transfer coefficient than horizontal heat exchanger?
in which state of india has no panchayatraj
what is the difference between interface and abstract class
used of viscocity in petrolium
in vfd why we did not give directac ac supply?
I am looking for bank exam coaching in Hyderabad. Can anybody tell me which is the best institute for BANK and which branch is good giving me branch details.
why the uncontrolled rectifiers are converting fixed ac to fixed dc only why not it is converting variable dc
weather any body knows how th partiton of sections in rrb is aote 1,genera nowlge 2,intellgince 3,maths 4,english 5,science
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?
REPO Structure and terminology
how to prepare for bcil bitp