Skip to main content

Operator Overloading

  • Chapter
  • 1285 Accesses

Abstract

Suppose that vl,v2, and v3 are three vectors and m is a matrix. Mathematically we can write

$$ \begin{gathered} v3 = v1 + v2; \hfill \\ v2 = v1 + m * v3; \hfill \\ \end{gathered} $$

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

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   54.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   69.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info
Hardcover Book
USD   99.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints 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

Publish with us

Policies and ethics