Destructor, C/C++ Programming


The purpose of destructor is to free the memory when the compiler memory is reduced or not enough to execute certain program. Sometimes there may several objects opened and it may occupy more memory which may lead to reduced memory for new objects to be created. Therefore to increase the memory; objects which are idle may be destruct or killed using the destructor.  The destructor is written in same way as constructor with following rule.

  • Destructor should be preceded with (tilde).
  • Destructor cannot have any argument or return type.
  • Destructor is initiated implicitly.
  • One destructor only for each class.
  • Destructor must be public.
  • Destructor can be defined anywhere in the public generally it is written at the end.
  • Destructor can have prototype.

Destructor should have the class name.


class item

{ int number; float cost; public:

void putdata(void)

{cout<<"This is a test for destructor";






{cout<<"\nRelease memory\n";}

void main()

{item x;




Note: The ~item is a destructor it will be invoked automatically as soon it the object comes out of the block.  There fore destructor should not be called in the main program.


A Sample program of how constructors are defined in different ways.

class item

{ int number;

float cost;



{number =10;

cost= 12.34;}

/*item(int x=200)

{number =x;

cost =222.345;} */ item(int a, float b); item(item &x);

void getdata(int a, float b);

//Create inline function inside a class void putdata(void)

{ cout<<"Number:" << number << "\n";

cout<<"Cost:" << cost << "\n";



void item::getdata(int a, float b)

{ number = a;

cost = b;


item::item(int a, float b)

{number =a; cost=b;}

item::item(item &i1)

{number=i1.number; cost=i1.cost;}

void main()



{item x; //create object x;

cout<<"\nConstructor without argument"<< "\n";



{int a; float b;


item x(a,b); //create object x;

cout<<"\nConstructor with dynamic initialization"<< "\n";



{item x(111,123.456); //create object x; cout<<"\nConstructor with arguments"<< "\n"; x.putdata();

item y(x); item z=x; item a; a=x;

cout<<"\nConstructor with object as argument"<< "\n";





item y; //create object y; cout<<"\nobject y"<< "\n"; y.getdata(100, 399.95); y.putdata();


Posted Date: 9/20/2012 8:15:50 AM | Location : United States

Related Discussions:- Destructor, Assignment Help, Ask Question on Destructor, Get Answer, Expert's Help, Destructor Discussions

Write discussion on Destructor
Your posts are moderated
Related Questions
Implement a menu driven real estate catalogue system that allows users to perform various catalogue maintenance and search tasks.  You are only allowed to use the C programming la

A: The simplest way is to use a stringstream: #include #include #include using namespace std; string itos(int i) // convert int to string { stringstream s;

Multiple Inheritance Multiple inheritance , as the name suggests , is  deriving a class from more than one class . The derived class inherits all the properties of all its base c

Explain high-order and low-order bytes. - Numbers are written from left to right in decreasing order of significance. In the same way, bits in a byte of computer memory can be

Read numbers form user and place them in array: Program is to read a group numbers of numbers form user and place them in array type of float and sort them void sort(floa

to design a car that travels along the room and gives the length of the room

Function Overloading Function overloading is a form of polymorphism. Function overloading facilitates explaining one function having many forms. In other words it facilitates e

c program of double linked list with full explanation

6999066263304447777077766622337778 -----> message sent by the first smuggler. my name is robert---------> message decoded by the second smuggler. Where ‘0’ denotes the "space".