Abstract
Suppose that vl,v2, and v3 are three vectors and m is a matrix. Mathematically we can write
provided the dimensions of the matrix and vectors are compatible. In C++, we can define classes for vectors and matrices and redefine the meanings of +, *, and = such that vector addition and matrix-vector multiplication can be written exactly in the same way as the mathematical expressions above. This kind of extension of operators such as +, *, and = from built-in types to user-defined types is called operator overloading. Operator overloading enables the C++ code of many mathematical methods to resemble their algorithms, which can make programming in C++ easier and C++ programs more readable. In this chapter, various issues on operator overloading are discussed. Examples are complex numbers, vectors, and matrices, which are building blocks of many scientific programs. Note that standard C++ libraries include complex numbers (§7.4) and vectors (§7.5) and (§10.1.1). A simpler and easier-to-understand version is presented here to illustrate how operators are overloaded. A deferred-evaluation technique is also presented to improve the efficiency of overloaded composite operators. In the final section, operator overloading is applied to solve systems of linear equations using the conjugate gradient method.
This is a preview of subscription content, log in via an institution.
Buying options
Tax calculation will be finalised at checkout
Purchases are for personal use only
Learn about institutional subscriptionsPreview
Unable to display preview. Download preview PDF.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2001 Springer Science+Business Media New York
About this chapter
Cite this chapter
Yang, D. (2001). Operator Overloading. In: C++ and Object-Oriented Numeric Computing for Scientists and Engineers. Springer, New York, NY. https://doi.org/10.1007/978-1-4613-0189-9_6
Download citation
DOI: https://doi.org/10.1007/978-1-4613-0189-9_6
Publisher Name: Springer, New York, NY
Print ISBN: 978-1-4612-6566-5
Online ISBN: 978-1-4613-0189-9
eBook Packages: Springer Book Archive