Monday 14 April 2014

Write A C++ Program To Implement The Inheritance And The Run-Time Polymorphism


Write A C++ Program To Implement The Inheritance And The Run-Time Polymorphism


Ex: No: 6 INHERITANCES, RUNTIME POLYMORPHISM

Aim:To write a c ++ program to implement the inheritance and the run-time polymorphism

Algorithm:
Step 1: Start the program.
Step 2: Create a class account with the following member variable name, acc_no, acc_type, and balance.
Step 3: Create derived classes with following member variables
(i).For current account information like balance gets deposit and get withdrawal amount.
(ii).For saving account information like balance get deposit and get withdrawal amount.

Step 4: Write a member function to get the deposit and withdrawal amount and to update the balance information for current and savings etc.
Step 5: Write a member function to display the balance information for respective account.
Step 6: Stop the program.


Program:

#include
#include
class account
{
char name[25];
char acc_no[20];
char acc_type[15];
float balance;
public:void readdata()
{
cout<<"enter name:";
cin>>name;
cout<<"account number:";
cin>>acc_no;
cout<<"account type:";
cin>>acc_type;
cout<<"balance:";
cin>>balance;
}
void displayaccountdata()
{
cout<<"name:"<
cout<<"account number:"<
cout<<"account type:"<
cout<<"balance:"<
}
int bal()
{
return balance;
}
};
class currentaccount:virtual public account
{
protected:
int deposit,withdraw,balance;
public:
int getdeposit()
{
cout<<"enter the amount to be deposited in the account:"<
cin>>deposit;
return deposit;
}
int getwithdraw()
{
cout<<"enter the amount to be withdraw from the account:"<
cin>>withdraw;
return withdraw;
}
};
class current:public currentaccount
{
private:
int balance;
public:
void depositbal()
{
balance=getdeposit()+bal();
cout<<"balance amount in the account:"<
}
void withdrawbal()
{
balance=bal()-getwithdraw();
cout<<"balance amont in the account:"<
}
};
class savingaccount:virtual public account
{
protected:
int deposit,withdraw,balance;
public:int getdeposit()
{
cout<<"enter the amount to be deposited in the account:"<
cin>>deposit;
return deposit;
}
int getwithdraw()
{
cout<<"enter the amount to be deposit in the account:"<
cin>>deposit;
return deposit;
}
};
class saving:public savingaccount
{
private:
int balance;
public:
void depositbal()
{
balance=getdeposit()+bal();
cout<<"balance amount in the account:"<
}
void withdraw()
{
cout<<"balance amount in the account:"<
}
};
void main()
{
clrscr();
current c1;
int option,choice;
c1.readdata();
cout<<"enter up option:1 current account 2.saving account:"<
cin>>option;
if(option==1)
{
cout<<"1.withdraw 2.deposit:"<
cin>>choice;
if(choice==2)
{
c1.displayaccountdata();
c1.depositbal();
}
else if(choice==1)
{
c1.displayaccountdata();
c1.withdrawbal();
}
}
if(option==2)
{
cout<<"1.withdraw 2.deposit:"<
cin>>choice;
{
c1.displayaccountdata();
c1.depositbal();
}
if(choice==1)
{
c1.displayaccountdata();
c1.withdrawbal();
}
}
getch();
}

Output:

Enter Name: VIJAY
Account No: 55555
Account type: AB
Balance: 10000
Enter up option: 1.Current account 2.Savings account
1. Withdraw 2.Deposit
Name: VIJAY
Account No: 55555
Account Type: AB
Balance: 10000
Enter the amount to be deposited in the account: 1000
Balance amount in the account: 11000

Result:

Thus the implementation of c ++ program for inheritance, runtime polymorphism is executed and the output has been verified.

0 comments:

Post a Comment