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

Write a program that reads a line of characters, Write a program that reads...

Write a program that reads a line of characters from the user and displays that entire line after converting any uppercase characters to lowercase also change any lowercase charact

Explain encapsulation and data hiding, Encapsulation and Data Hiding Th...

Encapsulation and Data Hiding The property of being a self-contained unit is known as encapsulation. The idea that the encapsulated unit can be used without knowing how it work

Programming, hello i have about 15 hours the dead line I Need a program in...

hello i have about 15 hours the dead line I Need a program in c language that''s determine the router ip or the gateway ip of the current NIC example the router ip is: 192.168.1.1

Blanche has a fashion design company called BLB_Best_Clothin, Blanche has a...

Blanche has a fashion design company called BLB_Best_Clothing Pty. That she has just opened recently

What is the difference among const char *mypointer &char *, What is the dif...

What is the difference among const char *myPointer and char *const myPointer?  A: Const char *myPointer is a non constant pointer to constant data; whereas char *const myPointer

I want skype recorder application, I want Skype Recorder Application + Setu...

I want Skype Recorder Application + Setup + Sourcecode + NICE UI Project Description: i want an application built which will allow user to record skype audio or video calls

Control structures in cpp, C o n t r o l S t r u c t u r e s ...

C o n t r o l S t r u c t u r e s I t i s o f t h r e e t y p e s: 1 .    S e qu e n c e s t r u c t u r e 2 .

Menus, create a shopping cart in c++

create a shopping cart in c++

Explain syntax rules for writing a destructor function, Syntax rules for wr...

Syntax rules for writing a destructor function Its name is the similar as that of the class to which it belongs, except that the first character of the name is the symbol t

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