c++ program to add 2 complex number using operator
overloading technique
Answers were Sorted based on User's Feedback
manish soni tagore collage jaipur
ph:9785261817
e_mail:monupanhar@yahoo.comf;
hapy to help;
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
struct complex
{
int real;
int img;
};
void getdata(complex &);
complex sum(complex,complex);
complex mult(complex,complex);
void display(complex);
int main()
{
complex c1,c2,c3,c4;
getdata(c1);
getdata(c2);
c3=sum(c1,c2);
cout<<"Sum= ";
display(c3);
c4=mult(c1,c2);
cout<<"MULT= ";
display(c4);
getch();
return 0;
}
complex sum(complex c1,complex c2)
{
complex c;
c.real=c1.real+c2.real;
c.img=c1.img+c2.img;
return c;
}
void display(complex c)
{
cout<<c.real<<"+"<<c.img<<"i"<<endl;
}
complex mult(complex c1,complex c2)
{
complex c;
c.real=c1.real*c2.real-c1.img*c2.img;
c.img=c1.img*c2.real+c1.real*c2.img;
return c;
}
void getdata(complex &c)
{
cout<<"Enter first complex number:"<<endl;
cout<<"Enter real ";
cin>>c.real;
cout<<"Enter img ";
cin>>c.img;
}
Is This Answer Correct ? | 44 Yes | 22 No |
Answer / a.bharathi raja bsc.,hdca.,
A.BHA RAJA
THENNAGALAPURI
#include<iostream.h>
#include<conio.h>
class complex
{
int r,i;
public:
{
void getdata(int a,int b)
{
r=a; i=b;
}
complex operator+(complex,complex);
void display(void);
};
complex complex :: operator+(complex a,complex b)
{
complex c;
c.r=a.r+b.r;
c.i=a.i+b.i;
return c;
}
void complex :: display(void)
{
cout<<r<<"+i"<<i<<"\n";
}
void main()
{
complex p,q,r;
int n,m;
clrscr();
cout<<"Enter the real number:";
cin>>n;
cout<<"Enter the imag number:";
cin>>m;
p.getdata(n,m);
cout<<"Enter the real number:";
cin>>n;
cout<<"Enter the imag number:";
cin>>m;
q.getdata(n,m);
r=p+q;
p.display(p);
q.display(q);
r.display(r);
getch();
}
Is This Answer Correct ? | 30 Yes | 12 No |
Answer / sundari.b.v
#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp x=x+c.x;
temp y=x+c.y;
return
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1=",c1.display();
cout<<"c2=",c2.display();
cout<<"c3=",c3.display();
return 0;
}
Is This Answer Correct ? | 23 Yes | 6 No |
Answer / a.bharathi raja
#include<iostream.h>
#include<conio.h>
class complex
{
int r;
int i;
public:
complex()
{ }
complex(int a,int b)
{
r=a;i=b;
}
friend complex operator+(complex,complex);
friend show(complex);
complex operator+(complex c1,complex c2)
{
complex c3;
c3.r=c1.r+c2.r;
c3.i=c1.i+c2.i;
return(c3);
}
show(complex c)
{
cout<<c.r<<"i+"<<c.i<<endl;
}
void main()
{
complex a,b,c;
clrscr();
a.complex(3,6);
b.complex(4,7);
c=a+b;
show(a);
show(b);
show(c);
getch()
}
Is This Answer Correct ? | 24 Yes | 11 No |
Answer / arvind
write a c++ program to create class student having
datamember name,Roll_no,age,and branch intilcization all the
member using constructor print the all the details on the
screen.
Is This Answer Correct ? | 9 Yes | 3 No |
Answer / 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 |
Answer / 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 |
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 |
Answer / vishnu tiwari,cochin universit
It is a correct program,thanks a lot...........
Is This Answer Correct ? | 14 Yes | 15 No |
Answer / waleed
i got a correct answer this will help me alot thanks
Is This Answer Correct ? | 29 Yes | 37 No |
where v get the models for the gate exam?& for apptitude test ?
For the FIFA world cup, Paul the octopus has been predicting the winner of each match with amazing success. It is rumored that in a match between 2 teams A and B, Paul picks A with the same probability as A’s chances of winning. Let’s assume such rumors to be true and that in a match between Ghana and Bolivia, Ghana the stronger team has a probability of 2/3 of winning the game. What is the probability that Paul will correctly pick the winner of the Ghana-Bolivia game?
giving an example describe how the modulus function works
c program to compare the initial portions of the two strings and return the matched portion if matches, otherwise return the empty string.
what is monetary policy
What is the differents between the Physics & technical Physics
Graduate Aptitude Test in Engineering ( GATE ) Question Paper - 2000 COMPUTER SCIENCE & ENGINEERING
What is the optimum frequency of an electronic choke?
what is a isenticity
1 Answers Suraj Constructions,
Who was the first writer in english to be awarded the sahitya akademi Award in 1961?
weather any body knows how th partiton of sections in rrb is aote 1,genera nowlge 2,intellgince 3,maths 4,english 5,science
i wanta model of tech. questions.