C++
  Home arrow C++ arrow Page 4 - Operator Overloading in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Operator Overloading in C++
By: Ben Watson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 253
    2002-12-08

    Table of Contents:
  • Operator Overloading in C++
  • Definition
  • Overloading =
  • Overloading
  • Matrix Multiplication - Overloading * Again
  • Putting It All Together
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Operator Overloading in C++ - Overloading


    (Page 4 of 7 )

    Let's do some more interesting things with these matrices now. There are a number of mathematical operations that can be performed on a matrix, the simplest perhaps is addition. Addition of matrices requires that they both have the same dimensions. The resulting matrix is made by simply adding each number in the same position in each matrix and putting the answer in the same position as the two operands.

    [1 0] [4 3] [5 3]
    [2 1] + [-1 0] = [1 1]


    Since addition creates a new matrix, we don't want to return a reference, but an actual matrix object. Here's what the code looks like:

    const Matrix operator+( const Matrix& m) {
    assert(numCols==m.numCols && numRows==m.numRows);
    Matrix theMatrix(numRows,numCols);
    for (int r=0;r<numRows;r++)
    for (int c=0;c<numCols;c++)
    theMatrix[r][c]=matrix[r][c]+m.matrix[r][c];
    return theMatrix;
    }


    This adds the current matrix to the matrix in argument m. We first assure that the dimensions are equivalent, then create a new matrix with the same dimensions as the sources. It is then a simple matter of adding the two sources, and returning the new matrix. Notice that we perform the actual math on the types that make up each row.

    Matrix<float> a(2,2);
    Matrix<float> b(2,2);

    Matrix<float> c(2,3);

    Matrix<float> d=a+b;

    Matrix<float> e=a+c;//will fail assertion, abort program


    It is just as easy to define subtraction:

    const Matrix operator-( const Matrix& m) {
    assert(numCols==m.numCols && numRows==m.numRows);
    Matrix theMatrix(numRows,numCols);
    for (int r=0;r<numRows;r++)
    for (int c=0;c<numCols;c++)
    theMatrix[r][c]=matrix[r][c]-m.matrix[r][c];
    return theMatrix;
    }


    Overloading += and -=
    += and -= are operators that both add and change the current object, so the code to describe it is a combination of +/- and =. We'll return a reference again because we don't want to create a new object, but just modify the existing one, which called the function. We'll just add whatever is currently in it to the other matrix, and return a reference to itself:

    Matrix& operator+=(const Matrix& m) {
    assert(numCols==m.numCols && numRows==m.numRows);
    for (int r=0;r<numRows;r++)
    for (int c=0;c<numCols;c++)
    matrix[r][c]+=m.matrix[r][c];
    return *this;
    }

    Matrix& operator-=( const Matrix& m) {
    assert(numCols==m.numCols && numRows==m.numRows);
    for (int r=0;r<numRows;r++)
    for (int c=0;c<numCols;c++)
    matrix[r][c]-=m.matrix[r][c];
    return *this;
    }


    We can now expand our repertoire to include the following possibilities:

    Matrix<int> a(2,1);
    Matrix<int> b(2,1);

    a+=b;
    a-=b;


    Scaling: Overloading *
    Another useful operation we can perform on matrices is scaling. This just multiples every element in the matrix by a constant.

    [1 2] [2 4]
    [2 4] * 2 = [4 8]


    This operation returns a new matrix so we will return by value, not reference. The code should be trivial to read by now:

    const Matrix operator*(const float s) {
    Matrix theMatrix(numRows,numCols);
    for (int r=0;r<numRows;r++)
    for (int c=0;c<numCols;c++)
    theMatrix[r][c]=matrix[r][c]*s;
    return theMatrix;
    }


    We use a float as the scalar, and multiply it by every value in the source matrix, and return a new matrix. It is left up to the reader to implement *=. (/ and /= could also be implemented as inverses of scaling, but since scaling allows a float, this is mostly redundant.)

    More C++ Articles
    More By Ben Watson


       · This example was very useful to me in both the operator over loading examples and...
       · opertor overloading is very important topic in c++i think it,s to understading...
       · Op overloading is a beginner's subject. Why use templates in a beginner level...
       · hi my name is shakil khan and i belong to swat pakistan.and i need full page about...
       · Operating overloading allows you to pass different variable types to the same...
     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT