Thread: arithmetic operator friend functions

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    arithmetic operator friend functions

    i can't quite figure out how the arithmetic operators are supposed to be used, as 2 parameters are being passed in:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    class complex
    {
    private:
        double real; // real part
        double imag; // imaginary part
    public:
        complex(double x=0.0, double y=0.0);    
        double magnitude() const; // return magnitude
        double realPart() const; // return real part
        double imagPart() const; // return imaginary part
        
        complex operator- ();
    
        friend complex operator+ (const complex& lhs, const complex &rhs);
        friend complex operator- (const complex& lhs, const complex &rhs);
        friend complex operator* (const complex& lhs, const complex &rhs);
        friend complex operator/ (const complex& lhs, const complex &rhs);
        friend bool operator== (const complex& lhs, const complex &rhs);
        friend ostream& operator << (ostream& ostr, const complex &x);
        friend istream& operator >> (istream& istr,  complex &x);
    };
    
    double complex::magnitude() const
    {
        double temp = 0.0;
        temp = (real * real) + (imag * imag);
        temp = sqrt(temp);
    }
    
    double complex::realPart() const
    {
        return real;
    }
    
    double complex::imagPart() const
    {
        return imag;
    }
    
    complex operator+ (const complex& lhs, const complex &rhs)
    {
        return complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
    }
    
    complex operator- (const complex& lhs, const complex &rhs)
    {
        return complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
    }
    
    int main()
    {
       // complex number i= 0 + 1i
       complex i(0,1),  z1, z2;
    
       // input values
       cout << "Enter two complex numbers: ";
       cin >> z1 >> z2;
    
       cout << "Test the binary arithmetic operators:" << endl;
       cout << " z1 + z2 = " << (z1 + z2) << endl;
       cout << " z1 - z2 = " << (z1 - z2) << endl;
       cout << " z1 * z2 = " << (z1 * z2) << endl;
       cout << " z1 / z2 = " << (z1 / z2) << endl;
    
       // test relational equality
       if (z1 == z2)
           cout << z1 << " = " << z2 << endl;
       else
           cout << z1 << " != " << z2 << endl;
    
       // verify that i*i = -1 and that -i*i = 1
       cout << "i*i = " << i*i << endl;
     
       cout << "-i*i = " << -i*i << endl;
       return 0;
    }
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Of course there are 2 parameters, each reflects one of the operands.

    Code:
    x = y + z
    gets translated into
    Code:
    x = operator+(&y, &z);

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    ok, that makes sense, but then why are both parameters a class of complex? that tells me that when i enter the addition function, now I am dealing with 3 class instances. what do i do with them? if what you say is true, then i would guess that either both parameters should be double or there should be only one parameter of the complex class. and my code gives me a few linker errors, saying something about complex::complex(double, double)
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Where are you getting the error? Why would doubles have anything to do with it? When using ints, you would have int a, b and c if a = b + c. Same with complex. Just because you can use doubles to initialize a complex instance, doesn't mean you need them around when adding them (except internally in the operator+ function).

    When adding two objects, you don't modify the two objects, you just add them and create a third value that is their sum. If you want to modify one of the objects, you would use += instead of +.

    Note that your operator+ actually does subtraction. Also not that there is an std::complex, so you should be careful about calling your own class that, especially when you are using a usingdirective for the std namespace.

    Finally, a common way to implement operator+ and the other three arithmetic operators is to implement += first (along with -=, *= and /=), and then use += to do the work of operator+:
    Code:
    complex operator+ (const complex& lhs, const complex &rhs)
    {
    	complex temp(lhs);
    	temp += rhs;
    	return temp;
    }
    >> x = operator+(&y, &z);
    Technically it is just translated to x = operator+(y, z);

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    i get 4 errors saying
    [Linker error] undefined reference to `complex::complex(double, double)'
    and one saying [Linker error] undefined reference to `complex:perator-()'
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have several functions in complex that you declared and use but you never implement. In the posted code there is no implementation of the constructor that takes two doubles. There is no implementation of operator*, operator/, operator==, operator<< or operator>>. You've got to implement those before it will link (or comment out the code in main that uses them).

  7. #7
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    wow... i forgot to implement the default constructor. that might help just a little. and the rest of the functions, i just didn't implement them in the code i posted. i think i got it under control now though. thanks
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  8. #8
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    while in the operator you indeed momentarilly have 3 instances, but that's to be expected.
    There are the two operands and then there's the result.

    Gets more interesting when you get around to implementing operators to allow adding of real numbers to complex numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM