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 subtract( int, int );
void multiply( int, int );

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
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 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 subtract( int, int );
void multiply( int, int );

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::subtract( int, int )
{

}

void Complex::multiply( int, int )
{

}

{

}

void Complex::subtract( Complex )
{

}

void Complex::multiply( Complex )
{

}

bool Complex::isEqual( int, int )
{

}

bool Complex::isEqual( Complex )
{

}``````

## All 3 Replies

I really have no idea how to do it.

Not paying attention in class, then?

But this is what I have done so far

Which is to say nothing, because all of the member functions are empty. Writing the declarations doesn't really count as doing anything, much as I'd like to debate the necessity of good class interface design.

There's really no trick to it. Take each function in turn and implement it. For example, the function int Complex::getReal() returns the real part of the complex number.

So, you need to write some code that returns the real of the object.

Here is my updated code...I just need to fix the int main because it isn't displaying the way that I thought it would.

``````#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 subtract( int, int );
void multiply( int, int );

void subtract( Complex );
void multiply( Complex );

bool isEqual( int, int );
bool isEqual( Complex );

private:
int real;
int imaginary;
};

int main()
{
Complex object1;
Complex object2(3,7);
Complex object3(4,-2);
Complex object4(8,0);
Complex object5;
object1.display();
object1.displayComplex();
object2.display();
object2.displayComplex();
object3.display();
object3.displayComplex();
cout<<"Object4: ("<<object4.getReal()
<<","<<object4.getImaginary()<<")"<<endl;
cout<<"Object5: ("<<object5.getReal()
<<","<<object5.getImaginary()<<")"<<endl;
object4.subtract(object3);
if(object5.isEqual(object1))
cout<<"Two objects are same";
else
cout<<"Two objects are not same";

cout << endl;

system("pause");
return 0;
}

// Methods

/************************************************************************
Constructor 1: Complex()
The first constructor for the Complex class is the default constructor and
takes no arguments. Its sole purpose is to create an object with both the
real and imaginary portions set to 1.
*************************************************************************/
Complex::Complex()
{
real=1;
imaginary=1;
}

/************************************************************************
Constructor 2: Complex()
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.
*************************************************************************/
Complex::Complex( int r, int i  )
{
real=r;
imaginary=i;
}

/************************************************************************
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 Complex::display()
{
cout<<"("<<real<<","<<imaginary<<")"<<endl;
}

/************************************************************************
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)
*************************************************************************/
void Complex::displayComplex()
{
cout << "real=" << real << "imaginary=" << imaginary << endl;
cout << real << "+" << imaginary << "i" << endl;
}

/************************************************************************
void setReal()
This method will use the passed in argument to initialize the real portion
of the Complex object.
*************************************************************************/
void Complex::setReal( int r )
{
real=r;
}

/************************************************************************
void setImaginary()
This method will use the passed in argument to initialize the imaginary
portion of the Complex object.
*************************************************************************/
void Complex::setImaginary( int im )
{
imaginary=im;
}

/************************************************************************
void setComplex()
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.
*************************************************************************/
void Complex::setComplex( int r, int i )
{
real=r;
imaginary=i;
}

/************************************************************************
int getReal()
This method returns the real portion of the Complex object
*************************************************************************/
int Complex::getReal()
{
return real;
}

/************************************************************************
int getImaginary()
This method returns the imaginary portion of the Complex object.
*************************************************************************/
int Complex::getImaginary()
{
return imaginary;
}

/************************************************************************
Both of the add methods will add two Complex objects. This first version
has the complex number passed in as its separate real and imaginary parts.
*************************************************************************/
void Complex::add( int c, int d )
{
}

/************************************************************************
Subtraction 1: void subtract()
Both of the subtract methods will subtract two Complex objects. This first
version has the complex number passed in as its separate real and imaginary
parts.
*************************************************************************/
void Complex::subtract( int c, int d )
{
cout<<"Subtract = "<<(real-c)<<"+i"<<(imaginary-d)<<endl;
}

/************************************************************************
Multiplication 1: void multiply()
Both of the multiply methods will multiply two Complex objects. This
first version has the complex number passed in as its separate real and
imaginary parts.
*************************************************************************/
void Complex::multiply( int c, int d )
{
int newReal=(real*c-imaginary*d);
int newimg=(real*d+imaginary*c);
cout<<"Mulplication = "<<newReal<<"+i"<<newimg<<endl;
}

/************************************************************************
Both of the add methods will add two Complex objects. This second version
has the complex number passed in as a Complex object.
*************************************************************************/
{
}

/************************************************************************
Subtraction 2: void subtract()
Both of the subtract methods will subtract two Complex objects. This second
version has the complex number passed in as a Complex object.
*************************************************************************/
void Complex::subtract( Complex obj )
{
cout<<"Subtract = "<<(real-obj.real)<<"+i"<<(imaginary-obj.imaginary)<<endl;
}

/************************************************************************
Multiplication 2: void multiply()
Both of the multiply methods will multiply two Complex objects. This
second version has the complex number passed in as a Complex object.
*************************************************************************/
void Complex::multiply( Complex obj )
{
int newReal=(real*obj.real-imaginary*obj.imaginary);
int newimg=(real*obj.imaginary+imaginary*obj.real);
cout<<"Mulplication = "<<newReal<<"+i"<<newimg<<endl;
}

/************************************************************************
Equality 1: bool isEqual()
Both of the isEqual methods will test if two Complex objects are equal.
This first version has the complex number passed in as its separate real
and imaginary parts. Complex numbers are equal if and only if the real
number portions are equal and the imaginary portions are equal.
*************************************************************************/
bool Complex::isEqual( int a, int b )
{
if(real==a&&imaginary==b)
return true;
else
false;
}

/************************************************************************
Equality 2: bool isEqul()
Both of the isEqual methods will test if two Complex objects are equal.
This 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.
*************************************************************************/
bool Complex::isEqual( Complex obj )
{
if(real==obj.real&&imaginary==obj.imaginary)
return true;
else
false;
}``````

Please help me get on the right track----ignore what is in int main because i know it is wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.