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

Explain variables, Variables Information stored in a variable can alter...

Variables Information stored in a variable can alter in the course of the program. The type used in the definition explains the kind of information the symbol can store. Variab

Areaunder curve, Write a program to find the area under the curve y = f(x) ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b

Vision based simultaneous mapping and localization, Project Description: ...

Project Description: Design and prepare software that can navigate a mobile robot using SLAM technique using vision sensor (camera). Skills required are C Programming, Engine

Bubble sort c program, Bubble sort C program: Write a program to defin...

Bubble sort C program: Write a program to define a bubble sort. void main()  {   clrscr();   int a[100],ch,n;   cout   cin>>n;   for (int i=0;i

Define how passing arrays to a function, Define How Passing Arrays to a Fun...

Define How Passing Arrays to a Function? A complete array can be passed to a function as an argument. The manner in which the array is passed be different from that of an ordin

ASCII, A string S is said to be "Super ASCII", if it contains the character...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci

Should one design a classes from the outside, Should one design a classes f...

Should one design a classes from the outside (interfaces first) or inside (data first)? A: From the outside. A superior interface provides a simplified view which is express

Linux driver and linux, Project Description: I´ve a need linux programmi...

Project Description: I´ve a need linux programming job. if you are interested, Skills required are C Programming, PCB Layout, Embedded Software, Python, Software Architecture

Mat lab programming, MAT LAB programming Project Description: Just fo...

MAT LAB programming Project Description: Just for who are PROFESSIONAL IN MATLAB i have simulation and i would like to simulate the equation in ,and test the all simulatio

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