Im very well a beginner and I cant quiet grasp classes. I found that this topic is all over forums and I have decided to give it a whirl and I am stuck. Here is what needs to be done.
You should create a class called ComplexNumber. This class will contain the real and imaginary parts of the complex number in private data members defined as doubles. Your class should contain a constructor that allows the data members of the imaginary number to be specified as parameters of the constructor. A default (non-parameterized) constructor should initialize the data members to 0.0.
Your complex number class should provide the following member functions:
double GetRealPart( ) This function will return the real part of the complex number, as a double.
double GetImaginaryPart( ) This function will return the imaginary part of the complex number, as a double.
Addition of two complex numbers: The function will take one complex number object as a parameter and return a complex number object. When adding two complex numbers, the real part of the calling object is added to the real part of the complex number object passed as a parameter, and the imaginary part of the calling object is added to the imaginary part of the complex number object passed as a parameter.
Subtraction of two complex numbers: The function will take one complex number object as a parameter and return a complex number object. When subtracting two complex numbers, the real part of the complex number object passed as a parameter is subtracted from the real part of the calling object, and the imaginary part of the complex number object passed as a parameter is subtracted from the imaginary part of the calling object.
You program should also include the following stand-alone function (it is not a member of the ComplexNumber class).
void printComplexNumber( const ComplexNumber& n) The function will print the Complex Number object passed as a parameter in the form
Here is my code so far:
complexNumber.h file
#include <iostream>
using namespace std;
class ComplexNumber
{
private:
double realPart;
double imaginaryPart;
public:
//Default Constructor.
ComplexNumber ( );
//Parameterized Constructor
ComplexNumber (double r, double i);
double GetRealPart ( ) const;
double GetImaginaryPart ( ) const;
void printComplexNumber ();
};
complexNumber.cpp file
#include <iostream>
#include "complexNumber.h"
using namespace std;
ComplexNumber::ComplexNumber(double, double)
{
}
double ComplexNumber::GetRealPart( ) const
{
return realPart;
}
double ComplexNumber::GetImaginaryPart( ) const
{
return imaginaryPart;
}
void printComplexNumber( const ComplexNumber& n)
{
if( n.GetImaginaryPart ( ) < 0)
cout << n.GetRealPart( ) <<" - "<< n.GetImaginaryPart( ) << "i";
if( n.GetImaginaryPart( ) > 0)
cout << n.GetRealPart( ) <<" + "<< n.GetImaginaryPart( ) << "i";
if( n.GetRealPart( ) == 0)
cout << n.GetImaginaryPart( ) << "i";
if( n.GetImaginaryPart( ) == 0 )
cout << n.GetRealPart( );
if( n.GetImaginaryPart( ) == 1 )
cout << n.GetRealPart( ) << "i";
}
and finally the driver.cpp file
#include <iostream>
#include "complexNumber.h"
using namespace std;
int main ( )
{
double c1, c2;
ComplexNumber userInput(c1, c2);
cout << "Please enter in a value for the real part of the number: "<< endl;
cin >> c1;
cout << "Please enter in a value for the imaginary part of the number: "<<endl;
cin >> c2;
cout << "The value of the complex number created is: "<< endl;
printComplexNumber(userInput);
system ("PAUSE");
return 0;
}
Thanks for the help. like I said before im just completly stuck on this and I know its not that hard. Classes are just confusing to me and i probably have made some really dumb mistakes somewhere.