Click to See Complete Forum and Search --> : help with C++ homework :)


JKlebs9225
01-25-2003, 04:32 PM
Hi there everyone, I need some help! In my C++ class we are learning about classes and data abstraction, and there's a lab exercise that I am having trouble with. The program is designed to perform arithmetic with complex numbers in the form realPart + imaginaryPart * i, where i is the square root of -1 Here it is:

/* There are 3 files, one to create the class, one to implement the
functions in the class, and one to use the class. This first one is
complex.h, to create the class */
class complex {
public:
Complex( double, double ); // constructor
void addition( const Complex &a );
void subtraction( const Complex &s)'
void printComplex( void );
void setComplexNumber( double real, double imaginary );

private:
float realPart;
float imaginaryPart;
};



/* next file is complexM.cpp, it implements the classes functions */
#inlcude <iostream>
using std::cout;
#include "complex.h"

Complex::Complex( double real, double imaginary )
{
setComplexNumber( real, imaginary );
}

void Complex::addition( const Complex &a )
{
// what do I do here?
}

void Complex::subtraction( const Complex &s )
{
// what do I do here?
}

void Complex::printComplex(void)

{
cout << '(' << realPart << ", " << imaginaryPart << ')';
}

void Complex::setComplexNumber( double real, double imaginary )
{
realPart = real;
imaginaryPart = imaginary;
}


// next file is complex.cpp, to test the class
#include <iostream>
#include "complex.h"
#include "complexM.cpp"

int main()
{
Complex b(1,7), c(9,2);
b.printComplex();
cout << " + ";
c.printComplex();
cout << " = ";
b.addition(c);
b.printComplex();

cout << endl;
b.setComplexNumber(10, 1 );
c.setComplexNumber(11,5);
b.printComplex();
cout << " - ";
c.printComplex();
cout << " = ";
b.subtraction(c);
b.printComplex();
cout << endl;

return 0;
}




I don't know what to put in the body of the functions addition and subtraction. Can anyone help? Thanks
-Jason

JKlebs9225
01-25-2003, 05:41 PM
Ok, I think I figured it out. In the addition potion I just have to put
realPart += a.realPart;
imaginaryPart += a.imaginaryPart;

The same thing goes for the subtraction.

majidpics
01-25-2003, 05:47 PM
void Complex::addition( const Complex &a )
{
realPart=realPart+a.realPart;
}

void Complex::subtraction( const Complex &s )
{
imaginaryPart=imaginarypart+a.imaginaryPart;
}