Destructor, C/C++ Programming

Assignment Help:

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

}


Related Discussions:- Destructor

#Luminous Jewels, #Byteland county is very famous for luminous jewels. Lumi...

#Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Ne

How to write program, how to write a program for all the types of beam reac...

how to write a program for all the types of beam reactions

Define the c preprocessor, Define the C Preprocessor? Preprocessor' is ...

Define the C Preprocessor? Preprocessor' is a translation stage that is applied to your source code before the compiler proper gets its hands on it usually the preprocessor per

Help, Deliverables: you are required to upload your c code in the assignmen...

Deliverables: you are required to upload your c code in the assignment dropbox set in Moodle. You are supposed to work with Linux gcc compiler and pico editor for compiling via the

Cpp, At a shop of marbles, packs of marbles are prepared. Packets are named...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Write a program of constructors and destructors, Write a program of constru...

Write a program of constructors and destructors Make a class drugs having encapsulated data for medicine name, whether solid or liquid, price and purpose of use. From this clas

Sizeof() operator, What is the specialty in sizeof() operator

What is the specialty in sizeof() operator

C program for bubble sort, C program for bubble sort: void main() {...

C program for bubble sort: void main() { int i,j,k,a[10],n; clrscr(); printf("How many values you want to enter\n"); scanf("%d",&n);  for(i=0;i  {  pri

Described the difference among "new" and "operator new" ?, Described the di...

Described the difference among "new" and "operator new" ? A:"operator new" works such as malloc.

I want to convert atmel c code into arduino mppt, Normal 0 fals...

Normal 0 false false false EN-US X-NONE X-NONE

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd