class Complex { friend Complex addComplex (const Complex & c1, const Complex & c
ID: 3732230 • Letter: C
Question
class Complex {
friend Complex addComplex (const Complex & c1, const Complex & c2);
// add two complex numbers c1 and c2
// and return a complex one.
friend Complex subComplex (( const Complex &c1, const Complex &c2);
// subtract two complex number c1 and
// c2 and return a complex one
public:
Complex (float r = 0.0, float g= 0.0);
Complex (const Complex & c); //The copy constructor
void print( ) const; // Print out the class data
Complex & negate ( ); // Multiplies the data members of a complex object by a
// negative one.
Complex multipComplex ( const Complex &c) const; // multiply two complex
// numbers and return a complex one
Complex assginComplex ( const Complex & c); // assign one complex number to
// another one and return the result in a complex object.
bool isEqual (const Complex & c) const; //compares one complex number to
// another and returns a boolean value
private:
float rel;
float img;
};
Question 1: A complex number is a number with two components, namely, the real part (rel) and the imaginary part (img). For example the complex number (a+bi) has a as the real part and b as the imaginary part. In developing such a class, assume that both of the real and imaginary parts to be floats. The symbol i representV-1. A declaration for the Complex class is presented next. class Complex { friend Complex addComplex (const Complex & cl, const Complex & c2) // add two complex numbers cl and c2 // and return a complex one friend Complex subComplex (const Complex &cl;, const Complex &c2;) // subtract two complex number cl and / c2 and return a complex one public Complex (float -0.0, float g 0.0); Complex (const Complex & c);I/The copy constructor void print() const Complex & negate () // Printout the class data // Multiplies the data members of a complex object by a // negative one Complex multipComplex ( const Complex &c;) const;multiply two complex // numbers and return a complex one Complex assginComplex ( const Complex & c); // assign one complex number to / another one and return the result in a complex object. bool isEqual (const Complex & c) const, l/compares one complex number to // another and returns a boolean value private float rel float img Implement and test the class Complex. Write a driver program to test your Complex class Instantiate several Complex objects. Test that all your member and friend functions work properly Hint Addition of two complex numbers: (atbi) +(ctdi) (atc) (b+d) i Subtraction: (atbi) (c+di)(a-c) + (b-d) i Multiplication: (a+bi) * (ctdi)-(ac-bd) + (ad+bc)Explanation / Answer
#include using namespace std; class Complex { friend Complex addComplex(const Complex& c1, const Complex& c2); // add two complex numbers c1 and c2 // and return a complex one. friend Complex subComplex ( const Complex &c1, const Complex &c2); // subtract two complex number c1 and // c2 and return a complex one public: Complex (float r = 0.0, float g= 0.0); Complex (const Complex & c); //The copy constructor void print( ) const; // Print out the class data Complex & negate ( ); // Multiplies the data members of a complex object by a // negative one. Complex multipComplex ( const Complex &c) const; // multiply two complex // numbers and return a complex one Complex assginComplex ( const Complex & c); // assign one complex number to // another one and return the result in a complex object. bool isEqual (const Complex & c) const; //compares one complex number to // another and returns a boolean value private: float rel; float img; }; Complex::Complex (float r, float g) { rel = r; img = g; } Complex::Complex (const Complex & c) { rel = c.rel; img = c.img; } void Complex::print( ) const { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.