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>
class Complex
{
public:
float real{0};
float imaginary{0};
Complex() {}
Complex(float r, float i) :real(r), imaginary(i)
{}
void display() {
if (imaginary>=0)
std::cout << real << "+" << imaginary << "i"<<std::endl;
else std::cout << real << imaginary << "i" << std::endl;
}
friend Complex sum(Complex c, Complex d);
};
Complex sum(Complex c, Complex d) {
Complex result;
result.real = c.real + d.real;
result.imaginary = c.imaginary + d.imaginary;
return result;
}
int main()
{
//example work ;
Complex z1(10, 35), z2(8, -97), z3;
z3 = sum(z1, z2);
z3.display();
return 0;
}
Comments
Leave a comment