Chapter 7

Mastering Classes and Objects

WHAT’S IN THIS CHAPTER?

  • How to use dynamic memory allocation in objects
  • The different kinds of data members you can have (static, const, reference)
  • The different kinds of methods you can implement (static, const, inline)
  • The details of method overloading
  • How to work with default parameters
  • How to use nested classes
  • How to make classes friends of other classes
  • What operator overloading is
  • How to write separate interface and implementation classes.

Chapter 6 started the discussion on classes and objects. Now it’s time to master their subtleties so you can use them to their full potential. By reading this chapter, you will learn how to manipulate and exploit some of the most complicated aspects of the C++ language in order to write safe, effective, and useful classes.

Many of the concepts in this chapter arise in advanced C++ programming, especially in the standard template library.

DYNAMIC MEMORY ALLOCATION IN OBJECTS

Sometimes you don’t know how much memory you will need before your program actually runs. As you know, the solution is to dynamically allocate as much space as you need during program execution. Classes are no exception. Sometimes you don’t know how much memory an object will need when you write the class. In that case, the object should dynamically allocate memory.

Dynamically allocated memory in objects provides several challenges, including freeing the memory, handling object copying, and handling object assignment.

The ...

Get Professional C++, Second 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.