GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Sep-2009, 11:49
bubaduff bubaduff is offline
New Member
 
Join Date: Sep 2009
Posts: 3
bubaduff is on a distinguished road

Operator Overloading


I am writing a program where i want to overload the multiplication operator to enable multiplication of two complex numbers as in algebra:

(a+bi)*(c+di) =(ac-bd)+(ad+bc)i

Here's what i've tried so far:

CPP / C++ / C Code:
// Complex.cpp

#include <iostream>

using std::cout;

#include "Complex.h"

// Constructor
Complex::Complex( double r, double i )
  : real( r ), imaginary( i ) 
{ 
}

// Overloaded addition operator
Complex Complex::operator+( const Complex &operand  ) const
{
    return Complex( real + operand .real,
                    imaginary + operand .imaginary );
}

// Overloaded subtraction operator
Complex Complex::operator-( const Complex &operand  ) const
{
    return Complex( real - operand .real,
                    imaginary - operand .imaginary );
}

// Overloaded = operator
const Complex& Complex::operator=( const Complex &right )
{
    real = right.real;
    imaginary = right.imaginary;
    return *this;  // enables cascading
}

// How can i go about displaying a complex object in the form: a + bi
void Complex::print() const
{ 
    cout << real << " + " << imaginary << "i";
}

Thanks
Last edited by LuciWiz : 15-Sep-2009 at 11:53. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 15-Sep-2009, 12:05
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Operator Overloading


So.....what seems to be the problem?
  #3  
Old 15-Sep-2009, 12:20
bubaduff bubaduff is offline
New Member
 
Join Date: Sep 2009
Posts: 3
bubaduff is on a distinguished road

Re: Operator Overloading


i cant get the program to compile. I want atleast some ideas on how to modify it.
  #4  
Old 15-Sep-2009, 12:56
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Operator Overloading


If I understand you correctly, you did not write the Complex class that resides in Complex.cpp and Complex.h? You simply want to use it and modify it. I would start with something like:

CPP / C++ / C Code:
#include "Complex.h"

int main()
{
  // Declare a Complex variable
  Complex c(0.0, 0.0);

  return 0;
}

Then, tell us what errors the compiler is reporting to you. After we get all of the errors taken care of and can get a clean compile, we can start to modify the class.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:02.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.