Question: Task: Formatting & Parsing The complex number is expressed in the form a +b i , where a and b are real numbers, and i
Task: Formatting & Parsing
The complex number is expressed in the form a +bi, where a and bare real numbers, and i represents the "imaginary unit".
- Write a class that defines a complex number and use streaming operations for formatting string output and parsing.
Details
- Define a class Complex that will store 2 integer coefficients of the complex number.
- Add the default constructor that accepts 2 int-typed parameters with default arguments which set these parameters to zero.
- All data members must be private.
- Write accessor methods re and im that provide read-only access to the real and imaginary part of the Complex object.
- Overload the stream extraction operator >> for the Complexclass to output the string representation of the complex number, as "i" where is a real and is an imaginary coefficients of the complex number.
- See related tests in main.cpp.
- The operator declaration is provided.
- Hint: For formatting, use append method and += operator of std::basic_string, or std::ostringstream streaming operators.
- Overload the stream insertion operator << for the Complex class to parse the formatted string, see above, and extract real and imaginary into the Complex object.
- See related tests in main.cpp.
- The operator declaration is provided.
- Assume that input string is always correctly formatted.
- Hint 1: Try to find a position in the string that separates real and imaginary parts of the complex number.
- Hint 2: Use stoi function for converting a string to an integer number.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
