Destructors Assignment Help

Assignment Help: >> Union, Nested Classes, Constructors and Destructors - Destructors

Destructors

A destructor is a function which automatically executes whenever an object is destroyed. Destructor function obtains executed whenever an object of the class to that it belongs goes out of existence. The main use of the destructor function is to release space on the heap.

Rules for writing destructor function:

1.   A destructor function name is the similar as that of the class it belongs except in which the first character of the name have to be a tilde (~).

2.   It is declared along with no return type.

3.   Destructors cannot be declared const, static or volatile

4.   It takes no arguments and thus cannot be overloaded

5.   It should have public access in the class declaration. The common form is

class user_name

{

private:

//data variables;

//member functions;

public:

user_name ( ); //constructor

~user_name( );          //destructor

//other member functions;

};

Example:

class student

{

public:

char name[20];

int age;

float avg;

int mark1,mark2, mark3;

student( );      //construcor

~student( );    //destructor

void getdata ( );

void display ( );

}

// description of constructor and destructor

# include<iostream.h>

# include<stdio.h>

class accout

{

private:

float balance;

float rate;

public:

account ( );

~account ( );

void deposit ( );

void compound ( );

void getbalance ( );

void menu ( );

};

account :: account ( )            //constructor

{

cout << "enter the initial balance \n";

cin>>balance;

cout<<"interest rate"<< endl;

cin>> rate;

}

account :: ~account ( )          // destructor

{

cout<<"data base deleted"<< endl;

}

void account :: deposit ( )

{

float amount;

cout<<"enter the amount"<< endl;

cin>>amount;

balance = balance + amount;

}

void account :: withdraw ( )

{

float amount;

cout<<"how much to withdraw?\n";

cin>>amount;

if ( amount <= balance)

{

balance = balance - amount;

cout<<"amount drawn = "<<amount<< endl;

cout<<"current balance = "<<balance<<endl;

}

else

cout<<0;

}

void account :: compound ( )

{

float interest;

interest = balance * rate;

balance = balance + interest;

cout<<"interest amount = " << interest<< endl;

cout<<"total amount = << balance<<endl;

}

void account :: getbalance ( )

{

cout<<"current balance : = "<<balance<<endl;

}

void account :: menu ( )

{

cout <<" press d -> deposit"<<endl;

cout <<" press w -> withdraw"<<endl;

cout <<" press c -> compound interest"<<endl;

cout <<" press g -> get balance"<<endl;

cout <<" press q -> quit"<<endl;

cout <<" option, please?\n";

}

void main (void)

{

class account acct;

char ch;

while ((ch = getchar ( ) ) ! = 'q')

{

switch (ch)

{

case 'd':

acct .deposit ( );

break;

case 'w':

acct . withdraw ( );

break;

case 'c':

acct . compound ( );

break;

case 'g':

acct . getbalance ( );

break;

}

}

}

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