Concept explainers
(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
- Modify the class to enable input and output of complex numbers via overloaded >> and << operators, respectively (you should remove the toString function from the class)
- Overload the multiplication operator to enable multiplication of two complex numbers as in algebra.
- 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>)
- // Fig. 10.14 : Complex.h
- // Complex class definition.
- #include <string>
- #ifndef COMPLEX_H
- #define COMPLEX_H
- Class Complex {
- Public :
- Explicit Complex (double = 0.0, double = 0.0) ; // constructor
- Complex operator+(const Complex&) const; // addition
- Complex operator-(const Complex&) const; // subtraction
- Std:: string toString () const:
- Private:
- Double real : // real part
- Double imaginary ; // imaginary part
- };
- #endif
Fig. 10.14 Complex class deginition.
- // Fig. 10.15 : Complex.cpp
- // Complex class member-function definitions.
- #include <string>
- #include “Complex.h” // Complex class definition
- Using namespace std;
- // Constructor
- Complex: : Complex (double realPart, double imaginaryPart)
- : reak {real Part}, imaginary {imaginaryPart} { }
- // addition operator
- Complex Complex : : operator+ (const Complex& operand2) const {
- Return Complex {real +operand2. Real, imaginary+ operand2. Imaginary};
- }
- // subtraction operator
- Complex Complex : : operator-(const Complex& opetand2) const {
- Return Complex { real − operand 2.real, imaginary − operand2. Imaginary } :
- }
- // return string representation of a complex object in the form: (a, b)
- String Complex : : to String () const {
- Return “(“s +to_string(real) + “, “s + to_string(imaginary) + “)”s;
- }
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)
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
C++ How to Program (10th Edition)
- 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.arrow_forward(Dynamic Binding vs. Static Binding) Distinguish between static binding and dynamicbinding. Explain the use of virtual functions and the vtable in dynamic bindingarrow_forward(Operator Overloads in Templates) Write a simple function template for predicate functionisEqualTo that compares its two arguments of the same type with the equality operator (==) andreturns true if they are equal and false otherwise. Use this function template in a program thatcalls isEqualTo only with a variety of fundamental types. Now write a separate version of the program that calls isEqualTo with a user-defined class type, but does not overload the equality operator.What happens when you attempt to run this program? Now overload the equality operator (withthe operator function) operator==. Now what happens when you attempt to run this program?arrow_forward
- (Complex Class) Consider class Complex shown in Figs. 18.14–18.16. The class enables operations on so-called complex numbers. These are numbers of the form realPart + imaginaryPart* i, where i has the valuea) Modify the class to enable input and output of complex numbers via overloaded >> and<< operators, respectively (you should remove the print function from the class).b) Overload the multiplication operator to enable multiplication of two complex numbersas in algebra.c) Overload the == and != operators to allow comparisons of complex numbersarrow_forward(Modifying Class GradeBook) Modify class GradeBook (Figs. 16.11–16.12) as follows:a) Include a second string data member that represents the course instructor’s name.b) Provide a set function to change the instructor’s name and a get function to retrieve it.c) Modify the constructor to specify course name and instructor name parameters.d) Modify function displayMessage to output the welcome message and course name,then the string "This course is presented by: " followed by the instructor’s name.Use your modified class in a test program that demonstrates the class’s new capabilities.arrow_forward.“Dangling and wild pointers are known to be problems with pointers”. Justify the given statement with the helpof suitable examplearrow_forward
- (Time Class Modification) It would be perfectly reasonable for the Time class of Figs. 17.4–17.5 to represent the time internally as the number of seconds since midnight rather than the threeinteger values hour, minute and second. Clients could use the same public methods and get thesame results. Modify the Time class of Fig. 17.4 to implement the time as the number of secondssince midnight and show that there is no visible change in functionality to the clients of the class.[Note: This exercise nicely demonstrates the virtues of implementation hiding.]arrow_forward(Enhancing Class Time) Modify the Time class of Figs. 17.4–17.5 to include a tick memberfunction that increments the time stored in a Time object by one second. Write a program that teststhe tick member function in a loop that prints the time in standard format during each iterationof the loop to illustrate that the tick member function works correctly. Be sure to test the followingcases:a) Incrementing into the next minute.b) Incrementing into the next hour.c) Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).arrow_forwardQ2) Write a C# class Factors having function static int CountOddFactors(int n) that would return count of odd factors of parameter n. For instance, odd factors of 20 are: 1 and 5, and odd factors of 30 are 1, 3, 5 and 15. Call this static function in main function that is in another class. SUBJECT:CSHARP(VISUAL PROGRAMMING)arrow_forward
- c++ exercise: 1- Implement the class “cylinder” with member variables radius and height, whichare private of type double. Define the global constant PI=3.1415 and use it incalculating the volume of the cylinder (PI*radius*radius*height).Implement in the class cylinder the following functions:a- A default constructor with default values of one.b- One constructor with two arguments. This constructor should check that thevariable is positive and does not exceed 20, otherwise the variable will beassigned its default value of one.c- A reader for each variable.d- A writer for each variable.e- A reader and a writer for diameter.f- A member function “volume” which calculates the volume of a cylinder.g- A member function “print” which prints the radius, diameter, and height of acylinder.h- A member function display_name() which displays “cylinderarrow_forward(Payroll System Modification) Modify the payroll system of Figs. 20.9–20.17 to includeprivate data member birthDate in class Employee. Use class Date from Figs. 18.6–18.7 to representan employee’s birthday. Assume that payroll is processed once per month. Create a vector of Employee references to store the various employee objects. In a loop, calculate the payroll for each Employee (polymorphically), and add a $100.00 bonus to the person’s payroll amount if the currentmonth is the month in which the Employee’s birthday occurs.arrow_forward(Polymorphic Banking Program Using Account Hierarchy) Develop a polymorphic banking program using the Account hierarchy created in Exercise 19.10. Create a vector of Accountpointers to SavingsAccount and CheckingAccount objects. For each Account in the vector, allowthe user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As youprocess each Account, determine its type. If an Account is a SavingsAccount, calculate the amountof interest owed to the Account using member function calculateInterest, then add the interestto the account balance using member function credit. After processing an Account, print the updated account balance obtained by invoking base-class member function getBalance.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education