Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in C++: The Complex class I wroted: #include #include using namespace std; class Complex { private: double real; double imag; public: Complex() { real

Code in C++:

The Complex class I wroted:

#include #include using namespace std; class Complex { private: double real; double imag;

public: Complex() { real = imag = 0; }

void input(istream &in) { cin>>real; cin>>imag; } void output(ostream &out) { cout<

int main() { Complex c1, c2; cout<<"Enter values for real and imaginary coefficients: "; c1.input(cin); cout<<"Enter values for real and imaginary coefficients: "; c2.input(cin); c1.output(cout); c2.output(cout); double temp; cout<<"Enter a new value for the real coefficient: "; cin>>temp; c2.setReal(temp); cout<<"The new real coefficient is "<

Extend the Complex class that I completed above. Recall that a complex number is of the form a + bi where a and b are real numbers and i^2 = -1. For example, 2.4 + 5.2i and 5.73 - 6.9i are complex numbers. a is referred to as the real part of the complex number and bi the imaginary part.

Make the following changes to the Complex class definition:

a) Add a constructor that will take two double arguments and initialize the real and imaginary components respectively.

b) Replace the input and output methods with overloaded >> and << operators, respectively, that are friend functions of the class. The output should be realValue + imaginaryValue i e.g., : 9.3 + 5.7i 12.4 - 8.4i

If the imaginaryValue component is zero, simply output the realValue as a 'regular' floating-point value. Also, if the imaginaryValue is negative, replace the '+' symbol with '-'. You may input the complex value in any format you wish.

c) Provide an overloaded addition operator that will return the sum of two Complex objects. For complex numbers a + bi and c + di, addition is defined as (a+c) + (b+d)i.

d) Provide an overloaded negation operator ( unary - ) that will negate both the real and imaginary parts of a complex number. For example if z is the complex number 3 - 2i, then -z should return a complex number -3 + 2i.

Thoroughly test your Complex number class by writing a test program that constructs various Complex values and displays the results until you are convinced that it is operating correctly. Include the following statements in your test program:

Complex c1,c2,c3;

cout << "Enter two complex values: ";

cin >> c1 >> c2;

c3 = c1+c2;

cout << "The sum is: " << c3 << endl;

cout << "and negating the sum yields: " << -c3 << endl;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions