C++ How to Program (10th Edition)
C++ How to Program (10th Edition)
10th Edition
ISBN: 9780134448237
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
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
2.68♦♦ Write code for a function with the following prototype: * Mask with least signficant n bits set to 1 * Examples: n = 6 -> 0x3F, n = 17-> 0x1FFFF * Assume 1 <= n <= w int lower_one_mask (int n); Your function should follow the bit-level integer coding rules Be careful of the case n = W.
Hi-Volt Components You are the IT manager at Hi-Voltage Components, a medium-sized firm that makes specialized circuit boards. Hi-Voltage's largest customer, Green Industries, recently installed a computerized purchasing sys- tem. If Hi-Voltage connects to the purchasing system, Green Industries will be able to submit purchase orders electronically. Although Hi-Voltage has a computerized accounting system, that system is not capable of handling EDI. Tasks 1. What options does Hi-Voltage have for developing a system to connect with Green Industries' pur- chasing system? 2. What terms or concepts describe the proposed computer-to-computer relationship between Hi-Voltage and Green Industries? why not? 3. Would Hi-Voltage's proposed new system be a transaction processing system? Why or 4. Before Hi-Voltage makes a final decision, should the company consider an ERP system? Why or why not?
Consider the following expression in C: a/b > 0 && b/a > 0.What will be the result of evaluating this expression when a is zero? What will be the result when b is zero? Would it make sense to try to design a language in which this expression is guaranteed to evaluate to false when either a or b (but not both) is zero? Explain your answer
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