Create a class Complex to represent complex number. Using this pointer perform addition of two complex numbers.
Member function for adding A=A+B
Complex Add(Complex B)
{
//use this for accessing Object A of Complex class
}
#include<iostream>
using namespace std;
class Complex{
public:
int real;
int imag;
/* Function to set the values of
* real and imaginary part of each complex number
*/
void setvalue()
{
cin>>real;
cin>>imag;
}
/* Function to display the sum of two complex numbers */
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
/* Function to add two complex numbers */
void sum(Complex A, Complex B)
{
real=A.real+B.real;
imag=A.imag+B.imag;
}
};
int main()
{
Complex A,B,c3;
cout<<"Enter real and imaginary part of first complex number"<<endl;
A.setvalue();
cout<<"Enter real and imaginary part of second complex number"<<endl;
B.setvalue();
cout<<"Sum of two complex numbers is"<<endl;
c3.sum(c1,c2);
c3.display();
return 0;
}
Comments
Leave a comment