Question

Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

Create the header file named “Complex.h” that contains the following class:

The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain:

Private double field named real.

Private double field named imaginary.

Public default constructor that assigns 1 to real and 0 to imaginary.

Public overloaded constructor that takes a double as a parameter named real. It assigns real to

the real field and 0 to the imaginary field.

Public overloaded constructor that takes two doubles as a parameter named real and imaginary

respectively. It assigns real to the real field and imaginary to the imaginary field.

Public copy constructor.

Public overloaded assignment operator.

Public empty destructor.

Public constant get method for real.

Public constant get method for imaginary.

Public set method for real.

Public set method for imaginary.

Public double constant method named Modulus() that takes no parameters. It returns the

complex modulus (or complex norm) of the object.

Public Complex reference constant method named Complement() that takes no parameters.

It returns the complement of the object.

Friend Complex reference function named Sum() that takes two constant Complex references

as parameters. It returns the sum of the parameters.

Friend Complex reference function named Difference() that takes two constant Complex

references as parameters. It returns the difference of the parameters.

Friend Complex reference function named Product() that takes two constant Complex ref-

erences as parameters. It returns the product of the parameters.

Friend Complex reference function named Quotient() that takes two constant Complex

references as parameters. It returns the quotient of the parameters only if the divisor parameter is not 0 (i.e. the modulus is not 0); otherwise, it throws the exception “Error: Cannot divide by zero.”.

Public string constant method named ToString() that takes no parameters. It returns a string in the format:

real + imaginary i

Friend overloaded ostream operator. It displays a Complex object in the same format as

ToString().

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
double realPart,imaginaryPart;
public:
Complex();
Complex(double real, double imaginary);
void ToString();
void setComplexNumber(double, double);
Complex getComplexNumber();
friend Complex add(const Complex &c1,const Complex &c2);
friend Complex subtract(const Complex &c1,const Complex &c2);
friend Complex multiply(const Complex &c1,const Complex &c2);
friend Complex divide(const Complex &c1,const Complex &c2);
Complex & operator=(const Complex &c);
~Complex();
Complex(const Complex& src);
double getReal()const;
double getImaginary()const;
void setReal(double realPart);
void setImagimary(double imaginaryPart);
friend std::ostream& operator<<(std::ostream& dout, const Complex&);
  
};
#endif // COMPLEX_H

_________________

Complex.cpp

#include <iostream>
using namespace std;
#include "Complex.h"
Complex::Complex(double real, double imaginary)
{
setComplexNumber(real, imaginary);
}
Complex add(const Complex &c1,const Complex &c2)
{
int a=c1.realPart;
int b=c1.imaginaryPart;
int c=c2.realPart;
int d=c2.imaginaryPart;
int realP=(a + c);
int imgP=(b+d);
Complex comp(realP,imgP);
return comp;
}
Complex subtract(const Complex &c1,const Complex &c2)
{
int a=c1.realPart;
int b=c1.imaginaryPart;
int c=c2.realPart;
int d=c2.imaginaryPart;
int realP=(a - c);
int imgP=(b-d);
Complex comp(realP,imgP);
return comp;
}

Complex multiply(const Complex &c1,const Complex &c2)
{
int a=c1.realPart;
int b=c1.imaginaryPart;
int c=c2.realPart;
int d=c2.imaginaryPart;
int realP=(a * c);
int imgP=(b*d);
Complex comp(realP,imgP);
return comp;
}

Complex divide(const Complex &c1,const Complex &c2)
{
int a=c1.realPart;
int b=c1.imaginaryPart;
int c=c2.realPart;
int d=c2.imaginaryPart;
  
int realP=((a*c)+(b*d))/((c*c)+(d*d));  
if(c2.imaginaryPart==0)
{
throw "Error: Cannot divide by zero.\n";
}
else
{
int imgP=((b*c)-(a*d))/((c*c)+(d*d));  
Complex comp(realP,imgP);
return comp;
}

  
}
void Complex::ToString()
{
if(imaginaryPart<0)
cout << "(" << realPart << "-" << (-imaginaryPart) << "i )";
else
cout << "(" << realPart << "+" << imaginaryPart << "i )";
}
void Complex::setComplexNumber(double rp, double ip)
{
realPart = rp;
imaginaryPart = ip;
}
Complex::Complex()
{
}
Complex Complex::getComplexNumber()
{
Complex comp(this->realPart,this->imaginaryPart);
return comp;
}

Complex & Complex::operator=(const Complex &c)
{
this->realPart=c.realPart;
this->imaginaryPart=c.imaginaryPart;
return *this;
}

Complex::~Complex()
{
  
}

Complex::Complex(const Complex& src)
{
this->realPart=src.realPart;
this->imaginaryPart=src.imaginaryPart;
}

double Complex::getReal()const
{
return realPart;
}
double Complex::getImaginary()const
{
return imaginaryPart;
}
void Complex::setReal(double realPart)
{
this->realPart=realPart;
}
void Complex::setImagimary(double imaginaryPart)
{
this->imaginaryPart=imaginaryPart;
}
  
// Function implementation which display Fraction numbers by using the operator overloading '<<'
ostream& operator<<(ostream& dout, const Complex& c)
{

if(c.imaginaryPart<0)
dout << "(" << c.realPart << "-" << (-c.imaginaryPart )<< "i )";
else
dout << "(" << c.realPart << "+" << c.imaginaryPart << "i )";
  
return dout;
}

_____________________

main.cpp

#include <iostream>
using namespace std;
#include "Complex.h"
int main()
{

double real1,real2,imag1,imag2;
cout<<"Complex Number#1"<<endl;
cout<<"Enter real Part :";
cin>>real1;
cout<<"Enter imaginary Part :";
cin>>imag1;
Complex c1(real1,imag1);
  
cout<<"Complex Number#2"<<endl;
cout<<"Enter real Part :";
cin>>real2;
cout<<"Enter imaginary Part :";
cin>>imag2;
Complex c2(real2,imag2);
  
Complex c;
c1.ToString();
cout << "+";
c2.ToString();
cout << "=";
c =add(c1,c2);
c.ToString();
  
cout << "\n";
  
c1.ToString();
cout << "-";
c2.ToString();
cout << "=";
c = subtract(c1,c2);
c.ToString();
  
cout << "\n";
  
c1.ToString();
cout << "*";
c2.ToString();
cout << "=";
c = multiply(c1,c2);
c.ToString();
  
cout << "\n";
  
//c1.ToString();
cout<<c1;
cout << "/";
c2.ToString();
cout << "=";
try
{
c = divide(c1,c2);
c.ToString();
}
catch (const char* msg)
{
cerr << msg << endl;
}
}

____________________

Output:

/ComplexAddSubMulDivException Complex Number#1 Enter real Part :4 Enter imaginary Part :5 omplex Number#2 Enter real Part 6 E

__________________Thank YOu

Add a comment
Know the answer?
Add Answer to:
Create the header file named “Complex.h” that contains the following class: The class Complex represents a...
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
  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • 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...

  • Game Development: Uno For this assignment you will be creating the game of Uno (See the...

    Game Development: Uno For this assignment you will be creating the game of Uno (See the accompanying pdf for the instructions of the game). Your version will adhere to all the rules except that only the next player can issue a challenge against the previous player in regards to penalties, and your games must have at least three (3) players and at most nine (9) players. To begin, you must create the class Card which contains: Private string field named...

  • Consider the following C struct that represents a complex number. struct complex {    double real;    double...

    Consider the following C struct that represents a complex number. struct complex {    double real;    double imaginary; }; (a) [20 points/5 points each] Change this struct into a class. Make the member variables private, and add the following to the class: A default constructor that initializes the real and imaginary parts to 0. A constructor that allows initialization of both real and imaginary parts to any double value. A public member function that returns the magnitude of the complex number....

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 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 of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...

    put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following: A private double 1D array instance variable named numbers. A constructor that takes one parameter, a 1D double array. The constructor should throw an IllegalArgumentException with the message "Array length of zero" if the parameter has a length of 0. If the exception is not thrown, the constructor should create the numbers array and copy every element from the parameter into the numbers instance...

  • A grocery list is provided below: Create three different classes named Milk. Bread, and Eggs, where...

    A grocery list is provided below: Create three different classes named Milk. Bread, and Eggs, where each has the following members: private data member "unit_price private data member "quantity". Default and overloaded constructors Provide properties for the private data members public total_price method which returns the total price (e.g., unit_price times quantity) A ToString() method that returns the info of the object Create a class named Grocery, which has the following members my_milk of type Milk my_bread of type Bread...

  • Use java, you are to create a brand new base class called NSpaceCoordinate in a file...

    Use java, you are to create a brand new base class called NSpaceCoordinate in a file called nspace/NSpaceCoordinate.java. It has the following requirements: 1. It must have a single private field, an ArrayList specialized (using the generics feature discussed in the lecture) to holding values of type double (actually, Double). 2. Provide a constructor that takes a double[] and sets up the ArrayList field to have those same values. 3. Provide a method called getNthCoordinate(int i). 4. Implement the Comparable...

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

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
Active Questions
ADVERTISEMENT