Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started