operator overloading issues!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: operator overloading issues!

  1. #1
    Join Date
    Jun 2013
    Location
    Langrood, Iran
    Posts
    7

    operator overloading issues!

    hey guys
    I wrote a simple Complex Class and overload input/output and +/- Operators in it!But there is something that doesn't work correctly!I can print an object of that class but I can't print sum of two object or something like that!

    Here is My Code:

    Code:
    #ifndef COMPLEX_H
    #define COMPLEX_H
    #include <iostream>
    using namespace std;
    
    class Complex
    {
    	friend ostream & operator<<(ostream &, const Complex &);
    	friend istream & operator>>(istream &, Complex &);
        public:
            Complex(double = 1, double = 1);
    		Complex & operator+(const Complex &);
    		Complex & operator-(const Complex &);
    		//const Complex & operator=(const Complex &);
        private:
            double real, imag;
    };
    
    #endif // COMPLEX_H
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include "Complex.h"
    
    using namespace std;
    
    Complex::Complex(double r, double i)
    {
        real = r;
        imag = i;
    }
    
    Complex & Complex::operator+(const Complex &c)
    {
    	Complex result(this->real + c.real, this->imag + c.imag);
    	return result;
    }
    	
    Complex & Complex::operator-(const Complex &c)
    {
    	Complex result(this->real - c.real, this->imag - c.imag);
    	return result;
    }
    
    ostream & operator<<(ostream &output, const Complex &c)
    {
    	output << "(" << c.real << ", " << c.imag << ")";
    	return output;
    }
    
    
    istream & operator>>(istream &input, Complex &c)
    {
    	string strtmp;	
    	input >> strtmp;
    	for(int i = 0;i < strtmp.length();i++)
    	{
    		if(strtmp[i] == '(' || strtmp[i] == ')' || strtmp[i] == ',') 
    			strtmp[i] = ' ';
    	}
    	c.real = stod(strtmp);
    	input >> strtmp;
    	for(int i = 0;i < strtmp.length();i++)
    	{
    		if(strtmp[i] == '(' || strtmp[i] == ')' || strtmp[i] == ',') 
    			strtmp[i] = ' ';
    	}
    	c.imag = stod(strtmp);
    	return input;
    }
    Code:
    // Complex.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include "Complex.h"
    
    using namespace std;
    
    int main()
    {
    	Complex c1(2, 2), c2(3, 3), c3, c4;
    	c3 = c1 + c2;
    	cout << c3;
    	cout << (c1 + c2);
        return 0;
    }
    cout << c3 is Working fine But cout << c1 + c2 is not giving me correct output!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: operator overloading issues!

    Quote Originally Posted by N@R!MAN
    I wrote a simple Complex Class
    I love the oxymoron

    Let's examine your definition of member operator+:
    Code:
    Complex & Complex::operator+(const Complex &c)
    {
    	Complex result(this->real + c.real, this->imag + c.imag);
    	return result;
    }
    So, you create a Complex object that is initialised to be the sum of the current object and c, as complex numbers, then you return this newly created Complex object named result. However, the return type is Complex&, hence you return a reference to result. But once control leaves this function, result is destroyed, hence the caller receives a reference to an object that no longer exists. That is a Bad Thing.

    Furthermore, suppose that the current object is const, e.g.,
    Code:
    const Complex c1(1, 2);
    const Complex c2(3, 4);
    Complex c3 = c1 + c2;
    The above fails because operator+ is a non-const member function. As such, you should declare operator+ to be a const member function that returns a Complex object by value.

    Now, chances are, you would also like to allow:
    Code:
    Complex c1(1, 2);
    const Complex c2(3, 4);
    c1 += c2;
    If so, then another approach is to overload operator+= as a member function, then declare operator+ as a non-member non-friend function, e.g.,
    Code:
    Complex operator+(Complex lhs, const Complex& rhs)
    {
        return lhs += rhs;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2013
    Location
    Langrood, Iran
    Posts
    7

    Re: operator overloading issues!

    tnx, I get it!

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: operator overloading issues!

    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center