Chapter 15

“Why Do You Build Me Up, Just toTear Me Down, Baby?”

In This Chapter

arrow Creating and destroying objects

arrow Declaring constructors and destructors

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

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

Get C++ For Dummies, 7th 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.