As opposed to a constructor, a destructor is called when a program has finished using an instance of an object. A destructor does the cleaning behind the scenes. Like the default constructor, the compiler always create a default destructor if you don’t create one. Like the default constructor, a destructor also has the same name as its object. This time, the name of the destructor starts with
a tilde.
To create your own destructor, in the header file, type ~ followed by the name of the object. We use #ifndef directive to define the class. Here is an example:
#ifndef BricksH #define BricksH class TBrick { public: TBrick(); TBrick(double L, double h, double t); TBrick(const TBrick &Brk); ~TBrick(); double getLength() const; void setLength(const double l); double getHeight() const; void setHeight(const double h); double getThickness() const; void setThickness(const double t); double CementVolume(); void ShowProperties(); private: double Length; double Height; double Thickness; }; #endif
As done with a default constructor, you don’t need to put anything in the implementation of a destructor. In fact, when a program terminates, the compiler can itself destroy all of the objects and variables that your program has used. The only true time you will be concerned with destroying objects is if the objects were created dynamically, which we will learn when studying pointers.
You can implement your destructor in the header file by just providing it with empty parentheses:
#ifndef BricksH #define BricksH class TBrick { public: ... ~TBrick() {} ... private: ... }; #endif
Otherwise, you can also implement it in the cpp file with empty parentheses. Here is an example:
TBrick::~TBrick() { }
Having difficulties understanding the code and output then you can get coding help and dive into expert knowledge and support tailored to your programming needs.
The Copy Constructor
Copying an Object
After creating an object and assigning appropriate values to its members, you can perform any regular operation on it. Although this gets a little particular with objects, which will be expanded when learning about operator overloading, you can assign an object to another object. We have already learned:
How to assign | Example |
A value to a variable | int a = 250; |
The value of one variable to another | NbrOfBoys = NbrOfGirls; |
A value to an object’s member | Video.Category = ‘Drama’ |
Assigning a variable to another is equivalent to making a copy of that variable. As you assign a variable to another, you can assign one object to another. Both objects must be recognizably equivalent to the compiler. Imagine you want to build the same brick twice. All you have to do is to assign one brick to another, which is take care of in the following main() function:
//--------------------------------------------------------------------------- #include <stdio.h> using namespace std; #include "Bricks.h" //--------------------------------------------------------------------------- int main() { // Declaring one brick TBrick Hott(5.25, 3.55, 3.55); Hott.ShowProperties(); cout << endl; // Assigning one brick to another TBrick Bosco = Hott; Bosco.ShowProperties(); return 0; } //---------------------------------------------------------------------------
Here is an example of running the program:
Foundation Brick Properties Length = 5.25 Height = 3.55 Thickness = 3.55 Cement Volume = 17.44 Foundation Brick Properties Length = 5.25 Height = 3.55 Thickness = 3.55 Cement Volume = 17.44 Press any key to continue...
Notice that both orders display the same thing.
Using Destructors
To illustrate the construction and destruction effects of an object, create a new Console Application named Library
Change the content of the file as follows:
// #include <iostream> using namespace std; // class TBook { public: TBook(); // Constructor ~TBook(); // Destructor void Message() { cout << "\tI read the book\n"; } }; // TBook::TBook() { cout << "I see a book... Using the Constructor\n"; } // TBook::~TBook() { cout << "I close the book! Using the Destructor\n"; } // int main() { cout << "I am at the library\n"; { TBook A; } cout << "\nI didn't like that book. I will try another\n\n"; { TBook B; B.Message(); } cout << "\nI am getting out of the library\n\n"; return 0; } //
Execute the program to see the result:
I am at the library I see a book... Using the Constructor I close the book! Using the Destructor I didn't like that book. I will try another I see a book... Using the Constructor I read the book I close the book! Using the Destructor I am getting out of the library
Return to your programming environment
Reopen the Students project from the Students2 folder.
To create a destructor for our TStudent class, change the header file as follows:
#ifndef StudentsH #define StudentsH #include <iostream> using namespace std; class TStudent { public: TStudent(); TStudent(string F, string L); // Name Initializer TStudent(int DOB, int MOB, int YOB); // Initial Date of birth TStudent(string fn, string ln, int DOB, int MOB, int YOB); ~TStudent(); TStudent(const TStudent& S); void Display(); private: string FirstName; string LastName; int DayOfBirth; int MonthOfBirth; int YearOfBirth; }; #endif
Implement the destructor in the Students.cpp file as follows:
TStudent::~TStudent() { }
Test the program and return to your programming environment.