Question: 12.24 HW2: Practice with Classes Objectives: Become familiar with developing a C++ class, to include developing: A constructor An multiplication operator A friend function (input

12.24 HW2: Practice with Classes

Objectives:

  • Become familiar with developing a C++ class, to include developing:
    • A constructor
    • An multiplication operator
    • A friend function (input operator)
  • Introduce various C and C++ functions for parsing user input

Assignment Details

This assignment consists of 6 parts.

Part 1: Using an IDE of your own choosing (Visual Studio is recommended), create a new project and copy the starter code

  • ComplexNumber.h
  • ComplexNumber.cpp and
  • TestComplexNumber.cpp

to this new project for developing and testing your solution.

Part 2: Complete the specification in ComplexNumber.h by adding function prototypes for:

  • A ComplexNumber constructor which takes as a parameter a single C-sytle string (i.e., const char *)

  • A multiplication operator as a member function which takes as a parameter a single constant reference to a ComplexNumber and returns a ComplexNumber

  • An input operator as a friend function which takes two input parameters

    • A reference to an istream and
    • A reference to a ComplexNumber and

    Initializes the ComplexNumber to value given in the istream and returns the modified istream

Part 3: Write implementations for the three new functions specified in Part 2. Your functions should adhere to the following requirements:

  • Your constructor initializes a ComplexNumber object using a C-string with one of the following forms:
    1. [+|-]realNumber
    2. [+|-][realNumber]i
    3. [+|-]realNumber(+|-)[realNumber]i
    • Please note:
      • | stands for OR,
      • [] stands for OPTIONALLY,
      • () stands for EITHER, and
      • realNumber stands for any valid real number literal, including numbers in signed or unsigned scientific notation.
    • For full credit your constructor must be able to read complex numbers matching any of the three forms.
    • For simplicity, your solution is not required to reject ill-formed complex numbers (i.e., numbers which do not match the specification above)
  • Your multiplication operator must be a member function using * as the operator and correctly multiply the this object and the ComplexNumber parameter and return the resulting ComplexNumber according to the definition for ComplexNumber multiplication from Wolfram.
  • Your input operator must be a friend function using >> as the operator and allow initialization of the ComplexNumber parameter using the input stream (e.g., typically cin).

These three functions are the only new functions you are required to add to ComplexNumber.cpp. While it is not necessary, you may add as many additional non-member or non-friend functions as you want to complete your solution.

Part 4: Test your implementations from Parts 2 and 3 using TestComplexNumber.cpp and various valid complex numbers.

Example Inputs and Results:

 Testing the string constructor with "1.23e-1+4.5i" results in the value 0.123+4.5i 

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!