Chapter 15
“Why Do You Build Me Up, Just toTear Me Down, Baby?”
In This Chapter
Creating and destroying objects
Declaring constructors and destructors
Invoking constructors and destructors
Objects in programs are built and scrapped just like objects in the real world. If the class is to be responsible for its well-being, it must have some control over this process. As luck would have it (I suppose some planning was involved as well), C++ provides just the right mechanism. But first, a discussion of what it means to create an object.
Creating Objects
Some people get a little sloppy in using the terms class and object. What’s the difference? What’s the relationship?
I can create a class Dog that describes the relevant properties of man’s best friend. At my house, we have two dogs. Thus, my single class Dog has two instances, Jack and Scruffy. (Well, I think there are two instances — I haven’t seen Scruffy in a few days.)
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
A class describes a type of thing. An object is one of those things. An object is an instance of a class. There is only one class Dog, no matter how many dogs I have. ...