Question

2. Enter, compile, and run Program 11.1.
he same name as the ave a return type nction as a member Chapter 11 Program 11.1 of this function the parameters include <ios
626 Introduction to Classes int main() Complex a, b, C(6.8,9.7); // declare 3 objects 1/ Assign new values to object bs data
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

class Complex {
private:
    double realPart;
    double imaginaryPart;
public:
    Complex(double=0.0, double=0.0);
    void assignNewValues(double, double);
    void showComplexValues();
};

Complex::Complex(double real, double imag) {
    realPart = real;
    imaginaryPart = imag;
}

void Complex::assignNewValues(double real, double imag) {
    realPart = real;
    imaginaryPart = imag;
}

void Complex::showComplexValues() {
    char sign = '+';
    if (imaginaryPart < 0) sign = '-';
    cout << "The complex number is " << realPart << ' ' << sign << ' ' << abs(imaginaryPart) << "i\n";
}

int main() {
    Complex a, b, c(6.8, 9.7);

    b.assignNewValues(5.3, -8.4);
    a.showComplexValues();
    b.showComplexValues();
    c.showComplexValues();
    return 0;
}

The complex number is 0 + Bi The complex number is 5.3 - 8.4i The complex number is 6.8 + 9.7i

Add a comment
Know the answer?
Add Answer to:
2. Enter, compile, and run Program 11.1. he same name as the ave a return type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    C++ //add as many comments as possible 5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real...

    C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this...

  • I have Majority of the code written but I just need to implement the <,>,and =...

    I have Majority of the code written but I just need to implement the <,>,and = sign in the code. I have posted the instructions for the whole code. I will add what I have at the end. Objectives: Implement basic class concepts in Java Implement inheritance with super and sub-classes Implement basic polymorphism concepts Problem: The TARDIS has been infected by a virus which means it is up to Doctor Who to manually enter calculations into the TARDIS interface....

  • A complex number is a number in the form a + bi, where a and b...

    A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c + di)...

  • redo program 1(what I have type on bottom to meet the following requirements) it must not...

    redo program 1(what I have type on bottom to meet the following requirements) it must not interact with the user to receive inputs for efficiency in testing programs, while meeting the following requirements. in c++ Employee class Change the struct data type Employee to a class with all preexisting attributes(, or data members, or data fields) and an additional data member named count that keeps track of the number of Employee objects. Implement the interface that include the following functionalities...

  • this is the essay.h file this is the gradedActivity.h file this is the gradeActivity.cpp file Can...

    this is the essay.h file this is the gradedActivity.h file this is the gradeActivity.cpp file Can someone help me write the code for this in c++. I'm really struggling with this. If you can include comments and a screenshot of the test run it would be greatly appreciated Part 1: Implement Essay.h and demonstrate the Essay class The Essay class is derived from the GradedActivity class presented in chapter 15. The Essay class determines the grade a student receives on...

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT