Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complex Numbers You are a programmer and are asked to use what you have learned about a rational number class to write a complex number

Complex Numbers

You are a programmer and are asked to use what you have learned about a rational number class to write a complex number class.

If you plan to do this question, please refresh your memory by reading the following:

Arithmetic types such as rational and complex numbers are common examples of user-defined types.

Rational numbers are ordered pairs of integers. Example (3,4) could be a rational number, representing the fraction . Given that a and b are integers, (a,b) could be some rational number,provided that b is not equal to zero.

Complex numbers are ordered pairs of real numbers.

Example (3.7,8.25) could represent the complex number 3.7 + 8.25i. i represents the "imaginary number". i is a number such that i2= -1.

Given that x and y are real numbers (x,y)could represent the complex number x + yi.

x is called the real part and y is called the imaginary part.

If the above makes sense to you, continue with this question.

Your answer should all be in the answer space below. Indicate which part of the question your answering.

Your answers inpart aand part bshould replace the question marks in the questions below.

Part a: Data members (2 points)

How should the data members be defined.

class Complex

{

private:

jQuery22409417793678945892_1621458471805??????

????????

};

Part b: Constructor Functions

0)The default constructor should have both the imaginary and the real parts zero. (2 points)

Complex()

{

???? ????

???? ????

}

1) Define the following for the two-parameter input constructor function for the complex number class. (2 points)

Complex (?? X,?? Y )

{

???? ????

???? ????

}

2)Define the following conversion constructor that creates a complex number from a real number. (2 points)

Complex ( ??? X )

{

????????

????????

}

Part c: Arithmetic operations

If we have two complex numbers x and y, we can add, subtract, multiply and divide them. Below you will be asked to write the corresponding member functions.

1)Write the overloaded + operator for the complex class. (2 points)

Example:(a + bi) + ( c + di )= (a+c) + (b+d)i

Complex operator+ ( Complex r ) ;

2)Write the overloaded - operator for the complex class. (2 points)

Example:(a+bi)-(c+di) = (a-c) - (b-d)i

Complex operator- ( Complex r ) ;

3) Write the overloaded * operator for the complex class. (5 points)

Example:(a+bi) * (c + di) = ac+(ad)i+ (bc)i -bd

Complex operator* ( Complex r );

4) Write the overloaded / operator for the complex class. (5 points)

Example:(a+bi) / (c + di) =(ac+bd)/(c2+ d2) + (bc-ad)/(c2+ d2)i

Complex operator/ ( Complex r );

Part d: output(left shift) and input(right shift)functions. These functions are friend functions.

0)

(a) What are friend functions,

(b) in what way are they like member functions,

(c) can we makeoperation <<() and operator >>() member function? why or why not? (8 points)

1)Write the overloaded output function (5 points):

ostream& operator<<(ostream& os, Complex& x);

2)Write the overloaded input function (5 points):

istream& operator>>(istream& is, Complex &x );

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions