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
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.
The Mikado Method by Ola Ellnestam Daniel Brolund(22435)
Hello! Python by Anthony Briggs(21626)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(20184)
Dependency Injection in .NET by Mark Seemann(19563)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(19311)
Kotlin in Action by Dmitry Jemerov(19237)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(18775)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(17578)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(16967)
Grails in Action by Glen Smith Peter Ledbrook(16730)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(14220)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(12199)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(10923)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10597)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(10029)
Hit Refresh by Satya Nadella(9117)
The Kubernetes Operator Framework Book by Michael Dame(8539)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8365)
Robo-Advisor with Python by Aki Ranin(8306)