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

Answers were Sorted based on User's Feedback



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

Answer / manish soni

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Answer / 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

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

Answer / vishnu tiwari,cochin universit

It is a correct program,thanks a lot...........

Is This Answer Correct ?    14 Yes 15 No

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

Answer / waleed

i got a correct answer this will help me alot thanks

Is This Answer Correct ?    29 Yes 37 No

Post New Answer

More GATE Interview Questions

Graduate Aptitude Test in Engineering ( GATE ) Question Paper - 2002 COMPUTER SCIENCE & ENGINEERING

2 Answers   IBM,


Hi, I am also looking for pattern & questions of NIC exam. If anyone knows it pls send it to gaurav.yagik@gmail.com. thanks in advance. regards Gaurav

0 Answers  


Y Electrical engineerig mainly the supply as sinusoidal otherthan non-sinsoidal?

5 Answers   Infosys,


Howto calculate OLR range for a motor 

1 Answers  


i live in water, if you cut my head i am your door,if you cut my tail i am a fruit,if you cut both then i am with u.who am i?

1 Answers   Infosys,






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.

0 Answers  


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

0 Answers  


a positively charged object is placed in the centre of a room with a uniform magnetic field . if the magnetic field is pointing east the object will

1 Answers  


the servents of india society was founded by ?

0 Answers  


used of viscocity in petrolium

0 Answers   ONGC,


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.

0 Answers  


can i knw what are we supposed to read for the coming oces/dgfs 2009 exam?what kind of book are we supposed to study?

1 Answers   BARC,


Categories