Solved

Overloading the = operator for my complex number class in c++

Posted on 2007-11-24
11
489 Views
Last Modified: 2013-12-14
I am working on an assignment for my c++ class where I am doing operator overloading.  I have everything working but the = operator, I am not sure how to make it work.  I need to be able to do something like Complex myNumber = 4.4 5.5 and have it store that.  I have attached my code below.

I am being told too many identifiers on my = code... can you see aproblem?

#include <iostream>
 
 using std::cout;
 
 #include"complex1.h"// Complex class definition
 
 // constructor
 Complex::Complex( double realPart, double imaginaryPart )
    : real( realPart ),
      imaginary( imaginaryPart )
 {
    // empty body
 
 } // end Complex constructor
 
 // addition operator
 Complex Complex::operator+( const Complex &operand2 ) const
 {
      return Complex( real + operand2.real,
       imaginary + operand2.imaginary );
 
 } // end function operator+
 
 // subtraction operator
 Complex Complex::operator-( const Complex &operand2 ) const
 {
      return Complex( real - operand2.real,
       imaginary - operand2.imaginary );
 
 } // end function operator-

 // I used the algorhthym posted in our conference.  I had something similar but my parens were off I think :)
 Complex Complex::operator*( const Complex &operand2 ) const
 {

      return Complex( ((real * operand2.real)-(imaginary * operand2.imaginary)), ((real * operand2.imaginary)+(operand2.real * imaginary)) );
 
 } // end function operator*

 Complex Complex::operator=( const double realPart, const double imaginaryParft, const Complex& complexNumber ) const
 {
      complexNumber.getReal(realPar);
      complexNumber.getImaginary(imaginaryPart);
      return complexNumber
 
 }
std::ostream& operator<<(std::ostream& stream,const Complex& complexNumber)
 {
     stream << "(" << complexNumber.getReal() << " , " << complexNumber.getImaginary()<<")" << std::endl;
     return stream;
 }

 std::istream& operator>>(std::istream& stream,Complex& value)
 {
      double realPart, imaginaryPart;

      stream >> realPart >> imaginaryPart;
      value.getReal(realPart);
      value.getImaginary(imaginaryPart);

      return stream;  
 }


 
 bool Complex::operator==( const Complex &operand2 ) const
 {
       if (real == operand2.real && imaginary == operand2.imaginary)
             return true;
       else
             return false;
 }
 bool Complex::operator!=( const Complex &operand2 ) const
 {
       if (real != operand2.real || imaginary != operand2.imaginary)
             return true;
       else
             return false;
 }

-------------------------------

#ifndef COMPLEX1_H
#define COMPLEX1_H
 
class Complex {
 
public:
    Complex( double = 0.0, double = 0.0 );       // constructor
    Complex operator+( const Complex & ) const;  // addition
    Complex operator-( const Complex & ) const;  // subtraction
      Complex operator*( const Complex &operand2 ) const; // Multiplication
      Complex operator=( const double realPart, const double imaginaryParft, const Complex& complexNumber );
    bool operator==( const Complex &operand2 ) const;
    bool operator!=( const Complex &operand2 ) const;
    friend std::ostream &operator<<(std::ostream &stream, const Complex &value);
    friend std::istream &operator>>(std::istream &stream, Complex &value);


      //accessor functions
    double getReal() const { return real; }
    double getImaginary() const { return imaginary; }
    void getReal(double r) {  real = r; }
    void getImaginary(double i) { imaginary = i; }
private:
    double real;       // real part
    double imaginary;  // imaginary part

}; // end class Complex

#endif
0
Comment
Question by:urobins
  • 6
  • 5
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20344689
>>Complex myNumber = 4.4 5.5
You cannot do this. You can assign only one value (whatever type is it)
As a general rule, when you overload an operator you cannot modify the way it is used (precedence, association, etc).
0
 

Author Comment

by:urobins
ID: 20344693
So I could overload it to pass a complex number ?

like Complex number1, number2;
number1=number2?

Is that possible?

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20344702
yes, you can.
I cannot provide a direct answer for your question because it is a homework, but you can learn from this similar example and adapt to your needs:
http://www.fredosaurus.com/notes-cpp/oop-overloading/overloadassign.html
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:urobins
ID: 20344708
Thanks, I'll have a look at this, do you mind if I leave the question open in case I have a question after modifying my code?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20344710
Still you can post after closing a question.
0
 

Author Comment

by:urobins
ID: 20344719
Looking at the example you provided I tried this... but when I try to compile I get this error...  Am I trying to access this wrong?



complex1.cpp(48) : error C2440: '=' : cannot convert from 'const double *__w64 ' to 'double'
1>        There is no context in which this conversion is possible
complex1.cpp(49) : error C2440: '=' : cannot convert from 'const double *__w64 ' to 'double'
1>        There is no context in which this conversion is possible

Complex Complex::operator=(const Complex& operand2 )
 {
      if (this != &operand2)
      {
            real = &operand2.real;
            imaginary=&operand2.imaginary;
      }

      return *this;
 
 }

New Header file after change

#ifndef COMPLEX1_H
#define COMPLEX1_H
 
class Complex {
 
public:
    Complex( double = 0.0, double = 0.0 );       // constructor
    Complex operator+( const Complex & ) const;  // addition
    Complex operator-( const Complex & ) const;  // subtraction
      Complex operator*( const Complex &operand2 ) const; // Multiplication
      Complex operator=( const Complex& operand2 );
    bool operator==( const Complex &operand2 ) const;
    bool operator!=( const Complex &operand2 ) const;
    friend std::ostream &operator<<(std::ostream &stream, const Complex &value);
    friend std::istream &operator>>(std::istream &stream, Complex &value);


      //accessor functions
    double getReal() const { return real; }
    double getImaginary() const { return imaginary; }
    void getReal(double r) {  real = r; }
    void getImaginary(double i) { imaginary = i; }
private:
    double real;       // real part
    double imaginary;  // imaginary part

}; // end class Complex

#endif
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20344741
it should be:

Complex Complex::operator=(const Complex& operand2 )
 {
      if (this != &operand2)
      {
            real = operand2.real;    // removed ampersand
            imaginary = operand2.imaginary;  // removed ampersand
      }

      return *this;
}

0
 

Author Comment

by:urobins
ID: 20344746
ah, thanks, Just out of curiuosity why do I remove the ampersnad?
0
 

Author Closing Comment

by:urobins
ID: 31410822
Awesome Job thanks!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20344757
because the ampersand means "the address of", so you are trying to assign the address of a value to a value, not value to value.
0
 

Author Comment

by:urobins
ID: 20344760
oh, I was confused, I thought I always had to use the ampersand when working with values like that, but I see my error, thanks for your help, I appreciate you pointing me in teh right direction and not just giving me the answer!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question