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

Answer Posted / binoy

Write a C++ program to create a class called COMPLEX and
implement the following overloading functions ADD that
return a COMPLEX number .
I. ADD(a,s2) - where a is an integer(real part) and s2 is a
complex number.
II. ADD(s1,s2) - where s1 and s2 are complex objects.


#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
void read();
void print();
friend complex add(int a,complex c);
friend complex add(complex c1,complex c2);
};
void complex::read()
{
cout<<"Enter real and imaginary\n";
cin>>r>>i;
}
void complex::print()
{
cout<<r<<"+i"<<i<<endl;
}
complex add(int a,complex c)
{
complex t;
t.r=a+c.r;
t.i=c.i;
return t;
}
complex add(complex c1,complex c2)
{
complex t;
t.r=c1.r+c2.r;
t.i=c1.i+c2.i;
return t;

}
void main()
{
int a=2;
clrscr();
complex s1,s2,s3;
s1.read();
cout<<"\ns1 : ";
s1.print();
s2=add(a,s1);
cout<<"s2 : 2+s1\n";
cout<<" : ";
s2.print();
s3=add(s1,s2);
cout<<"s3=s1+s2\n";
cout<<"s1 : ";
s1.print();
cout<<"s2 : ";
s2.print();
cout<<"s3 : ";
s3.print();
getch();
}


MORE C++ PROGRAMS AND SOLUTIONS:
1. ADDING 'TIME' OBJECTS AND DISPLAYING THE RESULT IN
HH:MM:SS FORMAT USING MEMBER FUNCTION

2. OPERATOR OVERLOADING - ADDITION, SUBTRACTION OF MATRICES
USING OPERATOR OVERLOADING

3. STACK IMPLEMENTATION USING OPERATOR OVERLOADING IN C++

http://programscpp.blogspot.in/

Is This Answer Correct ?    3 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

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

2101


how to prepare for bcil bitp

1726


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

1337


please send me NTPC previous papers on my email ID 18amarjeet.civil@gmail.com or mr.yadav143@gmail.com

1663


sir can you send me previous question paper of jsplGET entrance test

1909






.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.

1841


the servents of india society was founded by ?

1588


richman keeps in pocket poor man throws and kids eat me am an english word P-U--E--L

2062


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.

6236


in which state of india has no panchayatraj

1355


in vfd why we did not give directac ac supply?

2118


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

1744


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

1808


plz send me bhel paper....

1769


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

5981