More Overloading: A Vector Class
Let's look at another class design that uses operator overloading and friends, a class representing vectors. The class will also illustrate further aspects of class design, such as incorporating two different ways of describing the same thing into an object. Even if you don't care for vectors, you can use many of the new techniques in other contexts. A vector, as the term is used in engineering and physics, is a quantity having both a magnitude (size) and a direction. For example, if you push something, the effect depends on how hard you push (the magnitude) and in what direction you push. A push in one direction can save a tottering vase, whereas a push in the opposite direction can hasten its rush to doom. To ...
Get C++ Primer Plus, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.