Question
Define a class for complex numbers, name it myComplex. In addition to the member functions that sets and gets the member attributes, define following member
Define a class for complex numbers, name it myComplex. In addition to the member functions that sets and gets the member attributes, define following member functions:
1. Constructors
a. A constructor uses default parameters.
b. Copy constructor
2. Mutators: setReal(), setImaginary()
3. Assessors: getReal(), getImaginary()
4. Overload output operators (<<) for myComplex class.
5. Overload input operator (>>) for myComplex class.
6. Overload assignment operator (=) for myComplex class
Write a driver program using myComplex class. Every member functions and operators should be called or activated to verify that they are working properly.
The objective of this lab is to define a class with constructors and overload input/output operators.
Define and implement myComplex class
Define and implement constructors of mComplex class
Overload operators: >>, <<, =
This is the input/output screen using the given driver program:
--- Result of using constructors--
c1 = 0 + 0i c2 = 3 - 2i c3 = 3 - 2i
---using input operator-------
Enter a complex number: 3 -2 c1 = 3 - 2i
---using assignment operator----
c1 = 3 - 2i c2 - 2i
press any key to continue....
To design a complex class, we will list the information (i.e. data) of a complex number and actions that a complex number can do.
A complex number:
A complex number has the form of a + bi, where a and b are real numbers and i is the square root of 1. We refer to a as the real part and b as the imaginary part of the complex number. For example: 2.5 + 3 i is a complex number. 2.5 is the real part and 3 is the imaginary part of 2.5 + 3 i.
Member attributes:
The class should have two member attributes to represent the real and imaginary parts of a complex number.
Input operator (>>) is defined as a friend function of class complex. By doing so, the input operator can modify private member attributes. The function prototype is:
friend std::istream& operator >> (std::istream &ins, myComplex &c);
The output operator (<<) is defined as a non-member function of class complex. The function prototype takes the similar form as that of the input operator.
Have a header file, implementation file, and a driver program.
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