Here is the prompt of what I have to do:
Overview
In this assignment, implement a class called Complex, which will be used to represent a complex number. A complex number is one that is comprised of a real part and an imaginary part. It is typically represented in the form:
a + bi
where a is the real part and and bi is the imaginary part with i having the property i2 = -1. Functionality will be added to the Complex class by writing methods so that arithmetic, relational and output operations can be performed.
class Complex
Use the following class definition:
class Complex
{
public:
Complex(); //default constructor
Complex( int, int );
void display();
void displayComplex();
void setReal( int );
void setImaginary( int );
void setComplex( int, int );
int getReal();
int getImaginary();
void add( int, int );
void subtract( int, int );
void multiply( int, int );
void add( Complex );
void subtract( Complex );
void multiply( Complex );
bool isEqual( int, int );
bool isEqual( Complex );
private:
int real;
int imaginary;
};
Data Members
The Complex class contains two private data members. They are:
real - an integer to hold the real part of the complex number
imaginary - an integer to hold the imaginary part of the complex number
For example, the following Complex numbers would be represented as:
3+7i ==> real = 3, imaginary = 7
4-2i ==> 4 + (-2i) ==> real = 4, imaginary = -2
Constructors
There are two constructors for this class.
The first one is the default constructor. It takes no arguments. Its sole purpose is to create an object with both the real and imaginary portions set to 1.
The second constructor for the Complex class should take two integer arguments that will be used to initialize the two data members. The arguments passed to the constructor are: the real portion of the complex number, and the imaginary portion of the complex number, respectively.
--------------------------------------------------------------------------------
Display Methods
void display()
This method will display the Complex object in the form (real, imaginary). If the imaginary data member contains 0, then the Complex object should display in the form (real).
void displayComplex()
This method will display the Complex object in the form real + imaginary i. If the imaginary data member contains a negative number, then the imaginary portion of the complex number should be enclosed in parenthesis. For example:
real = 3, imaginary = 7 will display 3 + 7i
real = 4, imaginary = -2 will display 4 + (-2i)
--------------------------------------------------------------------------------
Access Methods
void setReal( int )
This method will use the passed in argument to initialize the real portion of the Complex object.
void setImaginary( int )
This method will use the passed in argument to initialize the imaginary portion of the Complex object.
void setComplex( int, int )
This method will use the passed in arguments to initialize a Complex object. The first argument is used to initialize the real data member, while the second argument is used to initialize the imaginary data member.
int getReal()
This method returns the real portion of the Complex object.
int getImaginary()
This method returns the imaginary portion of the Complex object.
--------------------------------------------------------------------------------
Arithmetic Operations
Addition 1: void add( int, int );
Addition 2: void add( Complex );
Both of the add methods will add two Complex objects. The first version has the complex number passed in as its separate real and imaginary parts, while the second version has the complex number passed in as a Complex object.
To add two complex numbers, simply add the two real portions together and add the two imaginary portions together. For example:
( a + bi ) + ( c + di ) = ( a + c ) + ( b + d ) i
( 3 + 7i ) + ( 4 + -2i ) = ( 3 + 4 ) + ( 7 + -2 ) i
= 7 + 5i
Subtraction 1: void subtract( int, int );
Subtraction 2: void subtract( Complex );
Both of the subtract methods will subtract two Complex objects. The first version has the complex number passed in as its separate real and imaginary parts, while the second version has the complex number passed in as a Complex object.
To subtract two complex numbers, simply subtract the two real portions and subtract the two imaginary portions. For example:
( a + bi ) - ( c + di ) = ( a - c ) + ( b - d ) i
( 3 + 7i ) + ( 4 + -2i ) = ( 3 - 4 ) + ( 7 - -2 ) i
= -1 + 9i
Multiplication 1: void multiply( int, int );
Multiplication 2: void multiply( Complex );
Both of the multiply methods will multiply two Complex objects. The first version has the complex number passed in as its separate real and imaginary parts, while the second version has the complex number passed in as a Complex object.
To multiply two complex numbers:
( a + bi ) * ( c + di ) = ( ac - bd ) + ( ad + bc ) i
( 3 + 7i ) * ( 4 + -2i ) = ( 3*4 - 7*-2 ) + ( 3*-2 + 7*4 ) i
= ( 12 - -14 ) + ( -6 + 28 ) i
= 26 + 22i
Relational Operation
Equality 1: bool isEqual( int, int )
Equality 2: bool isEqual( Complex )
Both of the isEqual methods will test if two Complex objects are equal. The first version has the complex number passed in as its separate real and imaginary parts, while the second version has the complex number passed in as a Complex object.
Complex numbers are equal if and only if the real number portions are equal and the imaginary portions are equal.
--------------------------------------------------------------------------------
main()
In main(), create 5 Complex objects.
Complex Object 1 should be created with the default constructor.
Complex Object 2 should be created with the second constructor and should have a real value of 3 and an imaginary value of 7.
Complex Object 3 should be created with the second constructor and should have a real value of 4 and an imaginary value of -2.
Complex Object 4 should be created with the second constructor and should have a real value of 8 and an imaginary value of 0.
Complex Object 5 should be created with the default constructor.
Use the 5 Complex objects to perform the following operations. The output that is produced by each step should have informative labels such as "The default constructor produces: ".
Display the contents of Complex Objects 1, 2, and 3 using the displayComplex and display methods.
Display the real and imaginary parts of Complex Objects 4 and 5 by using the get access methods.
Now test the arithmetic methods that were written. They should all display what is being added/subtracted/etc... and the result using both display methods. For example:
The sum of 3 + 7i and 4 + (-2i) is 7 + 5i or (7, 5)
Add Complex Object 3 to Complex Object 2 by using the add method that takes a Complex object as its argument.
Add (15, 3) to Complex Object 1 by using the add method that takes two integers as its arguments.
Subtract Complex Object 3 from Complex Object 5 by using the subtract method that takes a Complex object as its argument.
Subtract (9, 0) from Complex Object 2 by using the subtract method that takes two integers as its arguments.
Multiply Complex Object 2 by Complex Object 3 by using the multiply method that takes a Complex object as its argument.
Multiply Complex Object 4 by Complex Object 3 by using the multiply method that takes two integers as its arguments. Use the getReal and getImaginary methods to get the two values to pass to the multiply method.
Now test the relational method isEqual that was written. The two values that are being compared should be displayed using the displayComplex method. For each comparison, display whether the two complex values are equal or not equal. For example:
Comparing 2 + 24i and 4 + (-2i)
== False
!= True
Compare Complex Object 2 and Complex Object 3 by using the isEqual method that takes a Complex object as its argument.
Compare Complex Object 5 to itself by using the isEqual method that takes a Complex object as its argument.
Compare Complex Object 4 to (15, 6) by using the isEqual method that takes two integers as its arguments.
--------------------------------------------------------------------------------
Program Notes
In order to receive full credit for this assignment, you must meet the following requirements:
Each method must have a documentation box like a function. You may have one documentation box to cover the overloaded constructors and methods, but you MUST mention all of the overloaded methods in the box.
--------------------------------------------------------------------------------
Output
The default constructor produces:
1 + 1i or (1, 1)
The constructor with values produces:
3 + 7i or (3, 7)
4 + (-2i) or (4, -2)
The getReal and getImaginary methods produces:
The real portion of d4 is 8
The imaginary portion is 0
The real portion of d5 is 1
The imaginary portion is 1
The add method produces:
The sum of 3 + 7i and 4 + (-2i) is 7 + 5i or (7, 5)
The sum of (1, 1) and (15, 3) is (16, 4) or 16 + 4i
The subtract method produces:
The difference of 1 + 1i minus 4 + (-2i) is -3 + 3i or (-3, 3)
The difference of (7, 5) minus (9) is (-2, 5) or -2 + 5i
The multiply method produces:
The product -2 + 5i times 4 + (-2i) is 2 + 24i or (2, 24)
The product of (8) times (4, -2) is (32, -16) or 32 + (-16i)
The isEqual method produces:
Comparing 2 + 24i and 4 + (-2i)
== False
!= True
Comparing -3 + 3i and -3 + 3i
== True
!= False
Comparing (32, -16) and (15, 6)
== False
!= True
I really have no idea how to do it. But this is what I have done so far
#include <iomanip>
#include <iostream>
using namespace std;
// Class Definition
class Complex
{
public:
Complex(); //default constructor
Complex( int, int );
void display();
void displayComplex();
void setReal( int );
void setImaginary( int );
void setComplex( int, int );
int getReal();
int getImaginary();
void add( int, int );
void subtract( int, int );
void multiply( int, int );
void add( Complex );
void subtract( Complex );
void multiply( Complex );
bool isEqual( int, int );
bool isEqual( Complex );
private:
int real;
int imaginary;
};
int main()
{
cout << endl;
system("pause");
return 0;
}
/******************************
METHODS GO BELOW HERE
******************************/
Complex::Complex()
{
}
Complex::Complex( int, int )
{
}
void Complex::display()
{
}
void Complex::displayComplex()
{
}
void Complex::setReal( int )
{
}
void Complex::setImaginary( int )
{
}
void Complex::setComplex( int, int )
{
}
int Complex::getReal()
{
}
int Complex::getImaginary()
{
}
void Complex::add( int, int )
{
}
void Complex::subtract( int, int )
{
}
void Complex::multiply( int, int )
{
}
void Complex::add( Complex )
{
}
void Complex::subtract( Complex )
{
}
void Complex::multiply( Complex )
{
}
bool Complex::isEqual( int, int )
{
}
bool Complex::isEqual( Complex )
{
}