Home / Expert Answers / Accounting / identify-suitable-coding-modules-in-a-simple-program-that-implement-polymorphism-in-this-questio-pa631

(Solved): Identify suitable coding modules in a simple program that implement polymorphism In this questio ...



Identify suitable coding modules in a simple program that implement polymorphism In
this question you are asked to apply oper

Two instances of complexNumber can be added using the + operator. Consider the
following example:
cnl = 5 + 8i i.e. real = 5

(1)
Write the main() function to construct some ComplexNumber instances and
demonstrate the use of the +, * and << operators.

Identify suitable coding modules in a simple program that implement polymorphism In this question you are asked to apply operator overloading which is a form of polymorphism. An instance of complexNumber can be represented by real and img. Both real and img are integers. Two instances of complexNumber can be added using the + operator. Consider the following example: cnl = 5 + 8i i.e. real = 5 and img = 8 cn2 = 2 + 7i i.e. real = 2 and img = 7 cn3 = cnl + cn2 = 7 + 15i * Two instances of ComplexNumber can be multiplied using the * operator. The rule of multiplication is as follows: (a + bi) * (c + di) = (ac - bd) + (ad + bc)i Consider the following example: cnl = 5 + 8i i.e. real-5 and img = 8 cn2 = 2 + 7i i.e. real = 2 and img-7 cn4 = cnl * cn2 = (10 - 56) + (35 + 16) i = -46 + 5li Implement a CH class to model ComplexNumber as specified below: (a) Declare the data members real and img. (marks) Write the constructor and destructor. (5 marks) Overload the + operator. (4 marks) (d) Overload the * operator. (4 marks) (e) e Overload the << operator so that the statement (1) Write the main() function to construct some ComplexNumber instances and demonstrate the use of the +, * and << operators. (marks)


We have an Answer from Expert

View Expert Answer

Expert Answer


// Code #include using namespace std; class ComplexNumber { private: int real, imag; public: // Constructor ComplexNumber(int r = 0, int i = 0) { real = r; imag = i; } // Destructor ~ComplexNumb
We have an Answer from Expert

Buy This Answer $5

Place Order