CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    98

    [RESOLVED] overloading operator+

    hi all,
    I'm adding two fractions but couldn't figure it out, i tried it for hours. I'm getting 2 errors in it, please tell me how to get it free from bugs.
    Code:
    #include <iostream>
    using namespace std;
    
    class Fractions
    {
    	
    private:
    	int num;
    	int den;
    public:
    	Fractions();
    	~Fractions();
    	Fractions(int n,int d);
    	Fractions(const Fractions& f);
    	Fractions operator+(const Fractions& f1); 
    	void print(); 
    };
    int main()
    {
    	Fractions f(1/3);
      
    	Fractions r;
    	r=f.operator +(1/1);
    	r.print();
    
    	
    
            return 0;
    }
    Fractions::Fractions()
    {
    	num=den=0;
    	
    }
    Fractions::~Fractions()
    {
    
    }
    Fractions::Fractions(int n,int d)
    {
    	num=n;
    	den=d;
    }
    Fractions::Fractions(const Fractions& f)
    {
    	num=f.num;
    	den=f.den;
    }
    Fractions Fractions::operator +(const Fractions& f1)
    {
    	Fractions sum;
    	int lcm;
    	int r,s;
    	lcm=f1.den*this->den;
    	if(r=lcm/f1.den)
    	{
    		r=r*f1.num;
    	}
    	if(s=lcm/this->den)
    	{
    		s=s*this->num;
    	}
    	sum.num=r+s;
    	sum.den=lcm;
    	return sum;
    	
    }
    void Fractions::print()
    {
    	cout<<num/den;
    }

  2. #2
    Join Date
    Oct 2010
    Posts
    98

    Re: overloading operator+

    I've figured it out,

  3. #3
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: [RESOLVED] overloading operator+

    operator + is better written as a nonmember which kicks down to the member operator +=. For a full discussion on why some operators are better written as nonmenbers read this.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  4. #4
    Join Date
    Oct 2010
    Posts
    98

    Re: [RESOLVED] overloading operator+

    thanks

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: [RESOLVED] overloading operator+

    Quote Originally Posted by Russco View Post
    operator + is better written as a nonmember which kicks down to the member operator +=.
    Is += always naturally a member operator?

    Say you have an immutable object for example the int object 42. This object itself can never change and suddenly become 43 for example. It stays 42 forever.

    This,

    n += 1;

    is defined to be equivalent to this,

    n = n + 1;

    for the int class.

    Now say n holds the 42 object. In no case is 42 modified to become 43. Instead a new object 43 based on the 42 and 1 objects is returned and assigned to the n variable.

    The same applies to all immutable classes in my view. To such classes += just isn't a natural member. An immutable classes cannot have any operator that changes it.

    My point is that this idea that one should implement += as a member operator and then + as a non-member operator in terms of += is seriously flawed for immutable classes.

    (I'm aware that int technically isn't a class, but conceptually it is and it serves as the preferred model for numerical value classes.)
    Last edited by nuzzle; May 23rd, 2011 at 01:57 PM.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: [RESOLVED] overloading operator+

    Quote Originally Posted by nuzzle View Post
    ...
    I'm not sure I understand what you are saying.

    You seem to be saying that for classes that don't have operator+=, operator+ shouldn't be implemented in terms of operator+=?

    The argument is more of "Once your class has an operator+=, then operator+ can naturally be implemented as a non member in terms of operator+". If your class doesn't have operator+=, then, well... you just write operator+ straight up.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured