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
(Combining Class Time and Class Date) Combine the modified Time class of Exercise 17.7and the modified Date class of Exercise 17.8 into one class called DateAndTime. (In Chapter 19, we’lldiscuss inheritance, which will enable us to accomplish this task quickly without modifying the existing class definitions.) Modify the tick function to call the nextDay function if the time increments into the next day. Modify functions printStandard and printUniversal to output the dateand time. Write a program to test the new class DateAndTime. Specifically, test incrementing thetime into the next day.
(Payroll System Modification) Modify the payroll system of Figs. 20.9–20.17 to includeadditional Employee subclasses PieceWorker and HourlyWorker. A PieceWorker represents an employee whose pay is based on the number of pieces of merchandise produced. An HourlyWorker represents an employee whose pay is based on an hourly wage and the number of hours worked. Hourlyworkers receive overtime pay (1.5 times the hourly wage) for all hours worked in excess of 40 hours.Class PieceWorker should contain private instance variables wage (to store the employee’swage per piece) and pieces (to store the number of pieces produced). Class HourlyWorker shouldcontain private instance variables wage (to store the employee’s wage per hour) and hours (to storethe hours worked). In class PieceWorker, provide a concrete implementation of method earningsthat calculates the employee’s earnings by multiplying the number of pieces produced by the wageper piece. In class HourlyWorker, provide a concrete…
6. (Immutable Data Class Objects) Built-in types int, float, str and tuple are immutable. Data classes can simulate immutability by designating that objects of the class should be "frozen" after they're created. Client code cannot assign values to the attributes of a frozen object. Research "frozen" data classes, then reimplement this chapter's Complex class as a "frozen" data class. Show that you cannot modify a Complex object after you create it.
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
functions in c programming | categories of function |; Author: Education 4U;https://www.youtube.com/watch?v=puIK6kHcuqA;License: Standard YouTube License, CC-BY