I've read about 6 tutorials on this but for some reason it makes no sense to me. Could someone possibly work with me to help me understand this concept? Such as how would you overload something like this: int result = 2 + 5;

Member Avatar for iamthwee

That really is a poor example because that just works already.

Google operator overloading fractions /complex numbers

As these are the most frequently used.

Okay how about this example:

class complex{ protected:

    int a;//real

    int b;//imaginary

public:

    complex(int c, int d){

        a=c;

        b=d;

    }

    complex operator+(const complex& c) const {

        return complex result(a + c.a, b + c.b);

    }

    complex operator-(const complex& c) const {

        return complex(a - c.a, b - c.b);

    }

    void printNumber(){

        cout<<a<<” + “<<b<<”i”;

    }

};

int main(){

    complex r1(5, 3);

    complex r2(3, 1);

    complex r3 = r1 + r2;//adding r1 + r2 using operator overloading

    return 0;

}

Where is the operator overload happening in this example?

Member Avatar for iamthwee

There isn't anything really special about operator overloading...

In fact the same functionality can be achieved using named functions if it helps to clarify matters.

I like to think of overloading as syntax candy.

for example you can do:

Fraction a;
Fraction b;

a.addFraction(b);

instead of explicitly a + b

Edit: And your example is kinda broken.

complex operator+(const complex& c) const {

    return complex result(a + c.a, b + c.b);

}

That in red looks out of place, I'd remove it.

cout<<a<<" + "<<b<<"i";

These speech marks are wrong it should be "

And you probably want a to print the numbers so you can see them at the command prompt.

int main(){

    complex r1(5, 3);

    complex r2(3, 1);

    complex r3 = r1 + r2;//adding r1 + r2 using operator overloading

    r3.printNumber();
    cin.get();
    return 0;
}

So overloading operators is like making a new command. In your example addFraction is defined somewhere else, right? The 'a.addFraction' part, what does the 'a.' do?

EDIT:: Yeah I noticed that too when I took the code from the website and tried to run it.

Member Avatar for iamthwee

Basically, instead of writing the function addFraction and then calling it a.addFraction(b), where a and b are Fraction objects for example a could be 1/2 and b might be 1/3.

You just write:

a + b and it does the same thing.

As long as you define how you overload the + operator. In your example it is defined here as being:

complex operator+(const complex& c) const {

return complex result(a + c.a, b + c.b);

}

The std::string is a perfect example.

For example, when you concatenate two strings you simply write:

a = "hello";
b = "ugly";

c = a + b;

Now you could have defined a function addString() , which might be called a.addString(b) , but from an onlooker's point of view a + b looks cleaner?

Just think of it as syntax candy.

Okay I still don't understand some basic C++, perhaps that's the issue. '(const complex& c)' I know the const makes it a constant, what does the '&' do after the 'complex&'? And why is the 'c' there? Also, in 'a + c.a,' what does the 'c.a' mean?

Member Avatar for iamthwee

& is pass by reference I believe and the c is just an arbitrary variable name it could be anything you want.

E.g:

complex operator+(const complex& blah) const {

        return complex (a + blah.a, b + blah.b);

    }

*Edit: If you haven't written classes before or understood the concepts of pass by reference etc, I would do that as a pre-requisite to operator overloading defo!

I know I'm going to read up on that, I kind of got pushed into this class. I switched from one semester of Java to a second semester C++ class, so I'm a semester behind, lol.

Thank you for your help though.

I have an assignment in which I have to overload the multiplication, division, and all relational and equality operators.

Prog4.cpp

#include <iostream>
#include "Hugeint.h"

int main()
{
   HugeInt n1( 7654321 );
   HugeInt n2( 7891234 );
   HugeInt n3( "99999999999999999999999999999" );
   HugeInt n4( "1" );
   HugeInt n5;

   cout << "n1 is " << n1 << "\nn2 is " << n2
      << "\nn3 is " << n3 << "\nn4 is " << n4
      << "\nn5 is " << n5 << "\n\n";

   n5 = n1 + n2;
   cout << n1 << " + " << n2 << " = " << n5 << "\n\n";

   cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 ) << "\n\n";

   n5 = n1 + 9;
   cout << n1 << " + " << 9 << " = " << n5 << "\n\n";

   n5 = n2 + "10000";
   cout << n2 << " + " << "10000" << " = " << n5 << endl;
   return 0;
} // end main1


Hugeint.cpp

#include <iostream>
using namespace std;
#include <cctype> // isdigit function prototype
using std::isdigit;

#include <cstring> // strlen function prototype
using std::strlen;

#include "Hugeint.h" // HugeInt class definition

// default constructor; conversion constructor that converts
// a long integer into a HugeInt object
HugeInt::HugeInt( long value )
{
   // initialize array to zero
   for ( int i = 0; i <= 29; i++ )
      integer[ i ] = 0;

   // place digits of argument into array
   for ( int j = 29; value != 0 && j >= 0; j-- )
   {
      integer[ j ] = value % 10;
      value /= 10;
   } // end for
} // end HugeInt default/conversion constructor

// conversion constructor that converts a character string
// representing a large integer into a HugeInt object
HugeInt::HugeInt( const char *string )
{
   // initialize array to zero
   for ( int i = 0; i <= 29; i++ )
      integer[ i ] = 0;

   // place digits of argument into array
   int length = strlen( string );

   for ( int j = 30 - length, k = 0; j <= 29; j++, k++ )

      if ( isdigit( string[ k ] ) )
         integer[ j ] = string[ k ] - '0';
} // end HugeInt conversion constructor

// addition operator; HugeInt + HugeInt
HugeInt HugeInt::operator+( const HugeInt &op2 ) const
{
   HugeInt temp; // temporary result
   int carry = 0;

   for ( int i = 29; i >= 0; i-- )
   {
      temp.integer[ i ] =
         integer[ i ] + op2.integer[ i ] + carry;

      // determine whether to carry a 1
      if ( temp.integer[ i ] > 9 )
      {
         temp.integer[ i ] %= 10;  // reduce to 0-9
         carry = 1;
      } // end if
      else // no carry
         carry = 0;
   } // end for

   return temp; // return copy of temporary object
} // end function operator+

// addition operator; HugeInt + int
HugeInt HugeInt::operator+( int op2 ) const
{
   // convert op2 to a HugeInt, then invoke
   // operator+ for two HugeInt objects
   return *this + HugeInt( op2 );
} // end function operator+

// addition operator;
// HugeInt + string that represents large integer value
HugeInt HugeInt::operator+( const char *op2 ) const
{
   // convert op2 to a HugeInt, then invoke
   // operator+ for two HugeInt objects
   return *this + HugeInt( op2 );
} // end operator+

// overloaded output operator
ostream& operator<<( ostream &output, const HugeInt &num )
{
   int i;

   for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ )
      ; // skip leading zeros

   if ( i == 30 )
      output << 0;
   else

      for ( ; i <= 29; i++ )
         output << num.integer[ i ];

   return output;
} // end function operator<<

Hugeint.h

#ifndef HUGEINT_H
#define HUGEINT_H
#include <iostream>
using namespace std;
using std::ostream;

class HugeInt
{
   friend ostream &operator<<( ostream &, const HugeInt & );
public:
   HugeInt( long = 0 ); // conversion/default constructor
   HugeInt( const char * ); // conversion constructor

   // addition operator; HugeInt + HugeInt
   HugeInt operator+( const HugeInt & ) const;

   // addition operator; HugeInt + int
   HugeInt operator+( int ) const;

   // addition operator;
   // HugeInt + string that represents large integer value
   HugeInt operator+( const char * ) const;
private:
   short integer[ 30 ];
}; // end class HugeInt

#endif

How would I start to overload the addition operator, I dont want the answer I just want some help to get going.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.