Using Class Objects as Data Members

As was explained in Chapter 9, "C++ Class as a Unit of Modularization," the main purpose of the class construct in C++ is to let the programmer bind together those data and operations that logically belong together in the eyes of the class designer.

Almost all examples of C++ classes that appeared earlier in the book had data members of built-in types—integers, and floating point numbers. Some more complex examples had a character array as a data member. Actually, it was a pointer to a character array allocated on the heap. From the point of view of class composition, a pointer is similar to integers and floating point numbers. It has no internal structure of its own that is accessible from the outside.

Get Core C++ A Software Engineering Approach 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.