Member Functions

You have already seen member functions (sometimes called methods) being used. For instance, string has a find() method, which is called using the dot (.) operator. When defining structures, you defined member variables; it is now time to see how to define member functions.

Putting Functions inside Structures

Consider the Point structure from Chapter 5, “Structures and Pointers,” which consists of two fields, representing the x and y coordinates of a point. In Chapter 5 we defined the function make_point(), which generates points in a convenient way. A common property of points is their length, defined as the distance from the origin—(0,0)—to the point (x,y). These two points define the corners of a triangle, so the Pythagoras ...

Get C++ By Example: UnderC Learning 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.