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.

In ComplexNumber::ComplexNumber you are supposed to set the values of the class variables to that of the parameters.

ComplexNumber::ComplexNumber(double r, double i) 
{ 
    this->realPart = r;
    this->imagenaryPart = i;
}

in main() move the line ComplexNumber userInput(c1, c2); down to after the user has entered the values if c1 and c2.

alright i made those changes and now im getting these errors. Any ideas?

complexNumber.cpp:22: error: prototype for 'void ComplexNumber::printComplexNumber( const ComplexNumber&)does not match any in class 'ComplexNumber'

complexNumber.h:23: error: candidate is: void ComplexNumber::printComplexNumber()


Thanks for the help

1) ComplexNumber.h: move the last line down outside the class and code like this: extern void printComplexNumber (const ComplexNumber&); 2) you did not code the class constructor. One was to do it is in ComplexNumber.h ComplexNumber ( ) {realPart = 0; imaginaryPart = 0; } That is called inline code when you put the executable code in the *.h file instead of *.cpp.

Oh thanks, I thought I had all kinds of things screwed up. It really wasnt as bad as I thought. Thanks for the help

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.