rebelcowgirl_73

asked on

# Complex numbers Class

Ok I am gonna start at the beginning. I have listed my Complex.h file, and need some feedback. I am sure there will be a lot more questions from me, so I am gonna start with the first few questions I have. In my code snippet all instructions are included. I would first like to know if it looks like I have created a proper Complex.h file from my instructions. Secondly I am getting an error when I try to build it, however I was wondering if that is because I have not written the .cpp files (think there should be 2) yet???

PS. I am currently using Visual C++ 2008 Express Edition

The error I get when I build it is this:

1>.\Debug\Complex.exe.intermediate.manifest : general error c1010070: Failed to load and parse the manifest. The system cannot find the file specified.

PS. I am currently using Visual C++ 2008 Express Edition

The error I get when I build it is this:

1>.\Debug\Complex.exe.inte

```
/* Explaination of assignment. Create a class called Complex for performing
addition and subtraction of complex numbers. The write a program to test
your class. Complex numbers have the form (real + imaginary * i or a + bi)
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 (default values in case no initializers are provided). Provide
public member functions that perform the following tasks: adding two complex
numbers, subtracting two complex numbers (right operand subtracted from
left operand). For both the adding and subtracting, the real parts are together
and the imaginary parts are added or subtracted together. Finally provide a
public member function that prints Complex numbers in the form(a, b) where a
is the real part and b is the imaginary part.
This is part one of Exercise 9.5
Complex.h
Declaration of class Complex
Member functions are defined in Complex.cpp*/
//prevent multiple inclusions of header file
#ifndef COMPLEX_H
#define COMPLEX_H
//Complex class definition
class Complex
{
public:
Complex();//default constructor
Complex(double firstNumber, double secondNumber);//constructor
Complex addComplex(const &Complex, const &Complex);//add member function
Complex subtractComplex(const &Complex, const &Complex);//subtract member function
void displayComplex(); //print member function
private:
double real;//declare real variable
double imaginary;//declare imaginary variable
};//end class Complex
#endif
```

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER CERTIFIED SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

I think it's best he follows the assignment, jkr :

Provide public member functions that perform the following tasks: adding two complex numbers, subtracting two

complex numbers (right operand subtracted from left operand).

which clearly states the member functions need two parameters. The only thing you could change is the return value. You could return a reference, and return the this object (which will contain the sum of the two parameters).

Provide public member functions that perform the following tasks: adding two complex numbers, subtracting two

complex numbers (right operand subtracted from left operand).

which clearly states the member functions need two parameters. The only thing you could change is the return value. You could return a reference, and return the this object (which will contain the sum of the two parameters).

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Ok guys I have not even finished building the definitions in Complex.cpp. I have hit a roadblock. Here is what I have, but I know I have gone way off the deep end here. So tell me what I am doing wrong, as if there is probably none of it right. I hate the fact that my book does not discuss this in any depth what so ever. Anyways here is what I have. As I said I was down to subtractComplex() when I realized I am way off base here. Any help? Thanks

```
This is part two of Exercise 9.5
Complex.cpp
Declaration of class Complex
Member functions definitions for class Complex*/
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cmath>
#include "Complex.h"//include definition of class Complex from Complex.h
Complex::Complex()//Complex default constructor initializes each double to 0.0
{
real = 0.0;
imaginary = 0.0;
}//end Complex constructor
Complex::Complex(double realNumber, double imaginaryNumber)//Complex constructor definition
{
real = realNumber;//validate real number
imaginary = imaginaryNumber;//validate imaginary number
}//end member function Complex
Complex Complex::addComplex(double realNumber, double imaginaryNumber)//add member function definition
{
double tempReal;
double tempImaginary;
tempReal = realNumber + realNumber;
tempImaginary = imaginaryNumber + imaginaryNumber;
return tempReal, tempImaginary;
}//end member function addComplex
Complex Complex::subtractComplex(double realNumber, double imaginaryNumber)//subtract member function definition
{
double tempReal;
double temImaginary;
tempReal = realNumber - real
```

Your addComplex and subtractComplex took two Complex numbers (not doubles), didn't they ?

ASKER

yes that is where I fell off the diving board!!!!! I am lost in the oh no state!!!!

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Stuck #1

Shouldn't this part account for 2 seperate Complex #'s

Complex::Complex()//Complex default constructor initializes each double to 0.0

{

real = 0.0;

imaginary = 0.0;

}//end Complex constructor

Shouldn't this part account for 2 seperate Complex #'s

Complex::Complex()//Comple

{

real = 0.0;

imaginary = 0.0;

}//end Complex constructor

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Ok that said:

Stuck #2 if by signiture methods you meant just the first part and not the code in the {} then for the next part I have this:

Complex::Complex(double firstNumber, double secondNumber)//Complex constructor definition

{

}//end member function Complex

wouldn't this be the part of the assignment that would be: 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. Would the first constructor be necessary. Also for this one, I would want it to define both the doubles? not sure if define is the right word. I originally had (d real, d imag) and realized that was wrong this should have (d N1, d N2). God I hope I am making since to you! I am so lame when it comes to explaining in terms of programming!

Stuck #2 if by signiture methods you meant just the first part and not the code in the {} then for the next part I have this:

Complex::Complex(double firstNumber, double secondNumber)//Complex constructor definition

{

}//end member function Complex

wouldn't this be the part of the assignment that would be: 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. Would the first constructor be necessary. Also for this one, I would want it to define both the doubles? not sure if define is the right word. I originally had (d real, d imag) and realized that was wrong this should have (d N1, d N2). God I hope I am making since to you! I am so lame when it comes to explaining in terms of programming!

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

>> I originally had (d real, d imag) and realized that was wrong

In my Complex.h file it is:

Complex(double firstNumber, double secondNumber);//constructor

so in my Complex.cpp file shouldn't it be

Complex::Complex(double firstNumber, double secondNumber)//Complex constructor definition

In my Complex.h file it is:

Complex(double firstNumber, double secondNumber);//constructo

so in my Complex.cpp file shouldn't it be

Complex::Complex(double firstNumber, double secondNumber)//Complex constructor definition

You can change it in both ;) In any case, naming of the attributes does not have to be the same. The types need to match though.

ASKER

So if I did:

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

then I could take out the first constructor because in the second constructor, I have default values for each parameter, and if the arguments are left blank when the function is called it will call the default value? Is that the gist of it?

>>naming of the attributes does not have to be the same. The types need to match though.

I realize I can change the names but for me being so new it is easier if I can simplify as much as possible.

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

then I could take out the first constructor because in the second constructor, I have default values for each parameter, and if the arguments are left blank when the function is called it will call the default value? Is that the gist of it?

>>naming of the attributes does not have to be the same. The types need to match though.

I realize I can change the names but for me being so new it is easier if I can simplify as much as possible.

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

I need to get some sleep now - I'll be back in a few hours.

You're doing good, so just continue getting things to work. Feel free to post your code here for us to verify, as well as any questions you might have about this.

You're doing good, so just continue getting things to work. Feel free to post your code here for us to verify, as well as any questions you might have about this.

ASKER

So insdie the {} what comes here:

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

My assumption is declaring both numberOne and numberTwo? Hope you understand what I mean. This constructor does what? I think it would just declare each number. So maybe, numberOne = firstNumber and numberTwo = secondNumber?

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

My assumption is declaring both numberOne and numberTwo? Hope you understand what I mean. This constructor does what? I think it would just declare each number. So maybe, numberOne = firstNumber and numberTwo = secondNumber?

ASKER

How do you break up the real and imaginary part of each double variable?

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Ok Here is what I have so far, I have the Complex.h file and the Complex.cpp file. I think I am doing ok, but you can see that when I get to my Complex.cpp file I am having problems figuring out what to do next.

So here is what I have.

Here are my instructions:

Explaination of assignment. Create a class called Complex for performing

addition and subtraction of complex numbers. The write a program to test

your class. Complex numbers have the form (real + imaginary * i or a + bi)

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 (default values in case no initializers are provided). Provide

public member functions that perform the following tasks: adding two complex

numbers, subtracting two complex numbers (right operand subtracted from

left operand). For both the adding and subtracting, the real parts are together

and the imaginary parts are added or subtracted together. Finally provide a

public member function that prints Complex numbers in the form(a, b) where a

is the real part and b is the imaginary part.

So here is what I have.

Here are my instructions:

Explaination of assignment. Create a class called Complex for performing

addition and subtraction of complex numbers. The write a program to test

your class. Complex numbers have the form (real + imaginary * i or a + bi)

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 (default values in case no initializers are provided). Provide

public member functions that perform the following tasks: adding two complex

numbers, subtracting two complex numbers (right operand subtracted from

left operand). For both the adding and subtracting, the real parts are together

and the imaginary parts are added or subtracted together. Finally provide a

public member function that prints Complex numbers in the form(a, b) where a

is the real part and b is the imaginary part.

```
/*This is part oneA of Exercise 9.5
Complex.h
Declaration of class Complex
Member functions are defined in Complex.cpp*/
//prevent multiple inclusions of header file
#ifndef COMPLEX_H
#define COMPLEX_H
//Complex class definition
class Complex
{
public:
Complex(double firstNumber, double secondNumber);//constructor
Complex addComplex(const Complex&, const Complex&);//add member function
Complex subtractComplex(const Complex&, const Complex&);//subtract member function
void displayComplex(); //print member function
private:
double real;//declare real variable
double imaginary;//declare imaginary variable
};//end class Complex
#endif
/*This is part oneB of Exercise 9.5
Complex.cpp
Member functions definitions for class Complex*/
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cmath>
#include "Complex.h"//include definition of class Complex from Complex.h
Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition
{
...code goes here
}//end member function Complex
Complex Complex::addComplex(,)//add member function definition
{
...code goes here
}//end member function addComplex
Complex Complex::subtractComplex(,)//subtract member function definition
{
...code goes here
}//end member function subtractComplex
void Complex::displayComplex()
{
...code goes here
}//end function displayComplex
```

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Ok wait a minute what am i missing?

a complex number is: real +imaginary*i and if you add to complex numbers together you are adding (real+imaginary*i) + (real+imaginary*i) right and the same with a - between them for subtraction so if you break up real = realNumber;//validate real number and imaginary = imaginaryNumber;//validate imaginary number how do you have 2 real parts to add or subtract together and 2 imaginary parts to add or subtract together.

Instructions state Provide public member functions that perform the following tasks: adding 2 complex numbers: The real parts are added together and the imaginary parts are added together & subtract 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!

a complex number is: real +imaginary*i and if you add to complex numbers together you are adding (real+imaginary*i) + (real+imaginary*i) right and the same with a - between them for subtraction so if you break up real = realNumber;//validate real number and imaginary = imaginaryNumber;//validate

Instructions state Provide public member functions that perform the following tasks: adding 2 complex numbers: The real parts are added together and the imaginary parts are added together & subtract 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!

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

my instructor just clarified this to me:

The addComplex should have only one argument:

Complex addComplex(const Complex&);//add member function

Same holds for subtract.

Remember that each member function has one "implicit" argument, which is the object on which it is called. So, what you are adding is this object and the "explicit" argument.

The addComplex should have only one argument:

Complex addComplex(const Complex&);//add member function

Same holds for subtract.

Remember that each member function has one "implicit" argument, which is the object on which it is called. So, what you are adding is this object and the "explicit" argument.

ASKER

not sure about the implicit and explicit part but this is what I now have!

/*Complex.h

Declaration of class Complex

Member functions are defined in Complex.cpp*/

//prevent multiple inclusions of header file

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double firstNumber, double secondNumber);//constructor

Complex addComplex(const Complex&);//add member function

Complex subtractComplex(const Complex&);//subtract member function

void displayComplex(); //print member function

private:

double real;//declare real variable

double imaginary;//declare imaginary variable

};//end class Complex

#endif

/*Complex.cpp

Member functions definitions for class Complex*/

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

#include "Complex.h"//include definition of class Complex from Complex.h

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

numberOne = firstNumber;

numberTwo = secondNumber;

}//end member function Complex

Complex Complex::addComplex()//add member function definition

{

...code goes here

}//end member function addComplex

Complex Complex::subtractComplex()//subtract member function definition

{

...code goes here

}//end member function subtractComplex

void Complex::displayComplex()

{

...code goes here

}//end function displayComplex

/*Complex.h

Declaration of class Complex

Member functions are defined in Complex.cpp*/

//prevent multiple inclusions of header file

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double firstNumber, double secondNumber);//constructo

Complex addComplex(const Complex&);//add member function

Complex subtractComplex(const Complex&);//subtract member function

void displayComplex(); //print member function

private:

double real;//declare real variable

double imaginary;//declare imaginary variable

};//end class Complex

#endif

/*Complex.cpp

Member functions definitions for class Complex*/

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

#include "Complex.h"//include definition of class Complex from Complex.h

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

numberOne = firstNumber;

numberTwo = secondNumber;

}//end member function Complex

Complex Complex::addComplex()//add

{

...code goes here

}//end member function addComplex

Complex Complex::subtractComplex()

{

...code goes here

}//end member function subtractComplex

void Complex::displayComplex()

{

...code goes here

}//end function displayComplex

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

>>Is that right ? What are numberOne and numberTwo ??

I already changed that back to real and imaginary.

>>This is how you declared the add and subtract methods in your .h file. But in your .cpp file, you have them like this :

I realize the parameter is missing, but I can figure out how to put it into the function

>> Btw, try returning Complex& instead of Complex.

Not sure what you mean here?

I already changed that back to real and imaginary.

>>This is how you declared the add and subtract methods in your .h file. But in your .cpp file, you have them like this :

I realize the parameter is missing, but I can figure out how to put it into the function

>> Btw, try returning Complex& instead of Complex.

Not sure what you mean here?

>> but I can figure out how to put it into the function

You just need to make the function header the same as in the .h file. Then you can use the arguments inside the function.

>> >> Btw, try returning Complex& instead of Complex.

>>

>> Not sure what you mean here?

The return value of the add and subtract methods currently is a Complex object. It's better to return a reference to a Complex object, or to return Complex&, since you'll return the current object.

You just need to make the function header the same as in the .h file. Then you can use the arguments inside the function.

>> >> Btw, try returning Complex& instead of Complex.

>>

>> Not sure what you mean here?

The return value of the add and subtract methods currently is a Complex object. It's better to return a reference to a Complex object, or to return Complex&, since you'll return the current object.

ASKER

ok, have made a few changes, and am getting the following errors. What I was attempting to do is get it to a point where I could create a main and test just the constructor and the display method to aleast see that I had it so far, but as I said now I have errors.

Code is:

Complex.h

//prevent multiple inclusions of header file

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double realNumber, double imaginaryNumber);//constructor

Complex addComplex(const Complex&);//add member function

Complex subtractComplex(const Complex&);//subtract member function

void displayComplex(); //print member function

private:

double real;//declare real variable

double imaginary;//declare imaginary variable

};//end class Complex

#endif

Complex.cpp

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

#include "Complex.h"//include definition of class Complex from Complex.h

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

real= firstNumber;

imaginary = secondNumber;

}//end member function Complex

Complex Complex::addComplex(const Complex&)//add member function definition

{

//code goes here

return 0;

}//end member function addComplex

Complex Complex::subtractComplex(const Complex&)//subtract member function definition

{

//code goes here

return 0;

}//end member function subtractComplex

void Complex::displayComplex()

{

cout << "The Complex number is:";

cout << "(" << real << ", " << imaginary << ")" << endl;

}//end function displayComplex

Errors are:

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

1>D:\Homework\Visual Studio 2005\Projects\Complex\Debug\Complex.exe : fatal error LNK1120: 1 unresolved externals

Code is:

Complex.h

//prevent multiple inclusions of header file

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double realNumber, double imaginaryNumber);//constru

Complex addComplex(const Complex&);//add member function

Complex subtractComplex(const Complex&);//subtract member function

void displayComplex(); //print member function

private:

double real;//declare real variable

double imaginary;//declare imaginary variable

};//end class Complex

#endif

Complex.cpp

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

#include "Complex.h"//include definition of class Complex from Complex.h

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

real= firstNumber;

imaginary = secondNumber;

}//end member function Complex

Complex Complex::addComplex(const Complex&)//add member function definition

{

//code goes here

return 0;

}//end member function addComplex

Complex Complex::subtractComplex(c

{

//code goes here

return 0;

}//end member function subtractComplex

void Complex::displayComplex()

{

cout << "The Complex number is:";

cout << "(" << real << ", " << imaginary << ")" << endl;

}//end function displayComplex

Errors are:

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

1>D:\Homework\Visual Studio 2005\Projects\Complex\Debu

ASKER

Ok I added a main seperately as testComplex.com and I get the following error. Again, I am just trying to see if the constructor and display works here is the main.

testComplex.cpp

Program to test class Complex

Please note, this file must be compiled with Complex.cpp*/

#include <iostream>

using std::cout;

using std::endl;

#include "Complex.h"//include definition of class Complex from Complex.h

int main()

{

Complex complex1();

Complex complex2(2.3, 3.4);

Complex complex3(1.5, -12.2);

complex1.displayComplex();

complex2.displayComplex();

complex3.displayComplex();

return 0;

}//end main

Here is my error:

1>d:\homework\visual studio 2005\projects\complex\complex\testcomplex.cpp(36) : error C2228: left of '.displayComplex' must have class/struct/union

this error points to the first display part of the code.

testComplex.cpp

Program to test class Complex

Please note, this file must be compiled with Complex.cpp*/

#include <iostream>

using std::cout;

using std::endl;

#include "Complex.h"//include definition of class Complex from Complex.h

int main()

{

Complex complex1();

Complex complex2(2.3, 3.4);

Complex complex3(1.5, -12.2);

complex1.displayComplex();

complex2.displayComplex();

complex3.displayComplex();

return 0;

}//end main

Here is my error:

1>d:\homework\visual studio 2005\projects\complex\comp

this error points to the first display part of the code.

ASKER

Ok I figured out the error, by taking out the first complex1() with no arguments, it compiles which tells me that the default needs improvement, because it should give me a (0.0, 0.0) if no parameters are given but it doesn't compile if I add it back? Any ideas?

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

If you look at my last posted code, I dont have a default constructor anymore, the original Complex::Complex() was removed and now the only constructor I have is this:

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

real= firstNumber;

imaginary = secondNumber;

}//end member function Complex

therefore the Complex complex1 = Complex() does not work

any ideas, or is the only way to do this is to put the default constructor back?

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

real= firstNumber;

imaginary = secondNumber;

}//end member function Complex

therefore the Complex complex1 = Complex() does not work

any ideas, or is the only way to do this is to put the default constructor back?

>> the original Complex::Complex() was removed and now the only constructor I have is this:

The other constructor will take up the job of the default constructor (because it has default values for both parameters).

>> therefore the Complex complex1 = Complex() does not work

It should. Did you try it ?

The other constructor will take up the job of the default constructor (because it has default values for both parameters).

>> therefore the Complex complex1 = Complex() does not work

It should. Did you try it ?

ASKER

>>It should. Did you try it ?

I added it to main this is what I have for main()

int main()

{

Complex complex1 = Complex();

Complex complex2(2.3, 3.4);

Complex complex3(1.5, -12.2);

complex2.displayComplex();

complex3.displayComplex();

return 0;

}//end main

These are the errors I get:

1>d:\homework\visual studio 2005\projects\complex\complex\testcomplex.cpp(33) : error C2512: 'Complex::Complex' : no appropriate default constructor available

1>d:\homework\visual studio 2005\projects\complex\complex\testcomplex.cpp(33) : error C2512: 'Complex' : no appropriate default constructor available

I added it to main this is what I have for main()

int main()

{

Complex complex1 = Complex();

Complex complex2(2.3, 3.4);

Complex complex3(1.5, -12.2);

complex2.displayComplex();

complex3.displayComplex();

return 0;

}//end main

These are the errors I get:

1>d:\homework\visual studio 2005\projects\complex\comp

1>d:\homework\visual studio 2005\projects\complex\comp

Can you show your current complex.h and complex.cpp files ?

ASKER

Complex.h ...............

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double realNumber, double imaginaryNumber);//constructor

Complex addComplex(const Complex&);//add member function

Complex subtractComplex(const Complex&);//subtract member function

void displayComplex(); //print member function

private:

double real;//declare real variable

double imaginary;//declare imaginary variable

};//end class Complex

#endif

Complex.cpp .................

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

#include "Complex.h"//include definition of class Complex from Complex.h

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

real= firstNumber;

imaginary = secondNumber;

}//end member function Complex

Complex Complex::addComplex(const Complex&)//add member function definition

{

//code goes here

return 0;

}//end member function addComplex

Complex Complex::subtractComplex(const Complex&)//subtract member function definition

{

//code goes here

return 0;

}//end member function subtractComplex

void Complex::displayComplex()

{

cout << "The Complex number is:";

cout << "(" << real << ", " << imaginary << ")" << endl;

}//end function displayComplex

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double realNumber, double imaginaryNumber);//constru

Complex addComplex(const Complex&);//add member function

Complex subtractComplex(const Complex&);//subtract member function

void displayComplex(); //print member function

private:

double real;//declare real variable

double imaginary;//declare imaginary variable

};//end class Complex

#endif

Complex.cpp .................

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

#include "Complex.h"//include definition of class Complex from Complex.h

Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition

{

real= firstNumber;

imaginary = secondNumber;

}//end member function Complex

Complex Complex::addComplex(const Complex&)//add member function definition

{

//code goes here

return 0;

}//end member function addComplex

Complex Complex::subtractComplex(c

{

//code goes here

return 0;

}//end member function subtractComplex

void Complex::displayComplex()

{

cout << "The Complex number is:";

cout << "(" << real << ", " << imaginary << ")" << endl;

}//end function displayComplex

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Ok! Yeahhhhh. Almost done, now add for the addComplex and subtractComplex function the only help the instructor would give is this. He said:

To add in main you will have this eventually,

Complex complex4 = complex2.addComplex(complex3);

complex4.displayComplex();

//the add complex member function is applied on complex1 (its implicit argument), and complex2. The result is another instance of the Complex class.

Before you tackle the add function, make sure that you have the constructor and display methods working properly.

To add in main you will have this eventually,

Complex complex4 = complex2.addComplex(comple

complex4.displayComplex();

//the add complex member function is applied on complex1 (its implicit argument), and complex2. The result is another instance of the Complex class.

Before you tackle the add function, make sure that you have the constructor and display methods working properly.

>> To add in main you will have this eventually,

>> Complex complex4 = complex2.addComplex(complex3);

Ah, so that's how he wants it. You can ignore my earlier comment about returning by reference then ;)

>> the only help the instructor would give is this.

Is that sufficient ? Or do you have a question ?

>> Complex complex4 = complex2.addComplex(comple

Ah, so that's how he wants it. You can ignore my earlier comment about returning by reference then ;)

>> the only help the instructor would give is this.

Is that sufficient ? Or do you have a question ?

ASKER

I get lost because I do not understand how it passes Complex2 when the only parameter is Complex3 so I have no ideas!

ASKER

Complex Complex::addComplex(const Complex&)//add member function definition

{

double real = real + real,

imaginary = imaginary + imaginary;

return(Complex(real, imaginary));

}

I know this is probably wrong but it is where my head went

{

double real = real + real,

imaginary = imaginary + imaginary;

return(Complex(real, imaginary));

}

I know this is probably wrong but it is where my head went

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Ok I think I may have gotten somewhere but I had to change the addComplex() and I would just like to know if there is a way to change it back to:

Complex Complex::addComplex(const Complex&)//add member function definition

This is what I changed it to and it works, but I don't think I am suppose to have the op part: Here is what is working!

Complex Complex::addComplex(const Complex& op)//add member function definition

{

Complex temp;

temp.real = real + op.real;

temp.imaginary = imaginary + op.imaginary;

return temp;

Complex Complex::addComplex(const Complex&)//add member function definition

This is what I changed it to and it works, but I don't think I am suppose to have the op part: Here is what is working!

Complex Complex::addComplex(const Complex& op)//add member function definition

{

Complex temp;

temp.real = real + op.real;

temp.imaginary = imaginary + op.imaginary;

return temp;

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

now I am just trying to make the print out snazzy for the teacher, and then I will come back and assign the points. Hoping there are no more problems... Thanks so much. Rebel

>> Hoping there are no more problems...

If there are, feel free to ask about them.

If you want, you can post the code once it's finished, and I'll look it over ...

If there are, feel free to ask about them.

If you want, you can post the code once it's finished, and I'll look it over ...

ASKER

Ok here it is finished:

Complex.h

//prevent multiple inclusions of header file

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double realNumber = 0.0, double imaginaryNumber = 0.0);//

Complex addComplex(const Complex&);//

Complex subtractComplex(const Complex&);//

void displayComplex(); //

private:

double real;//

double imaginary;//

};//end class Complex

#endif

Complex.cpp

Complex::Complex(double firstNumber, double secondNumber)//

{

real= firstNumber;

imaginary = secondNumber;

}//

Complex Complex::addComplex(const Complex& argument)//

{

Complex temporary;

temporary.real = real + argument.real;

temporary.imaginary = imaginary + argument.imaginary;

return temporary;

}//

Complex Complex::subtractComplex(const Complex& argument)//

{

Complex temporary;

temporary.real = real - argument.real;

temporary.imaginary = imaginary - argument.imaginary;

return temporary;

}//

void Complex::displayComplex()

{

cout << "(" << real << ", " << imaginary << ")" << endl;

}//

testComplex.cpp

Program to test class Complex

Please note, this file must be compiled with Complex.cpp*/

#include <iostream>

using std::cout;

using std::endl;

#include "Complex.h"//include definition of class Complex from Complex.h

int main()

{

//introduce myself

Complex complex1 = Complex();//

cout << "1st Complex #: ";//

complex1.displayComplex();

Complex complex2(2.3, 3.4);//

cout << "2nd Complex #: ";//

complex2.displayComplex();

Complex complex3(1.5, -12.2);//

cout << "3rd Complex #: ";//

complex3.displayComplex();

Complex complex4(7.5, 8.7);//

cout << "4th Complex #: ";//

complex4.displayComplex();

//

Complex complex5 = complex2.addComplex(complex3);

cout << endl;//

cout << "Adding the 2nd and 3rd Complex number gives you:" << endl;

cout << "(2.3, 3.4) + (1.5, -12.2) = ";

complex5.displayComplex();

//

Complex complex6 = complex4.subtractComplex(complex2);

cout << endl; //

cout << " Subtracting the 2nd Complex number from the 4th Complex number gives you:" << endl;

cout << "(7.5, 8.7) - (2.3, 3.4) = ";

complex6.displayComplex();

return 0;

}//

OF COURSE I TOOK OUT ALL THE COMMENTS TO SAVE SPACE AND HOPE IT MAKES IT MORE DIFFICULT FOR OTHERS TO JUST GO AND COPY MY WORK IF THEIR ARE ANY OTHERS AROUND HERE!

Complex.h

//prevent multiple inclusions of header file

#ifndef COMPLEX_H

#define COMPLEX_H

//Complex class definition

class Complex

{

public:

Complex(double realNumber = 0.0, double imaginaryNumber = 0.0);//

Complex addComplex(const Complex&);//

Complex subtractComplex(const Complex&);//

void displayComplex(); //

private:

double real;//

double imaginary;//

};//end class Complex

#endif

Complex.cpp

Complex::Complex(double firstNumber, double secondNumber)//

{

real= firstNumber;

imaginary = secondNumber;

}//

Complex Complex::addComplex(const Complex& argument)//

{

Complex temporary;

temporary.real = real + argument.real;

temporary.imaginary = imaginary + argument.imaginary;

return temporary;

}//

Complex Complex::subtractComplex(c

{

Complex temporary;

temporary.real = real - argument.real;

temporary.imaginary = imaginary - argument.imaginary;

return temporary;

}//

void Complex::displayComplex()

{

cout << "(" << real << ", " << imaginary << ")" << endl;

}//

testComplex.cpp

Program to test class Complex

Please note, this file must be compiled with Complex.cpp*/

#include <iostream>

using std::cout;

using std::endl;

#include "Complex.h"//include definition of class Complex from Complex.h

int main()

{

//introduce myself

Complex complex1 = Complex();//

cout << "1st Complex #: ";//

complex1.displayComplex();

Complex complex2(2.3, 3.4);//

cout << "2nd Complex #: ";//

complex2.displayComplex();

Complex complex3(1.5, -12.2);//

cout << "3rd Complex #: ";//

complex3.displayComplex();

Complex complex4(7.5, 8.7);//

cout << "4th Complex #: ";//

complex4.displayComplex();

//

Complex complex5 = complex2.addComplex(comple

cout << endl;//

cout << "Adding the 2nd and 3rd Complex number gives you:" << endl;

cout << "(2.3, 3.4) + (1.5, -12.2) = ";

complex5.displayComplex();

//

Complex complex6 = complex4.subtractComplex(c

cout << endl; //

cout << " Subtracting the 2nd Complex number from the 4th Complex number gives you:" << endl;

cout << "(7.5, 8.7) - (2.3, 3.4) = ";

complex6.displayComplex();

return 0;

}//

OF COURSE I TOOK OUT ALL THE COMMENTS TO SAVE SPACE AND HOPE IT MAKES IT MORE DIFFICULT FOR OTHERS TO JUST GO AND COPY MY WORK IF THEIR ARE ANY OTHERS AROUND HERE!

Looks good :) Good work ! Oh, and if there should be a problem with someone copying you, I can vouch for you - just show him this thread ;)

what would the definitions for multiplying and dividing complex numbers look like? i don't think multiplying would be a big deal, but the division could be. maybe just a simple if statement preventing the denominator from being = to 0?

sold22, it's probably best to post your own question about this. That way, you get personal assistance, rather than piggy-backing on someone else's question.

ASKER