I had posted this before. But I am more on the right track. The places that I need help with have something saying like "need something here" or "need to fix the way it is displaying" I can't figure it out. I also am not sure if the multiplication portion of the methods is correct. I have been working on it for the past day. Please help me.

This is what I have:

#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()
{
    
Complex object1;
Complex object2(3,7);
Complex object3(4,-2);
Complex object4(8,0);
Complex object5;    

cout << "The default constructor produces: \n";
object1.displayComplex();
cout << "    or    ";
object1.display();
cout << endl << endl << endl;

cout << "The constructor with values produces: \n";
object2.displayComplex();
cout << "    or    ";
object2.display();
cout << endl << endl;
object3.displayComplex();
cout << "    or    ";
object3.display();
cout << endl << endl << endl;

cout << "The getReal and getImaginary methods produces:" << endl << endl;
cout << "The real portion of d4 is " << object4.getReal() << endl;
cout << "The imaginary portion is " << object4.getImaginary() << endl << endl;
cout << "The real portion of d5 is " << object5.getReal() << endl;
cout << "The imaginary portion is " << object5.getImaginary()<< endl << endl << endl;

cout << "The add method produces:" << endl << endl;

cout << "The sum of ";
object2.displayComplex();
cout << " and ";
object3.displayComplex();
cout << " is   ";
object2.add(object3);
cout << "  or  ";
// need something here

cout << endl << endl;
cout << "The sum of ";
object1.display();
cout << " and (15,3) is  ";
object1.add(15,3);
cout << "  or  ";
//need something here


cout << endl << endl << endl;

cout << "The subtract method produces:" << endl << endl;
cout << "The difference of ";
object5.displayComplex();
cout << " minus ";
object3.displayComplex();
cout << " is  ";
object5.subtract(object3);
cout << " or ";
//need something here

cout << endl << endl;

//Fix this one 
cout << "The difference of ";
object1.add(6,4); // need to fix the way it is displaying -- should be in () without the i
cout << " minus (9) is  ";
//need something here
cout << "   or   ";
//need something here
cout << endl << endl << endl;


cout << "The multiply method produces:" << endl << endl;
cout << "The product ";
object1.add(-3,4);
cout << " times ";
object3.displayComplex();
cout << " is  ";
//need something here
cout << "   or   ";
// need something here

cout << endl << endl << "The product of ";
object1.add(7,-1); // need to fix the way it is displaying -- should be just (8) not (8, 0i)
cout << " times ";
object3.display();
cout << "  is  ";
// need something here
cout << "  or  ";
//need something here


cout << endl << endl << endl;

cout << "The isEqual method produces: " << endl << endl;

cout << "Comparing ";
object1.add(1,23);
cout << "and ";
object3.displayComplex();
//need something here

cout << endl;

cout << "Comparing ";
object1.add(-4,2);
cout << "and ";
object1.add(-4,2);
//need something here

cout << endl;

cout << "Comparing ";
object1.add(31,-17); // need to fix the way it is displaying -- should be in () without the i
cout << "and ";
object1.add(14,5);  // need to fix the way it is displaying -- should be in () without the i
//need something here



   
    
    
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 << "+" << 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;   
}


/************************************************************************
Addition 1: void add()
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 )
{
cout<<(real+c)<<"+"<<(imaginary+d)<< "i" <<endl;    
}


/************************************************************************
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<<(real-c)<<"+"<<(imaginary-d)<< "i" <<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<<newReal<<"+"<<newimg<< "i" <<endl; 
}


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


/************************************************************************
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<<(real-obj.real)<<"+"<<(imaginary-obj.imaginary)<< "i" <<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<<newReal<<"+"<<newimg<<"i" << 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 isEqual()
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;
}

This is what it should look like:

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 have a question as to why you are displaying in your add method. Your add method should do nothing more than add the two numbers together. You may want to check with your specification to make sure that your code is not supposed to add the two together and return a reference to a third number. Either way, though, your display should be separate from your operations.

Seeing if your imaginary portion is 0 takes an if statement in the display function. If you want to choose which form to display (,) vs a+bi, I would say add a parameter to your display function (with a default value of one way or the other) which governs how the output is printed.

Otherwise, you need to be a bit more specific rather than saying "need something here." You are the one that knows this program best at this point, you need to present it to us as if we've never seen it before.

I understand that this is fill in blanks program from teacher. You should explain your problems one by one including error messages.

Also this program is first time I see complex numbers not including real numbers.
Seems strange to have numbers that can express result of sqrt(-1) but not 1/2 or sqrt(2)

Be a part of the DaniWeb community

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