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
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 complex number i and i are two operands and the operation is addition A complex number is represented as where is the real part, is the imaginary part and is the imaginary unit. Assume the first operand of an equation is the second operand is In this question, we consider three operations: addition, subtraction, and multiplication with the following definitions:
Addition: return the result as
Subtraction: return the result as
Multiplication: return the result as
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:
num: a MyComplex variable, storing the value of the first operand for a equation.
num: a MyComplex variable, storing the value of the second operand for a equation.
The class Calculator further have two public member functions:
set MyComplex n MyComplex n :set the values for the two operands num and num
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, ie polymorphism.
Note : you can add proper constructor for the struct MyComplex.
Note : Check the examples below carefully for the output of complex numbers in different situations.
ExampleInput is underlined:ExampleInput is underlined:ExampleInputs are underlined:ExampleInputs are underlined:ExampleInputs are underlined:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
