3.constructor and destructor

6
AIM : To write a C++ program using constructor and destructor. ALGORITHM: 1. Start the program. 2. Initializes a class account. 3. Declare variable balance, rate under private. 4. Declare a constructor and destructor. 5. Declare function deposit, withdraw, compound, and get balance and main function menu. 6. Access the constructor and destructor. 7. In deposit function the amount is enquired and is added to initial balance. 8. In withdraw function, amount enquired withdraw from the balance. 9. In compound function, interest enquired to calculate the interest. 10. In get balance function, the current balance is shown. 11. Option are provides in menu to perform various operations. 12. Main function declared, acct variable declared to access the menu function. 13. Switch case initialized to select the operation to be executed. 14. Stop the program. Ex no: CONSTRUCTOR AND DESTRUCTOR Date: Page No. 8

Upload: suganya-periasamy

Post on 22-Oct-2015

7 views

Category:

Documents


3 download

DESCRIPTION

3.Constructor and Destructor. with its examples and functions

TRANSCRIPT

Page 1: 3.Constructor and Destructor

AIM :

To write a C++ program using constructor and destructor.

ALGORITHM:

1. Start the program.

2. Initializes a class account.

3. Declare variable balance, rate under private.

4. Declare a constructor and destructor.

5. Declare function deposit, withdraw, compound, and get balance and main function menu.

6. Access the constructor and destructor.

7. In deposit function the amount is enquired and is added to initial balance.

8. In withdraw function, amount enquired withdraw from the balance.

9. In compound function, interest enquired to calculate the interest.

10. In get balance function, the current balance is shown.

11. Option are provides in menu to perform various operations.

12. Main function declared, acct variable declared to access the menu function.

13. Switch case initialized to select the operation to be executed.

14. Stop the program.

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Ex no:CONSTRUCTOR AND DESTRUCTOR

Date:

Page No. 8

Page 2: 3.Constructor and Destructor

PROGRAM:

#include<iostream.h>

#include<conio.h>

class account

{

private:

float balance;

float rate;

public:

account();

~account();

void deposit();

void withdraw();

void compound();

void getbalance();

void menu();

};

account::account()

{

cout<<"Enter the initial balance:\n";

cin>>balance;

cout<<"Interest Rate:\n";

cin>>rate;

}

account::~account()

{

cout<<"Data base has been deleted.\n";

}

void account :: deposit()

{

float amount;

cout<<"Enter the amount:\n";

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No. 9

Page 3: 3.Constructor and Destructor

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

cout<<balance<<endl;

}

void account::menu(){

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

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No. 10

Page 4: 3.Constructor and Destructor

cout<<"w->withdraw"<<endl;cout<<"c->Compound interest"<<endl;

cout<<"g->getbalance"<<endl;

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

cout<<"Option please?\n";

}

void main(void)

{

clrscr();

account acct;

char ch;

acct.menu();

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

{

switch(ch)

{

case 'd':

acct.deposit();

break;

case 'w':

acct.withdraw();

break;

case 'c':

acct.compound();s

break;

case 'g':

acct.getbalance();

break;

}

}

getch();

}

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No. 11

Page 5: 3.Constructor and Destructor

RESULT:

Thus the C++ program using constructor and destructor has been executed and verified

successfully.

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No. 12