O'Reilly logo

Professional C++, Second Edition by Scott J. Kleper, Nicholas A. Solter, Marc Gregoire

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required