Answer to Question #173786 in C++ for Rocky bhai

Question #173786

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


1
Expert's answer
2021-03-20T23:28:00-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS