Objective Create a class named Complex to represent complex numbers. Use the main() function and create at least three complex number objects and test all member functions of the Complex class. How to use classes. Create a program named LastnameFirstname19.cpp, that does the following: Note: There is no user input in this assignment. Copy and paste the Complex class definition from the previous assignment to this one. If you did not complete the previous assignment, you will need to create the class definition. Make any corrections to your Complex class based on the feedback provided from Assignment 18. Add the following member functions to the Complex class definition: Additional constructor with 2 parameters Signature: Complex(double realParam, double imaginaryParam) Allows a Complex object to be created with the specified real and imaginary parts. copy constructor Signature: Complex(const Complex ©Complex) Copies the data members from copyComplex to this object. destructor Signature: ~Complex() Output a message in the format: Destructor for: (a + bi) followed by a newline. If the imaginary part is negative (less than 0), print in the format Destructor for: (a - bi) followed by a newline. For example, if real is 6 and imaginary is -3: Destructor for: (6 - 3i)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
  • Objective
    • Create a class named Complex to represent complex numbers. Use the main() function and create at least three complex number objects and test all member functions of the Complex class.
    • How to use classes.
  • Create a program named LastnameFirstname19.cpp, that does the following:
    1. Note: There is no user input in this assignment.
    2. Copy and paste the Complex class definition from the previous assignment to this one.
      • If you did not complete the previous assignment, you will need to create the class definition.
    3. Make any corrections to your Complex class based on the feedback provided from Assignment 18.
    4. Add the following member functions to the Complex class definition:
      1. Additional constructor with 2 parameters
        • Signature: Complex(double realParam, double imaginaryParam)
        • Allows a Complex object to be created with the specified real and imaginary parts.
      2. copy constructor
        • Signature: Complex(const Complex &copyComplex)
        • Copies the data members from copyComplex to this object.
      3. destructor
        • Signature: ~Complex()
        • Output a message in the format: Destructor for: (a + bi) followed by a newline.
        • If the imaginary part is negative (less than 0), print in the format Destructor for: (a - bi) followed by a newline.
          • For example, if real is 6 and imaginary is -3: Destructor for: (6 - 3i)
        • See the example program destructor.cpp
      4. add member function
        • Signature: Complex add(const Complex &addComplex) const
        • Returns a new Complex object that is this object added by the specified object.
        • Formula: (a + bi) + (c + di) = (a + c) + (b + d)i
          • New Real: (a + c)
          • New Imaginary: (b + d)
      5. subtract member function
        • Signature: Complex subtract(const Complex &subtractComplex) const
        • Returns a new Complex object that is this object subtracted by the specified object.
        • Formula: (a + bi) - (c + di) = (a - c) + (b - d)i
          • New Real: (a - c)
          • New Imaginary: (b - d)
      6. multiply member function
        • Signature: Complex multiply(const Complex &multiplyComplex) const
        • Returns a new Complex object that is this object multiplied by the specified object.
        • Formula: (a + bi) * (c + di) = (a*c - b*d) + (b*c + a*d)i
          • New Real: (a*c - b*d)
          • New Imaginary: (b*c + a*d)
      7. divide member function
        • Signature: Complex divide(const Complex &divideComplex) const
        • Returns a new Complex object that is this object divided by the specified object.
        • Formula: (a + bi) / (c + di) = ((a*c + b*d)/(c*c + d*d)) + ((b*c - a*d)/(c*c + d*d))i
          • New Real: ((a*c + b*d) / (c*c + d*d))
          • New Imaginary: ((b*c - a*d) / (c*c + d*d))
      8. Be sure to have a program description at the top and in-line comments.
        • Be clear with your comments and output to the user, so I can understand what the program is doing.

 

Complex Class that should be adjusted

 

class Complex
{
    private:
            double real;
            double imaginary;
    
    public:
    //structure of non parameterized constructor
            Complex()
            {
                real = imaginary = 0.0;
            }
 
         void set(double r, double i)
         {
             real = r; //r initialized during object creation
             imaginary = i; //i initialized during object creation
         }
 
         void print() const
         {
         //statement that adds real part of complex and adds imaginary parts of complex numbers.
             if(imaginary < 0)
                    if(imaginary == -1)
                        cout << "(" << real << " - i" << ")" << endl;
                    else
                        cout << "("<< real << " - " << imaginary << "i" << ")" << endl;
                else
                    if(imaginary == 1)
                        cout << "("<< real << " + i" << ")"<< endl;
                    else
                        cout << "("<< real << " + " << imaginary << "i"  << ")" <<endl;
         }
         //return real
         double getReal() 
         {
             return real;
         }
         //return imaginary
         double getImaginary() 
         {
             return imaginary;
         }
};
// main class
int main()
{
    //printing of first complex statement
    cout<<"Test the constructor"<<endl;
    Complex c1;
    cout<<"Complex number complex1 is: ";
    c1.print();
    //printing of second complex statement
    cout<<"\nTest the one set() function."<<endl;
    Complex c2;
    c2.set(3.3, -4.4);
    cout<<"Complex number complex2 is: ";
    c2.print();
    //printing of third complex statement
    cout<<"\nTest the two get() function."<<endl;
    Complex c3;
    c3.set(5.5, 6.6);
    cout<<"Complex number complex3's real part is: " << c3.getReal() << endl;
    cout<<"Complex number complex3's imaginary part is: " << c3.getImaginary() << endl;

    return 0;
}

Example Output
% make
% ./program
Test the constructor.
Complex number complex1 is: (0 + 0i)
Test the one set() function.
Complex number complex2 is: (3.3 - 4.4i)
Test the two get() functions.
Complex number complex3's real part is: 5.5
Complex number complex3's imaginary part is: 6.6
Transcribed Image Text:Example Output % make % ./program Test the constructor. Complex number complex1 is: (0 + 0i) Test the one set() function. Complex number complex2 is: (3.3 - 4.4i) Test the two get() functions. Complex number complex3's real part is: 5.5 Complex number complex3's imaginary part is: 6.6
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY