Destructor, C/C++ Programming

Destructor:

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";

}

~item();

};

 

item::~item()

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

void main()

{item x;

x.putdata();

}

 

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;

public:

item(void)

{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()

{

clrscr();

{item x; //create object x;

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

x.putdata();

}

{int a; float b;

cin>>a>>b;

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

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

x.putdata();

}

{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";

y.putdata();

z.putdata();

a.putdata();

}

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
# include stdio.h> # include string.h> # include conio.h> void main()   {           int i=0,count=0;           char a[100],b;           clrscr();

Explain Operators in C Language? The C language is very rich in built - in - operators and it places more significance on operators than do most other computer languages. The C

C++ Programming Assignment ATM Machine Phase 1 In this assignment you will create a program that allows a user to do the following: 1) Create a bank account by supplying a use

given integer ''n'' and permutation of numbers 1,2,..n-1, c program to print permutation that lexicograghically precedes given input permutation if permutation is least print input

Jewels can only be removed for polishing from either end of the necklace

string S convert it to a palindrome by doing chara, C/C++ Programming

Using your mountain project source code as a starting point, you are to write a code that randomly chooses one of the triangles on the surface of your mountain, this will be the en

Automatic Variables Automatic variables are variable which are explained within the functions. They lose their value when the function terminates. It can be accessed only in th

What is inheritance? Class, the vehicle, which is used to execute object-oriented concepts in C++, has given a new dimension to this idea of reusability. Many vendors now offer

Literals (Constants) Constants are data storage locations whose address is not accessible for the user. Their value is not altered during the course of the program. Literal