SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Friends Overloaded Operators And Arrays In Classes
Exercise:
Programming Projects
Question:6 | ISBN:9780321531346 | Edition: 7

Question

Define a class for complex numbers. A complex number is a number of

the form a + b*i

where, for our purposes, a and b are numbers of type double, and i is a

number that represents the quantity . Represent a complex number

as two values of type double. Name the member variables real and

imaginary. (The variable for the number that is multiplied by i is the one

called imaginary.) Call the class Complex.

Include a constructor with two parameters of type double that can be used

to set the member variables of an object to any values. Also include a

constructor that has only a single parameter of type double; call this

parameter real_part and define the constructor so that the object will be

initialized to real_part + 0*i. Also include a default constructor that

initializes an object to 0 (that is, to 0 + 0*i). Overload all of the following

operators so that they correctly apply to the type Complex: ==, +, , *, >>, and <<. You should write a test program to test your class.

Hints: To add or subtract two complex numbers, you add or subtract the

two member variables of type double. The product of two complex

numbers is given by the following formula:

(a + b*i)*(c + d*i) == (a*c b*d) + (a*d + b*c)*i

In the interface file, you should define a constant i as follows:

const Complex i(0, 1);

This defined constant i will be the same as the i discussed earlier.

delete p;

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.