Mission Impossible and Constructors in C++

Every one who has seen any movie of the Mission Impossible franchise would remember the unique way of starting every mission.

You Tube link to the scene (58 seconds) :

utorial Linear Search in C++ , Programming
Code Basics, C Plus Plus

As soon as Ethan Hunt gets his retina or finger print scanned, the message automatically starts, “….your mission should you choose to accept it is to….” Remember?

Now what happens after the message is over?

The last words in the message are always – “This message will self-destruct in 5 seconds.”


A Class Constructor is a special member function of a class that is executed whenever we create new objects of that class,

Exactly like the message in the movie, which automatically starts after scanning the retina or finger print.

A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for initializing the values for member variables of the class.


A destructor is a special member function of a class that is executed whenever an object of it’s class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class,

Exactly like the message’s self-destructio

n in the movie.

A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

Example Code:

‪#‎include‬ <cstdlib>
#include <iostream>
#include <stdio.h>

using namespace std;

class Line

void setLength( double len ); // Function 1 Declaration

double getLength( void ); // Function 2 Declaration

Line(); // This is the constructor declaration

~Line(); // This is the destructor declaration

double length;


// Member functions definitions including constructor

Line::Line(void) // Constructor Definition
cout << “Object is being created” << endl;

Line::~Line(void) // Destructor Definition
cout << “Object is being deleted” << endl;

void Line::setLength( double len ) // Function 1 Definition
length = len;

double Line::getLength( void ) // Function 2 Definition
return length;

int main()
Line line;

// set line length
line.setLength(6.0); // Funtion 1 Called

cout << “Length: ” << line.getLength() <<endl; // Function 2 Called

return 0;



Object is being created
Length: 6
Object is being deleted



Please enter your comment!
Please enter your name here