Question: Program Development [ 4 0 % ] Write a cpp program to implement a calculator to compute equations of complex numbers. An equation consists of

Program Development [40%]
Write a cpp program to implement a calculator to compute equations of complex numbers. An equation consists of two operands and an operation. For example, for equation (2+3i)+(1+2i), complex number 2+3i and 1+2i are two operands and the operation is addition ("+"). A complex number is represented as a+bi, where a is the real part, b is the imaginary part and i is the imaginary unit. Assume the first operand of an equation is a+bi, the second operand is c+di. In this question, we consider three operations: addition, subtraction, and multiplication with the following definitions:
Addition: return the result as (a+c)+(b+d)i
Subtraction: return the result as (a-c)+(b-d)i
Multiplication: return the result as (ac-bd)+(ad+bc)i
To implement the calculator for complex number, first define a struct MyComplex consisting of two ints Real and Imaginary, which stores the real part and the imaginary part of a complex number. Then implement an abstract class Calculator with two protected members:
num1: a MyComplex variable, storing the value of the first operand for a equation.
num2: a MyComplex variable, storing the value of the second operand for a equation.
The class Calculator further have two public member functions:
, set (MyComplex n1, MyComplex n2) :set the values for the two operands num1 and num2.
getResult (): This function is used to return the result of the current equation. In class Calculator, getResult () is a pure virtual function and will be reloaded by other derived classes.
Then implement three operarions above with the following classes:
class AdditionCal. It (public) inherits class Calculator to perform addition of complex numbers by overloading virtual function getResult () in Calculator.
class SubtractionCal. It (public) inherits class Calculator to perform subtraction of complex numbers by overloading virtual function getResult () in Calculator.
class MultiplyCal. It (public) inherits class Calculator to perform multiplication of complex numbers by overloading virtual function getResult () in Calculator.
In this question, we will provide inputs for two complex numbers as operands to calculate the result of three operations (addition, subtraction, and multiplication). After reading inputs, you should define a pointer Cal with the data type of class Calculator. Then use Cal to create objects for each derived classes (AdditionCal, SubtractionCal, and MultiplyCal) to perform the corresponding operations, i.e., polymorphism.
Note 1: you can add proper constructor for the struct MyComplex.
Note 2: Check the examples below carefully for the output of complex numbers in different situations.
Example-1(Input is underlined):Example-2(Input is underlined):3Example-3(Inputs are underlined):Example-4(Inputs are underlined):Example-5(Inputs are underlined):
Program Development [ 4 0 % ] Write a cpp program

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 Programming Questions!