Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ 2) Complex Class A complex number is of the form a+ bi where a and b are rea

ID: 3682945 • Letter: C

Question

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 complex class yourself.) The members of this class are as follows: Private data members: real and imag of type double to represent the complex number (real + imag i) Public member functions: Default constructor that will initialize real and imag to zero void input (istream&)/Input values from the specified stream for real and imag void output (ostream&) I Output the Complex number to the specified stream in this form e.g., 2.3 4.6i double getReal ();//Accessor method that returns the value of data member real double getImag O; l/Accessor method that returns the value of data member imag void setReal (double) I Mutator method that sets the value of real void setImag (double); I/ Mutator method that sets the value of imaçg Before writing any code, you and your partner should discuss what the code for this class will look like. Make sure you both have a good idea of what you need to do before you start writing code for this class. Write a main) driver function that calls the appropriate member functions to do the following: Declare two Complex objects: c1 and c2 Ponae the en t compe value for tw objects cl and c2

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <string>

#include "Complex.hpp"
#include <algorithm>

using namespace std;

void stretch2();
int main()
{
    srand(static_cast<unsigned int>(time(nullptr)));
stretch2();

    return 0;
}


void stretch2()
{
    Complex c1;
    Complex c2;

    cout << "Enter values for real and imaginary coefficients: ";
    c1.input(cin);
    cout << "Enter values for real and imaginary coefficients: ";
    c2.input(cin);

    c1.output(cout);
    cout << endl;
    c2.output(cout);
    cout << endl;

    double temp;
    cout << "Enter a new value for the real coefficient: ";
    cin >> temp;

    c2.setReal(temp);
    cout << "The new real coefficient is " << c2.getReal() << endl;
    c2.output(cout);
    cout << endl;

}

Complex.cpp
#include <iostream>
#include <iomanip>

#include "Complex.hpp"

using namespace std;

Complex::Complex()
    : real(0)
    , imag(0)
{
  
}


void Complex::input(istream& in)
{
    in >> real;
    in >> imag;
}

void Complex::output(ostream& out)
{
    out << fixed << setprecision(1) << real << " + " << fixed << setprecision(1) << imag << "i";
}

double Complex::getReal()
{
    return real;
}

double Complex::getImag()
{
    return imag;
}

void Complex::setReal(double r)
{
    real = r;
}

void Complex::setImag(double i)
{
    imag = i;
}


Complex.hpp

#ifndef COMPLEX
#define COMPLEX
#include <iostream>

class Complex
{
private:
    double real;
    double imag;
public:
    Complex();
    void input(std::istream&);
    void output(std::ostream&);
    double getReal();
    double getImag();
    void setReal(double);
    void setImag(double);
};

#endif COMPLEX

sample output

Enter values for real and imaginary coefficients: 17.4 13.9                                                                                                 
Enter values for real and imaginary coefficients: 10.2 16.81                                                                                                
17.4 + 13.9i                                                                                                                                                
10.2 + 16.8i                                                                                                                                                
Enter a new value for the real coefficient: 22.2                                                                                                            
The new real coefficient is 22.2                                                                                                                            
22.2 + 16.8i                                                                                                                                                

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote