Question: Consider the following C struct that represents a complex number. struct complex { double real; double imaginary; }; (a) [20 points/5 points each] Change this

Consider the following C struct that represents a complex number.

struct complex {

double real;

double imaginary;

};

(a) [20 points/5 points each] Change this struct into a class. Make the member variables private, and add the following to the class:

  • A default constructor that initializes the real and imaginary parts to 0.
  • A constructor that allows initialization of both real and imaginary parts to any double value.
  • A public member function that returns the magnitude of the complex number.
  • A public member function that will accumulate another complex number into the object, for example, in pseudocode its equivalent to obj += other. (A note for those of you reading ahead, Im not looking for operator overloading here.)

(b) [10 points] Implement a main function that demonstrates how to use your class. Your code must exercise all parts of the definition from part (a).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!