Chapter 16 - Why Do You Build Me Up, Just to Tear Me Down, Baby PDF

Title Chapter 16 - Why Do You Build Me Up, Just to Tear Me Down, Baby
Author USER COMPANY
Course C++ Programming Techniques
Institution Charles Sturt University
Pages 12
File Size 231.5 KB
File Type PDF
Total Downloads 52
Total Views 141

Summary

Why Do You Build Me Up, Just to Tear Me Down, Baby?...


Description

Chapter 16

“Why Do You Build Me Up, Just to Tear Me Down, Baby?” In This Chapter 䊳 Creating and destroying objects 䊳 Declaring constructors and destructors 䊳 Invoking constructors and destructors

O

bjects 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 preplanning 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 class Dog has two instances, Trude (pronounced “Troo-duh”) and Scooter (well, I think there are two instances — I haven’t seen Scooter in a few days). 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. Objects are created and destroyed, but classes simply exist. My pets, Trude and Scooter, come and go, but the class Dog (evolution aside) is perpetual.

210

Part III: Introduction to Classes Different types of objects are created at different times. Global objects are created when the program first begins execution. Local objects are created when the program encounters their declaration. A global object is one that is declared outside of a function. A local object is one that is declared within a function and is, therefore, local to the function. In the following example, the variable me is global, and the variable notMe is local to the function pickOne(): int me = 0; void pickOne() { int notMe; }

According to the rules, global objects are initialized to all zeros when the program starts executing. Objects declared local to a function have no particular initial value. Having all data members have a random state may not be a valid condition for all classes. C++ allows the class to define a special member function that is invoked automatically when an object of that class is created. This member function, called the constructor, must initialize the object to a valid initial state. In addition, the class can define a destructor to handle the destruction of the object. These two functions are the topics of this chapter.

Using Constructors The constructor is a member function that is called automatically when an object is created. Its primary job is to initialize the object to a legal initial value for the class. (It’s the job of the remaining member functions to ensure that the state of the object stays legal.)

Why you need constructors You could initialize an object as part of the declaration — that’s the way the C programmer would do it — for example: struct Student { int semesterHours; float gpa; };

Chapter 16: “Why Do You Build Me Up, Just to Tear Me Down, Baby?” void fn() { Student s1 = {0, 0.0}; // or Student s2; s2.semesterHours = 0; s2.gpa = 0.0; // ...function continues... }

You could outfit the class with an initialization function that the application calls as soon as the object is created. Because this initialization function is a member of the class, it would have access to the protected members. This solution appears as follows: class Student { public: void init() { semesterHours = 0; gpa = 0.0; } // ...other public members... protected: int semesterHours; float gpa; }; void fn() { Student s; // create the object... s.init(); // ...then initialize it // ...function continues... }

The only problem with this solution is that it abrogates the responsibility of the class to look after its own data members. In other words, the class must rely on the application to call the init() function. If it does not, the object is full of garbage, and who knows what might happen. What is needed is a way to take the responsibility for calling the init() function away from the application code and give it to the compiler. Every time an object is created, the compiler can insert a call to the special init() function to initialize it. That’s a constructor!

211

212

Part III: Introduction to Classes

Making constructors work The constructor is a special member function that’s called automatically when an object is created. It carries the same name as the class to differentiate it from the other members of the class. The designers of C++ could have made up a different rule, such as: “The constructor must be called init().” It wouldn’t have made any difference, as long as the compiler could recognize the constructor. In addition, the constructor has no return type, not even void, because it is only called automatically — if the constructor did return something, there would be no place to put it. A constructor cannot be invoked manually.

Constructing a single object With a constructor, the class Student appears as follows: // // Constructor - example that invokes a constructor // #include #include #include using namespace std; class Student { public: Student() { cout...


Similar Free PDFs