Create the class Complex that has floating point data members for storing real and imaginary parts and perform complex number addition. The class Complex has the following member functions: Complex() - Null constructor invokes when an object is created Complex(float, float) - Parameterized constructor to set the specified value of real and imaginary parts in object friend complex sum(complex, complex) - Friend function to perform complex number addition and return complex number display() - Function to display complex number object
#include<iostream>
using namespace std;
class complex
{
int real,imag;
public:
void set()
{
cout<<"enter real and imag part";
cin>>real>>imag;
}
friend complex sum(complex,complex);
void display();
};
void complex::display()
{
cout<<"the sum of complex num is"<<real<<"+i"<<imag;
}
complex sum(complex a,complex b)
{
complex t;
t.real=a.real+b.real;
t.imag=a.imag+b.imag;
return t;
}
int main()
{
complex a,b,c;
a.set();
b.set();
c=sum(a,b);
c.display();
return(0);
}
Comments
Leave a comment