Overloading Operators

This is a discussion on Overloading Operators within the C++ Programming forums, part of the General Programming Boards category; hi, Can anyone teach me something about overloading operators ? and what is the purpose of it ? and can ...

  1. #1
    Unregistered
    Guest

    Talking Overloading Operators

    hi,

    Can anyone teach me something about overloading operators ? and what is the purpose of it ? and can you give me very simple example so that I can easily understand it ?

    Thanks
    curious mind

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Its very useful for handling your own user defined objects. For example I have a class Vector3d which comprises of x, y, and z values. Now in order to add vectors you need to add the x's, add the y's and add the z's. This is the purpose of operator overloading. Here's the code for the + operator: (the x,y, and z values are in an array)

    Code:
    //declaration inside class
    friend Vector3d operator + ( const Vector3d& a, const Vector3d& b );
    
    //definition
    Vector3d operator +( const Vector3d& a, const Vector3d& b )
    {
    	Vector3d temp;
    	int i;
    
    	for( i = 0; i < 3; i++ )
    	{
    		temp.xyz[ i ] = b.xyz[ i ] + a.xyz[ i ];
    	}
    
    	return temp;
    }
    Hope that clears it up a bit...
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 07:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21