Mylab Programming With Pearson Etext -- Access Code Card -- For C++ How To Program (early Objects Version)
bartleby

Concept explainers

bartleby

Videos

Textbook Question
100%
Book Icon
Chapter 10, Problem 10.8E

(Complex Class) Consider class Complex shown in Figs. 10.14-10.16. The class enables operations on so-called complex numbers. These are numbers of the form real Part + imaginaryPart * i. where i has the value

  Chapter 10, Problem 10.8E, (Complex Class) Consider class Complex shown in Figs. 10.14-10.16. The class enables operations on

  1. Modify the class to enable input and output of complex numbers via overloaded >> and
  2. << operators, respectively (you should remove the toString function from the class)
  3. Overload the multiplication operator to enable multiplication of two complex numbers as in algebra.
  4. Overload the == and t = operators to allow comparisons of complex numbers.

After doing this exercise, you might want to read about the Standard Library’s complex class (from) header <comp1ex>)

  1. // Fig. 10.14 : Complex.h
  2. // Complex class definition.
  3. #include <string>
  4. #ifndef COMPLEX_H
  5. #define COMPLEX_H
  6. Class Complex {
  7. Public :
  8. Explicit Complex (double = 0.0, double = 0.0) ; // constructor
  9. Complex operator+(const Complex&) const; // addition
  10. Complex operator-(const Complex&) const; // subtraction
  11. Std:: string toString () const:
  12. Private:
  13. Double real : // real part
  14. Double imaginary ; // imaginary part
  15. };
  16. #endif

Fig. 10.14 Complex class deginition.

  1. // Fig. 10.15 : Complex.cpp
  2. // Complex class member-function definitions.
  3. #include <string>
  4. #include “Complex.h” // Complex class definition
  5. Using namespace std;
  6. // Constructor
  7. Complex: : Complex (double realPart, double imaginaryPart)
  8. : reak {real Part}, imaginary {imaginaryPart} { }
  9. // addition operator
  10. Complex Complex : : operator+ (const Complex& operand2) const {
  11. Return Complex {real +operand2. Real, imaginary+ operand2. Imaginary};
  12. }
  13. // subtraction operator
  14. Complex Complex : : operator-(const Complex& opetand2) const {
  15. Return Complex { real − operand 2.real, imaginary − operand2. Imaginary } :
  16. }
  17. // return string representation of a complex object in the form: (a, b)
  18. String Complex : : to String () const {
  19. Return “(“s +to_string(real) + “, “s + to_string(imaginary) + “)”s;
  20. }

Fig.10.15 Complex class member-function definition

25 // Fig. 10.16; fig10_16.cpp
26 // Complex class test program.
27 #include <iostream>
28 #include “Complex.h”
29 using namespace std;
30
31 int main () {
32 Complex x:
33 Complex y {4.3, 8,2}:
34 Complex z {3,3, 1,1}:
35
36 count << “x: “ << x.toString () << :\ny: “<<y.to string ()
37 << “\nz: “ <<z:
38
39 x=y+z;
40 count << “\n\nx = y+z:\n” << x.toString () << “= “ << y.toString()
41 << “ + ” <<z.toString ():
42
43 x = y - z :
44 count << “\n\nx = y-z:\n” << x. to String () << “ = “ << y. to String ()
45 << “ - “ << z. toString () << end}:

X: (0, 0)
Y: (4,3, 8,2)
Z: (3,3, 1.1)

X = y+z:
(7.6, 9.3) = (4.3, 8.2) + (3.3, 1.1)
X= y-z :
(1, 7.1) = (4.3, 8.2 ) −(3.3, 1.1)
Fig. 10.16 Complex class test program (Part 2 of 2)

Blurred answer
Students have asked these similar questions
State whether each of the following is true or false. If false, explain why g) Definitions can appear anywhere in the body of a function.
- in this exercise, please do not include and use string class. The function is using only array notation and manipulation.- string functions such as strlen is not allowed.- it should not have multiple return statements in the same function- there should be no global variable.- the function should not traverse the arrays more than once (e.g. looping through the array once only) A C++ PROGRAM named "changeCase" that takes an array of characters terminating by NULL character (C-string) and a boolean flag of toUpper. If the toUpper flag is true, it will go through the array and convert all lowercase characters to uppercase. Otherwise, it will convert all uppercase to lowercase. For example, if the array is {'H', 'e', 'l', 'l', 'o', '\0'} and the flag is true, then the array will become{'H', 'E', 'L', 'L', 'O', '\0'}. And if the flag is false, the array will become{'h', 'e', 'l', 'l', 'o', '\0'}
- Give an example of short circuit evaluation (in any language) and explain why it is short circuited - Give an example of an overloaded operator (in any language – but not overloaded by a function).  Explain why it is overloaded.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
functions in c programming | categories of function |; Author: Education 4U;https://www.youtube.com/watch?v=puIK6kHcuqA;License: Standard YouTube License, CC-BY