6 Replies - 1132 Views - Last Post: 21 February 2012 - 12:37 AM Rate Topic: -----

#1 Sahle123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Good examples of overloading operators

Posted 19 February 2012 - 09:45 PM

Hello,

I have a question. I am a novice programmer in C++ and I'm not totally familiar with overloading operators, especially in a class.

I have checked out so many websites and I have seen very poor examples of overloading operators--none of them make sense to me.

Can anyone explain or show me some example code of overloading operators?

Thank you :)

Is This A Good Question/Topic? 0
  • +

Replies To: Good examples of overloading operators

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: Good examples of overloading operators

Posted 19 February 2012 - 10:19 PM

Here's something I threw together to add imaginary numbers:
using namespace std;
#include <iostream>
struct imaginary {
  int A, B; // imaginary numbers are A + Bi wher i = sqrt(-1)
};

class imaginary_numbers {
  public:
  imaginary num;
  
  imaginary_numbers(int a, int b ) {
    num.A = a;
    num.B = b;
  }
  
  void show_number() {
    cout << num.A << " + " << num.B <<"i" << endl;
  }
    
};

imaginary_numbers operator +(imaginary_numbers a, imaginary_numbers b ) {
  b.num.A += a.num.A;
  b.num.B += a.num.B;
  return b;
}

int main (int argc, const char * argv[])
{
  imaginary_numbers First(2,3);
  imaginary_numbers Second(12,43);
  imaginary_numbers Result(0,0);
  Result = First + Second;
  First.show_number();
  Second.show_number();
  Result.show_number();
  return 0;
}


This post has been edited by CTphpnwb: 19 February 2012 - 10:27 PM

Was This Post Helpful? 2
  • +
  • -

#3 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2156
  • View blog
  • Posts: 4,219
  • Joined: 30-May 10

Re: Good examples of overloading operators

Posted 19 February 2012 - 10:24 PM

I can suggest an exercise for you to try.

http://en.wikipedia..../Roman_numerals

Implement a Roman class, where addition is overloaded.

Roman a("MCM");
Roman b("LXXXIV");
Roman c = a + b;
cout << c;


Was This Post Helpful? 2
  • +
  • -

#4 cryptoxic   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 22-January 11

Re: Good examples of overloading operators

Posted 20 February 2012 - 12:37 AM

An example of common operator overloading, is matrix multiplication:
matrix A;
matrix B;
matrix C;
C = A*B;
Was This Post Helpful? 0
  • +
  • -

#5 nuclearfroggy   User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Good examples of overloading operators

Posted 20 February 2012 - 01:09 AM

The overloaded operators in the string class are also pretty nifty. It allows you to compare two strings with == and != and allows you to append a string or character onto another string with + and +=.

I often overload the insertion and extraction operator << and >>. I find this quite useful for debugging and stuff, making the contents of my classes easy to output.
Was This Post Helpful? 1
  • +
  • -

#6 Sahle123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Re: Good examples of overloading operators

Posted 20 February 2012 - 10:21 PM

Thanks guys. :)

I just have another question. How does the program known whether to use the regular operator or the user made overloaded one. I've tested out some of my programs and they all seem to function well using the operators how I wanted them to work even though I did have no code that explicitly differentiates the two.
Was This Post Helpful? 0
  • +
  • -

#7 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Good examples of overloading operators

Posted 21 February 2012 - 12:37 AM

If you define a new operator it will always use the new definition.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1