Skip to Content
Navigating C++ and Object-Oriented Design
book

Navigating C++ and Object-Oriented Design

by Paul Anderson, Gail Anderson
October 1997
Intermediate to advanced
800 pages
20h 48m
English
Pearson
Content preview from Navigating C++ and Object-Oriented Design

6.4. Why Overload Operators?

C++ doesn't force you to overload operators, so why bother? Let's consider the following.

int i, j, k;                  // integers 
float m, n, p;                // floats
k = i + j;                    // integer addition and assignment
p = m + n;                    // floating addition and assignment

Complex v, x, y;              // Complex numbers
String a, b, c;               // String objects
v = Complex_add(x, y);        // Complex addition and assignment
c = concat(a, b);             // String concatenation, assignment

The compiler overloads the + operator for built-in integer and float types by default, producing integer addition with i+j and floating addition with m+n. Here, Complex_add() and concat() perform Complex addition and String concatenation, but this approach forces users to remember function names and data ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exploring C++20: The Programmer's Introduction to C++

Exploring C++20: The Programmer's Introduction to C++

Ray Lischner

Publisher Resources

ISBN: 0135327482Purchase book