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

For C++ How do I create the implementation portion of the complex class describe

ID: 3772334 • Letter: F

Question

For C++ How do I create the implementation portion of the complex class described in the complex definition file "complex.h"
#ifndef COMPLEX_H
#define COMPLEX_H

class complex {

public:
    complex();
    complex(double a);
    complex(double a,double b);
    complex(int a,int b);
    void print() const;
    void set(double a=0,double b=0);
    void set(int a=0,int b=0);
    double getReal() const;
    double getImag() const;
    void get(double&,double&) const;

   complex operator* (complex&);
    complex operator* (double&);
    complex operator*= (complex&);
    complex operator*= (double&);
    complex operator/ (complex&);
    complex operator/= (complex&);
    complex operator/= (double);
    complex operator/ (double);
    void operator= (const complex&);
    bool operator== (complex&);
    bool operator!=(complex &c);
    friend std::istream &operator>> (std::istream &in, complex& c);
    friend std::ostream &operator<<(std::ostream &out, complex c);

    complex conj() const;
    double norm() const;
    double modulus() const;

private:
    double real;
    double imag;
};

#endif // COMPLEX_H

Explanation / Answer

Using the Complex Library

To use the complex library, include the header file complex.h in your program, and compile and link with the -library=complex option.

Type of complex

The complex arithmetic library defines one class: class complex. An object of class complex can hold a single complex number. The complex number is constructed of two parts:

The value of an object of class complex is a pair of double values. The first value represents the real part; the second value represents the imaginary part.

#include <stdio.h>  

#include <complex.h>

int main()

{

    int negNum;

    printf("Calculate negative square roots: "

           "Enter negative number:");

    scanf("%d", &negNum);

    double complex negSqrt = csqrt(negNum);

    double Real = creal(negSqrt);

    double Imag = cimag(negSqrt);

    printf(" Real part %f, imaginary part %f"

           ", for negative square root.(%d)",

           Real, Imag, negNum);

    return 0;

}

Function Descriptions

The function descriptions for the complex library are organized as follows:

constructors and conversion operators

include constructors and conversion operators for complex variables.

cartesian and polar coordinate functions

include descriptions of abs() , arg() , conj(),  imag(),  norm() ,  polar() , and  real() .

exponential, logarithmic, power, and square root functions

include descriptions of exp() ,  log() ,  pow() , and  sqrt() .

trigonometric and hyperbolic functions

include descriptions of sin() ,  cos() ,  sinh() , and  cosh() .

operators

include the operators available for the complex library ( + ,  * ,  == , and so on).

complex I/O functions

provide complex I/O, that is, the insertion and extraction operators << and  >> .

Each set of function descriptions includes the following information:  

In the following descriptions, specific diagnostic information is given for a function or group of functions, where appropriate. However, more general diagnostic information is not included in this book. Also, some of the complex library functions call SAS/C math library functions. If you find you need more information on error handling for math functions, or if you need more information about functions called by the complex library functions, see the SAS/C Library Reference. If you want to use signal handlers to trap overflows, see the SAS/C Library Reference, Volume 1 also.

complex()

Constructors and Conversion Operators

SYNOPSIS

DESCRIPTION

The following constructors are defined for class complex .

complex()

enables you to declare complex variables without initializing them. File-scope complex variables declared without an initializer have an initial value of (0,0) ; other uninitialized complex variables have an undefined initial value.

complex(double real, double imag = 0.0)

allows explicit initialization of complex variables. For example, the following two statements are valid:

This constructor also allows for implicit conversion from arithmetic types to complex values. For example, the following two statements are valid:

Using this constructor, you can also create complex values within expressions. Here is an example:

Complex Operators

Operators for the C++ Complex Library

SYNOPSIS

DESCRIPTION

The usual arithmetic operators, comparison operators, and assignment operators are overloaded for complex numbers. The usual precedence relations among these operators are in effect. In the descriptions below, a and  b are of type  complex and  d is of type  double .

  Arithmetic operators

The following are the arithmetic operators.

a + b

is the arithmetic sum of a and  b .

-a

is the arithmetic negation of a .

a - b

is the arithmetic difference of a and  b .

a * b

is the arithmetic product of a and  b .

a/b and  a/d

are the arithmetic quotient of a and  b or  a and  d .

  Comparison operators

The following are the comparison operators.

a == b

is nonzero if a is equal to  b ; it is zero otherwise.

a != b

is nonzero if a is not equal to  b ; it is zero otherwise.

  Assignment operators

The following are the assignment operators.

a += b

assigns to a the arithmetic sum of itself and  b .

a -= b

assigns to a the arithmetic difference of itself and  b .

a *= b

assigns to a the arithmetic product of itself and b .

a /= b and  a /= d

assign to a the arithmetic quotient of themselves and  b or  d .

CAUTION

The assignment operators do not yield a value that can be used in an expression. For example, the following construction is not valid:

operator << and operator >>

Complex I/O Functions

SYNOPSIS

DEFINITION

The following functions provide insertion and extraction capabilities for complex numbers.

ostream& operator << (ostream& os,  
complex c)

writes a complex number c to  os . The output is formatted in the following manner:

where real-part and imag-part are the real and imaginary parts of the complex number, respectively. Both real-part and imag-part are formatted as doubles. For more information, refer to the descrip tion of the operator <<(ostream&, double) in class ostream . The formatting of real-part and imag-part is controlled by flags associated with the stream. See enum format_state .

istream& operator >>(istream& is,  
complex& c)

reads a formatted complex number from is into  c . The  istream should contain the complex number to be read in one of these formats:

where real-part and imag-part are the real and imaginary parts of the complex number, respectively. Both real-part and imag-part should be formatted as double s. For more information, refer to the description of the  operator >>(istream&, double&) in class istream . The formatting of real-part and imag-part is controlled by flags associated with the stream. See enum format_state .

Remember the following when performing complex I/O:

If your input variable represents a real number such as 5e-2 or (502), the >> operator interprets it as a complex number with an imaginary part of 0.

  class complex {  
      double Real, Imag;  
  };  
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