G. H. Raisoni college of engineering, nagpur



Download 117.52 Kb.
Page2/2
Date17.12.2020
Size117.52 Kb.
#55034
1   2
PracticalNo.2 3rd Sem B 28

Default Constructor


A Default constructor is a type of constructor which doesn’t take any argument and has no parameters.

Parameterized Constructor


Passing of parameters to the constructor is possible. This is done to initialize the value using these passed parameters. This type of constructor is called a parameterized constructor.


Copy Constructor


A Copy Constructor is a Constructor which initializes an object of a class using another object of the same class.
Destructor

Destructors are another type of member function that is responsible for destroying or deleting the object. It frees up space occupied by the object after it is no longer needed. A Destructor is called automatically when the object is out of scope and no longer needed. A Destructor has the name same as the class name, but the only difference is that the name is preceded by a tile(~).

~test() { } ;

There has to be only one Destructor in a class. A Destructor has no return type and no parameters. If we do specify a destructor in class then, the compiler creates a default destructor. The default destructor works fine unless memory is dynamically allocate or pointer is declared.


The Destructor is called when,

  • A function ends.

  • A program ends.

  • A block that contains the local variable ends.

  • A delete operator is called in the program.




Algorithm

Step 1: Start

Step 2: Write a program to implement constructor and destructor in the form of ATM machine.

Step 3: Using main function, display menu for the different operations to be performed on the constructor and destructor.
Step 4: Read the value of the selected choice.
Step 5:- If the selected choice is 1 then displayed preference would be View Balance.
Step 6 :- If the selected choice is 2 then displayed preference would be Withdraw.

Step 7 :- If the selected choice is 3 then displayed preference would be change the password.

Step 8:- If the selected choice is 4 then displayed preference would be cancel the transaction.

Step 9:- Stop.



Flowchart



Program

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;
class ATM_Card{
private:

int ATM_PIN;

public:

ATM_Card(int pin){


ATM_PIN=pin;

}
int get_atm_pin()

{

return ATM_PIN;



}
void set_atm_pin(int pin1)

{

ATM_PIN=pin1;



cout<<"\nPIN changed successfully";

}
};


class ATM_Transaction {
private:

float balance;

int Transaction_PIN;

bool status;

int amount;
public:

ATM_Transaction(){

status=true;

balance=5000;

}
void set_transaction_pin(int pin);

void View_Balance();

void Withdraw();

void exit();


~ATM_Transaction(){

status=false;

cout<<"\nDestructor call : Transaction is ended\n";

}
};


int arr_pin[5]={3456,4349,3244,2431,5566};
void ATM_Transaction::set_transaction_pin(int pin)

{

Transaction_PIN=pin;



}
void ATM_Transaction::View_Balance()

{

cout<<"\n\t\t\t\tYour Balance is : "<

}
void ATM_Transaction::Withdraw()

{
cout<<"\nEnter Amount to withdraw : ";

cin>>amount;

if(amount >0 && amount < balance)

{

balance=balance-amount;



cout<<"\nWithdraw Succcess! New balance is : ";

cout<

}

else


cout<<"\nInsufficient balance in your account\n";

}
void ATM_Transaction::exit()

{

cout<<"\nTransaction is cancelled by the user\n";



}
int main() {

system ("Color 5f");

cout<<"\t\t\t--------------------\n"<

//Prompt to show today's date

cout << "\t\tCurrent date : ";

//Show date and time function

time_t now;

time(&now);

printf("%s\n", ctime(&now));;

//Give space for the function of date and time

cout<<"\t\t\t--------------------\n"<

cout<<"\tPress 1 and Then Press Enter to Access Your Account Via Pin Number\n\n";

int access;

cin>>access;

switch(access)

{

case 1://pin to access account



system("cls");int i, pin;

cout<<"\n\nEnter Your Acc Pin Access Number! [Only one attempt is allowed]\n\n"<

cin>>pin;

system("cls");


int user_choice,pin3,flag=0,y=1;
for(int i=0;i<5;i++)

if(arr_pin[i]==pin)

flag=1;

if(flag==0)

{

cout<<"\nTransaction Aborted! Wrong PIN";



return 0;
}

ATM_Card atm(pin);


ATM_Transaction transac;
transac.set_transaction_pin(atm.get_atm_pin());
while(y){
cout<<"\n\t\t\t\tWelcome user! Enter your choice\n\n";

cout<<"\t\t\t\t1. View Balance\n\t\t\t\t2. Withdraw money\n\t\t\t\t3. Change Card PIN\n\t\t\t\t4. Cancel Transaction\n";


cin>>user_choice;
switch(user_choice)

{

case 1:



transac.View_Balance();

break;
case 2:

transac.Withdraw();

break;
case 3:

cout<<"\nEnter new ATM pin: ";

cin>>pin3;

atm.set_atm_pin(pin3);

break;
case 4:

transac.exit();

return 0;


}

cout<<"\n\nDo you want to continue? \nPress 1 for continue and 0 to exit: ";

cin>>y;

cout<<"-------------------------\n";

system("cls");
}

return 0;



}

}


Output









Conclusion

Hence implementation of Constructor and Destructor has been done successfully.

Download 117.52 Kb.

Share with your friends:
1   2




The database is protected by copyright ©ininet.org 2024
send message

    Main page