(Complex Class) Create a class called Complex for performing arithmetic with complex num- bers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is 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:

a) Adding two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtracting two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Printing Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.


```
#ifndef COMPLEX_H
#define COMPLEX_H

class Complex
{
public:
Complex( double = 0.0, double = 0.0 ); // default constructor
Complex add( const Complex & ); // function add
Complex subtract( const Complex & ); // function subtract
void printComplex(); // print complex number format
void setComplexNumber( double, double ); // set complex number
private:
double realPart;
double imaginaryPart;
}; // end class Complex

#endif
```
```
// Member-function definitions for class Complex.
#include
#include "Complex.h"
using namespace std;

Complex::Complex( double real, double imaginary )
{
setComplexNumber( real, imaginary );
} // end Complex constructor

Complex Complex::add( const Complex &right )
{
return Complex(
realPart + right.realPart, imaginaryPart + right.imaginaryPart );
} // end function add

Complex Complex::subtract( const Complex &right )
{
return Complex(
realPart - right.realPart, imaginaryPart - right.imaginaryPart );
} // end function subtract

void Complex::printComplex()
{
cout << '(' << realPart << ", " << imaginaryPart << ')';
} // end function printComplex

void Complex::setComplexNumber( double rp, double ip )
{
realPart = rp;
imaginaryPart = ip;
} // end function setComplexNumber
```
```
#include
#include "Complex.h"
using namespace std;

int main()
{
Complex a( 1, 7 ), b( 9, 2 ), c; // create three Complex objects

a.printComplex(); // output object a
cout << " + ";
b.printComplex(); // output object b
cout << " = ";
c = a.add( b ); // invoke add function and assign to object c
c.printComplex(); // output object c

cout << '\n';
a.setComplexNumber( 10, 1 ); // reset realPart and
b.setComplexNumber( 11, 5 ); // and imaginaryPart
a.printComplex(); // output object a
cout << " - ";
b.printComplex(); // output object b
cout << " = ";
c = a.subtract( b ); // invoke add function and assign to object c
c.printComplex(); // output object c
cout << endl;
} // end main
```
(1, 7) + (9, 2) = (10, 9)
(10, 1) - (11, 5) = (-1, -4)

Computer Science & Information Technology

You might also like to view...

You can select slide objects in order to delete them simultaneously by pressing the ____ key as you click each object.

A. SHIFT B. ESC C. ALT D. TAB

Computer Science & Information Technology

Clicking a list number or bullet symbol is an efficient way to select the entire bullet point

Indicate whether the statement is true or false

Computer Science & Information Technology

Theme ________ are variations of the current theme

Fill in the blank(s) with correct word

Computer Science & Information Technology

Match the sound elements and attributes to the descriptions

I. bgsound A. Used with embed to determine if the sound will play when the page opens II. embed B. The attribute that determines how many times a sound will play III. autostart C. The element used to play a background sound when a page opens IV. loop D. The attribute used to specify the location of the sound file V. src E. The element used to add a sound and a player

Computer Science & Information Technology