Defining class behavior

A class can define functions that can only be called through an instance of the class; such a function is often called a method. An object will have state; this is provided by the data members defined by the class and initialized when the object is created. The methods on an object define the behavior of the object, usually acting upon the state of the object. When you design a class, you should think of the methods in this way: they describe the object doing something.

    class cartesian_vector     {     public:         double x;         double y;         // other methods         double get_magnitude() { return std::sqrt((x * x) + (y * y)); }     };

This class has two data members, x and y, which represent the direction of a two-dimensional vector resolved ...

Get Beginning C++ Programming 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.