C++ For Dummies by Unknown

C++ For Dummies by Unknown

Author:Unknown
Language: eng
Format: epub
Published: 0101-01-01T00:00:00+00:00


Chapter 15: “Why Do You Build Me Up, Just to Tear Me Down, Baby?” 221

The constructor for Student is called first because it is declared first. Then the constructor for Teacher is called.

The member Teacher.c of class Course is constructed as part of building the Teacher object. The Course constructor gets a shot first. Each object within a class must construct itself before the class constructor can be invoked.

Otherwise, the main constructor would not know the state of its data members.

After all member data objects have been constructed, control returns to the

open brace, and the constructor for TutorPair is allowed to construct the remainder of the object.

Dissecting a Destructor

Just as objects are created, so are they destroyed (ashes to ashes, dust to dust).

If a class can have a constructor to set things up, it should also have a special member function to take the object apart. This member is called the destructor.

Why you need the destructor

A class may allocate resources in the constructor; these resources need to

be deallocated before the object ceases to exist. For example, if the constructor opens a file, the file needs to be closed before leaving that class or the program. Or, if the constructor allocates memory from the heap, this memory

must be freed before the object goes away. The destructor allows the class

to do these cleanup tasks automatically without relying on the application to

call the proper member functions.

Working with destructors

The destructor member has the same name as the class but with a tilde (~)

added at the front. (C++ is being cute again — the tilde is the symbol for the logical NOT operator. Get it? A destructor is a “not constructor.” Très clever.) Like a constructor, the destructor has no return type. For example, the class

Student with a destructor added appears as follows:

class Student

{

public:

Student()

{

semesterHours = 0;

gpa = 0.0;

}

222 Part III: Introduction to Classes

~Student()

{

// ...whatever assets are returned here...

}

protected:

int semesterHours;

double gpa;

};

The destructor is invoked automatically when an object is destroyed, or

in C++ parlance, when an object is destructed. That sounds sort of circular (“the destructor is invoked when an object is destructed”), so I’ve avoided

the term until now. For non-heap memory, you can also say, “when the

object goes out of scope.” A local object goes out of scope when the func-

tion returns. A global or static object goes out of scope when the program

terminates.

But what about heap memory? An object that has been allocated off the heap

is destructed when it’s returned to the heap using the delete command. This is demonstrated in the following DestructMembers program:

// DestructMembers - this program both constructs and

// destructs a set of data members

//

#include <cstdio>

#include <cstdlib>

#include <iostream>

using namespace std;

class Course

{

public:

Course() { cout << "constructing course" << endl; }

~Course() { cout << "destructing course" << endl; }

};

class Student

{

public:

Student() { cout << "constructing student" << endl;}

~Student() { cout << "destructing student" << endl; }

};

class Teacher

{

public:

Teacher()

{

cout << "constructing teacher" << endl;

pC = new Course;

}

Chapter 15: “Why Do You Build Me Up, Just to Tear Me Down, Baby?” 223

~Teacher()

{

cout << "destructing teacher" << endl;

delete pC;

}

protected:

Course*



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.