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.
Deep Learning with Python by François Chollet(12699)
Hello! Python by Anthony Briggs(10007)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9874)
The Mikado Method by Ola Ellnestam Daniel Brolund(9871)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9786)
Dependency Injection in .NET by Mark Seemann(9421)
Hit Refresh by Satya Nadella(8870)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8386)
The Kubernetes Operator Framework Book by Michael Dame(7980)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7837)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7808)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7778)
Grails in Action by Glen Smith Peter Ledbrook(7771)
Practical Computer Architecture with Python and ARM by Alan Clements(7720)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7686)
Robo-Advisor with Python by Aki Ranin(7672)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7653)
Building Low Latency Applications with C++ by Sourav Ghosh(7566)
Svelte with Test-Driven Development by Daniel Irvine(7554)
