Thread: How do I Overload these operators in this program?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    34

    How do I Overload these operators in this program?

    I have a program in which you have to overload the operators (all under a class called ComplexNum) >>, <<, +, -, /, and *. My program is that I must take two sets of numbers, each set having two numbers, and manipulate each with these operators. But here is the kicker, one of the numbers is an imaginary number. Ok, here is an example:
    ---------------
    Input file:
    2.5 -2.2
    1.0 1.0
    Output (what it is suppose to be)
    A = (2.5) + (-2.2)i
    B = (1.0) + (1.0)i

    A + B = (3.5) + (-1.2)i ....and so on
    ---------------
    Ok, I have been given for example, what to do with the overloaded assignment operator, such as
    >> extracts two parts of the complex number (or)
    << inserts the complex number with (a) + (b)i format (or)
    + is (a + c) + i(b+d)
    * is (ac-bd) + i(bc + ad)
    ....so on

    But how do I implent or do any of this? I was not instructed very well, and any help with the coding would be apreciated. Thanks,
    aaron

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    class ComplexNum
    {
        double real, imaginary;
    public:
        // Constructor
        ComplexNum( double realval = 0.0, double imagval = 0.0 ) : real(realval), imaginary(imagval) {}
        // Copy constructor
        ComplexNum( const ComplexNum& rhs )
        {
            real = rhs.real;
            imaginary = rhs.imaginary;
        }
        friend ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs );
        friend ostream& operator<<(ostream& out,const ComplexNum& rhs );
    };
    
    ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs )
    {
        return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary);
    }
    
    ostream& operator<<(ostream& out,const ComplexNum& rhs )
    {
        return out << "(" << rhs.real << ") + (" << rhs.imaginary << ")i";
    }
    
    int main()
    {
        ComplexNum A(2.5,-2.2), B(1.0,1.0), C;
    
        C = A + B;
    
        cout << "A is: " << A << endl;
        cout << "B is: " << B << endl;
        cout << "C is: " << C << endl;
    
        return 0;
    }
    Output:
    Code:
    A is: (2.5) + (-2.2)i
    B is: (1) + (1)i
    C is: (3.5) + (-1.2)i
    You can do the other operators yourself I'm sure.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Hey, cool. Thanks a lot for your help!!!

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Quick question, I am going to be reading from an input file. How do I read two numbers from the input file and put them both into A like this? I belive this is why I have to overload the >> function?
    Last edited by advocation; 04-23-2005 at 05:09 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I would guess you are supposed to implement the operator>> in order to read the numbers from your input file. Try and have a go at it.

    Code:
    class ComplexNum
    {
        ...
    public:
        ...
        friend istream& operator>>(istream& is, ComplexNum& rhs );
    };
    
    istream& operator>>(istream& is, ComplexNum& rhs )
    {
        // Figure out what to put here
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    cross posted.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Ok, I am now getting these errors with this code:
    Code:
    #include <fstream.h>
    #include <ostream.h>
    
    class ComplexNum
    {
       public:
          float real;
          float imaginary;
          ComplexNum( double realval = 0.0, double imagval = 0.0 ) : real(realval), imaginary(imagval) {}
          ComplexNum(const ComplexNum& rhs)
          {
             real = rhs.real;
             imaginary = rhs.imaginary;
          }
          friend ostream& operator<<(ostream& outFile, const ComplexNum&
                                       rhs);
          friend istream& operator>>(istream& inFile, ComplexNum& rhs);
          friend ComplexNum operator+(ostream& outFile, const ComplexNum&
                                      rhs);
    };
    
    ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs)
    {
       return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary);
    }
    
    ostream& operator<<(ostream& outFile, const ComplexNum& rhs)
    {
       return outFile << "(" << rhs.real << ") + (" << rhs.imaginary << ")i";
    }
    Now, these errors:
    /tmp/cclNvRtt.o(.text+0x0): In function `operator+(ComplexNum const&, ComplexNum const&)':
    : multiple definition of `operator+(ComplexNum const&, ComplexNum const&)'
    /tmp/ccCuUZWQ.o(.text+0x0): first defined here
    /tmp/cclNvRtt.o(.text+0x44): In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)':
    : multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)'
    /tmp/ccCuUZWQ.o(.text+0x44): first defined here
    collect2: ld returned 1 exit status

    Any ideas why??
    Last edited by advocation; 04-24-2005 at 09:50 AM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by advocation
    Ok, I am now getting these errors with this code:
    Code:
    #include <fstream.h>  // Use <fstream> instead
    #include <ostream.h>  // Use <iostream> instead
    using namespace std;  // Add this
    
    class ComplexNum
    {
    public:
        ...
        friend ostream& operator<<(ostream& outFile, const ComplexNum& rhs);
        friend istream& operator>>(istream& inFile, ComplexNum& rhs);
        friend ComplexNum operator+(ostream& outFile, const ComplexNum&
                                      rhs);
    };
    
    ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs)
    {
       return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary);
    }
    
    ostream& operator<<(ostream& outFile, const ComplexNum& rhs)
    {
       return outFile << "(" << rhs.real << ") + (" << rhs.imaginary << ")i";
    }
    Now, these errors:
    /tmp/cclNvRtt.o(.text+0x0): In function `operator+(ComplexNum const&, ComplexNum const&)':
    : multiple definition of `operator+(ComplexNum const&, ComplexNum const&)'
    /tmp/ccCuUZWQ.o(.text+0x0): first defined here
    /tmp/cclNvRtt.o(.text+0x44): In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)':
    : multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)'
    /tmp/ccCuUZWQ.o(.text+0x44): first defined here
    collect2: ld returned 1 exit status

    Any ideas why??
    Your definition of operator+ does not match the declaration of it. These need to be the same. The operator+ function should take two const ComplexNum objects as arguments. What is that ostream object doing there?

    You really should be using the newer headers (the ones without the .h extension). I.e. fstream instead of fstream.h and so on. As far as the ostream header, you are going to need the istream header as well since you are also going to be dealing with input streams so you might as well just include the iostream header which includes everything you need for both input and output streams. Using the newer headers means you will have to deal with namespaces, at this stage it is probably easiest to just put a using namespace std; line after all your header statements.

    Don't know specifically what might be causing the problem with the operator<< function. Make the other fixes and see if you still get the error.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Cool, no errors. Yeah, that was just a stupid mistake when I put in ostream into the function declaration instead of the other const ComplexNum. I changed everything around though, and I have no errors; I am gonna finish coding it, so I will keep you posted if it all goes well (which I don't see why it shouldn't).

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    All works fine. Thanks once again for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM