O'Reilly logo

C++ For Dummies, 7th Edition by Stephen R. Davis

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

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